GCC Bugzilla will be upgraded from version 4.4.9 to 5.0rc3 on Saturday, April 25, starting around 17:00 UTC. The upgrade process should only last a few minutes. Check bug 64968 for details.
Bug 10852 - Old for-scoping rules improperly used?
Old for-scoping rules improperly used?
Status: NEW
Product: gcc
Classification: Unclassified
Component: c++
3.3
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
: accepts-invalid, monitored, rejects-valid
: 14515 (view as bug list)
Depends on:
Blocks: 12944 33260
  Show dependency treegraph
 
Reported: 2003-05-19 07:46 UTC by derrick
Modified: 2011-10-09 13:59 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-09-03 21:39:36


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description derrick 2003-05-19 07:46:00 UTC
I think this is a bug in g++; but there is always the possibility that it is a bug in my understanding of C++. In the following code gcc assumes that the variable i at the end of function is the same as in the for loop and produces the following error:
nstest.cc: In function `void f()':
nstest.cc:9: error: name lookup of `i' changed for new ISO `for' scoping
nstest.cc:6: error:   using obsolete binding at `i'

I think it should use the variable i from inside the namespace and quit complaining, but as I said, I may just be misunderstanding C++ arcana.

Here's the sample code...
namespace X {
  int i;
}

void f() {
  for (int i=0; i<10; ++i)
    ;
  using namespace X;
  i;
}

Release:
gcc-3.3
Comment 1 derrick 2003-05-19 07:46:00 UTC
Fix:
Workaround is to replace "using namespace X" with "using X::i"
Comment 2 Gabriel Dos Reis 2003-05-19 10:15:54 UTC
From: Gabriel Dos Reis <gdr@integrable-solutions.net>
To: derrick@cco.caltech.edu
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c++/10852: Old for-scoping rules improperly used?
Date: 19 May 2003 10:15:54 +0200

 derrick@cco.caltech.edu writes:
 
 | I think this is a bug in g++; but there is always the possibility that it is a bug in my understanding of C++. In the following code gcc assumes that the variable i at the end of function is the same as in the for loop and produces the following error:
 | nstest.cc: In function `void f()':
 | nstest.cc:9: error: name lookup of `i' changed for new ISO `for' scoping
 | nstest.cc:6: error:   using obsolete binding at `i'
 
 That is a bug in the compiler.
 
 The compiler should first look into X before complaining.
 
 -- Gaby
Comment 3 Wolfgang Bangerth 2003-05-19 14:12:47 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Gaby analyzed this. Interesting bug :-)
Comment 4 Andrew Pinski 2004-03-10 16:48:07 UTC
*** Bug 14515 has been marked as a duplicate of this bug. ***
Comment 5 Volker Reichelt 2006-03-07 15:54:15 UTC
Here's an example where the code is invalid, but is accepted because
of the for-scope-warning machinery:

==================================
namespace N
{
    int i;
}

int i;

void foo()
{
    for (int i=0; i<10; ++i) ;
    using namespace N;
    i;
}
==================================

The problem is that we track old and new scoping rules simultaneously
to be able to issue warnings. This doesn't work properly in corner
cases like in this PR.
Comment 6 Wolfgang Bangerth 2011-10-09 13:59:42 UTC
Still happens for gcc4.5.1.