Bug 62074 - "right shift count >= width of type" warning on dead branch in template code
Summary: "right shift count >= width of type" warning on dead branch in template code
Status: RESOLVED DUPLICATE of bug 4210
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-08-09 06:01 UTC by Brooks Moses
Modified: 2014-08-09 06:09 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brooks Moses 2014-08-09 06:01:41 UTC
Consider the following code:

------------------cut here---------------------
typedef unsigned int uint32;

template <uint32 offset>
int foo(uint32 x) {
  if (offset <= 1) {
    return x << offset;
  } else {
    // this branch is dead for offset = 0, but it still generates a warning
    return (x << (offset - 1)) +
           (x >> (32 - offset));
  }
}

int main(int argc, char** argv) {
  return foo<0>(argc);
}
------------------cut here---------------------

As noted in the comment, the "else" branch is dead code for the offset=0 instantiation, but (at r213772 on trunk) we still generate warnings for the shifts that are out of range in the offset=0 case:

$ /home/bmoses/gcc-archive/213772/bin/g++ b16825807.cc -o b16825807
b16825807.cc: In instantiation of ‘int foo(uint32) [with unsigned int offset = 0u; uint32 = unsigned int]’:
b16825807.cc:15:21:   required from here
b16825807.cc:9:15: warning: left shift count >= width of type
     return (x << (offset - 1)) +
               ^
b16825807.cc:10:15: warning: right shift count >= width of type
            (x >> (32 - offset));
               ^

(This is also present in the google/gcc-4_9 branch, and thus I suspect present on upstream 4.9.1 although I have not yet tested it.)

Google bug: 16825807
Comment 1 Andrew Pinski 2014-08-09 06:09:14 UTC
Dup of much older bug: 4210.

*** This bug has been marked as a duplicate of bug 4210 ***