Bug 94767 - (unsigned bitfield) + (int) operation results in int, not unsigned int.
Summary: (unsigned bitfield) + (int) operation results in int, not unsigned int.
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, wrong-code
Depends on:
Blocks:
 
Reported: 2020-04-26 02:08 UTC by jh718.park
Modified: 2020-04-27 10:03 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-04-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jh718.park 2020-04-26 02:08:13 UTC
For these variables below,

unsigned m_schemeEnd : 26;
unsigned m_userStart;

m_userStart == m_schemeEnd + 1

this comparison emits a compiler warning as below.

warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]

This bug was found during WebKit gtk port build with gcc 9.3.0.

Temporarily, this warning was removed by this patch,
https://trac.webkit.org/changeset/260715/webkit
like

bool slashSlashNeeded = m_userStart == static_cast<unsigned>(m_schemeEnd + 1);

but we think that this bug should be fixed in gcc.
Comment 1 Andrew Pinski 2020-04-26 08:41:37 UTC
I think this has been fixed on the trunk.
Comment 2 Richard Biener 2020-04-27 06:40:33 UTC
Who knows, please provide a testcase that reproduces the issue.
Comment 3 Jonathan Wakely 2020-04-27 08:36:17 UTC
(In reply to jh718.park from comment #0)
> For these variables below,
> 
> unsigned m_schemeEnd : 26;
> unsigned m_userStart;
> 
> m_userStart == m_schemeEnd + 1
> 
> this comparison emits a compiler warning as below.
> 
> warning: comparison of integer expressions of different signedness:
> ‘unsigned int’ and ‘int’ [-Wsign-compare]

Why do you think that's wrong?

In [conv.prom] p5 the standard says:

"A prvalue for an integral bit-field (11.4.9) can be converted to a prvalue of type int if int can represent all the values of the bit-field;"

Since int is wider than 26 bits it can represent all the values of m_schemeEnd, so the operands of 'm_schemeEnd + 1' are both promoted to int, and the result is an int.
Comment 4 jh718.park 2020-04-27 10:03:24 UTC
(In reply to Jonathan Wakely from comment #3)
> (In reply to jh718.park from comment #0)
> > For these variables below,
> > 
> > unsigned m_schemeEnd : 26;
> > unsigned m_userStart;
> > 
> > m_userStart == m_schemeEnd + 1
> > 
> > this comparison emits a compiler warning as below.
> > 
> > warning: comparison of integer expressions of different signedness:
> > ‘unsigned int’ and ‘int’ [-Wsign-compare]
> 
> Why do you think that's wrong?
> 
> In [conv.prom] p5 the standard says:
> 
> "A prvalue for an integral bit-field (11.4.9) can be converted to a prvalue
> of type int if int can represent all the values of the bit-field;"
> 
> Since int is wider than 26 bits it can represent all the values of
> m_schemeEnd, so the operands of 'm_schemeEnd + 1' are both promoted to int,
> and the result is an int.

Thank you for your comment, Jonathan.
I understand your point.

I thought that unsigned bitfield should be converted to unsigned
during usual arithmetic conversions without knowing the item,
http://eel.is/c++draft/conv.prom#5.

Then, I will mark this issue as resolved/invalid, and update the bug
https://bugs.webkit.org/show_bug.cgi?id=211044
with the comment you left here.

Thank you for your help:)