public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
@ 2002-08-05  5:20 Dylan Cuthbert
  2002-08-05  7:10 ` Fergus Henderson
  0 siblings, 1 reply; 8+ messages in thread
From: Dylan Cuthbert @ 2002-08-05  5:20 UTC (permalink / raw)
  To: gcc


for ( int i = 0; i < 10; i++ )
{
    int i = 10;       <===== shadowing the i variable
    printf( "%d\n", i  );
}


To my understanding, the "int i" in the for statement is within the scope of
the following curly brackets?

If this isn't an error (ie. there is a special "mini" scope created around
the for statement's scope), then it *definitely* *definitely* (pretty
please) should throw up a warning.  I just spent a while looking at some
code before realising it was something as silly as this.

Regards

---------------------------------
Q-Games, Dylan Cuthbert.
http://www.q-games.com
P2P internet radio - http://www.peercast.org




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
  2002-08-05  5:20 shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin) Dylan Cuthbert
@ 2002-08-05  7:10 ` Fergus Henderson
  2002-08-05 18:29   ` Dylan Cuthbert
  0 siblings, 1 reply; 8+ messages in thread
From: Fergus Henderson @ 2002-08-05  7:10 UTC (permalink / raw)
  To: Dylan Cuthbert; +Cc: gcc

That's what `-Wshadow' is for.

Were you compiling with `-Wshadow' enabled?

On 05-Aug-2002, Dylan Cuthbert <dylan@q-games.com> wrote:
> 
> for ( int i = 0; i < 10; i++ )
> {
>     int i = 10;       <===== shadowing the i variable
>     printf( "%d\n", i  );
> }
> 
> 
> To my understanding, the "int i" in the for statement is within the scope of
> the following curly brackets?
> 
> If this isn't an error (ie. there is a special "mini" scope created around
> the for statement's scope), then it *definitely* *definitely* (pretty
> please) should throw up a warning.  I just spent a while looking at some
> code before realising it was something as silly as this.
> 
> Regards
> 
> ---------------------------------
> Q-Games, Dylan Cuthbert.
> http://www.q-games.com
> P2P internet radio - http://www.peercast.org
> 
> 
> 

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
  2002-08-05  7:10 ` Fergus Henderson
@ 2002-08-05 18:29   ` Dylan Cuthbert
  2002-08-05 18:41     ` Dylan Cuthbert
  0 siblings, 1 reply; 8+ messages in thread
From: Dylan Cuthbert @ 2002-08-05 18:29 UTC (permalink / raw)
  To: gcc


Hi there, I am compiling with -Wall and -Werror, surely that should
include -Wshadow?  If it doesn't currently, then I suggest it should be.

The scoping seems strange in gcc, is that the iso-standard?  ie. the for
statement is making *two* scopes, one to scope the variables created within
its statement and the other for the body.

Regards

---------------------------------
Q-Games, Dylan Cuthbert.
http://www.q-games.com
P2P internet radio - http://www.peercast.org

"Fergus Henderson" <fjh@cs.mu.OZ.AU> wrote in message
news:20020805141033.GA22390@ceres.cs.mu.oz.au...
> That's what `-Wshadow' is for.
>
> Were you compiling with `-Wshadow' enabled?
>
> On 05-Aug-2002, Dylan Cuthbert <dylan@q-games.com> wrote:
> >
> > for ( int i = 0; i < 10; i++ )
> > {
> >     int i = 10;       <===== shadowing the i variable
> >     printf( "%d\n", i  );
> > }
> >
> >
> > To my understanding, the "int i" in the for statement is within the
scope of
> > the following curly brackets?
> >
> > If this isn't an error (ie. there is a special "mini" scope created
around
> > the for statement's scope), then it *definitely* *definitely* (pretty
> > please) should throw up a warning.  I just spent a while looking at some
> > code before realising it was something as silly as this.
> >
> > Regards
> >
> > ---------------------------------
> > Q-Games, Dylan Cuthbert.
> > http://www.q-games.com
> > P2P internet radio - http://www.peercast.org
> >
> >
> >
>
> --
> Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the
pursuit
> The University of Melbourne         |  of excellence is a lethal habit"
> WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
>



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
  2002-08-05 18:29   ` Dylan Cuthbert
@ 2002-08-05 18:41     ` Dylan Cuthbert
  2002-08-05 19:32       ` Fergus Henderson
  0 siblings, 1 reply; 8+ messages in thread
From: Dylan Cuthbert @ 2002-08-05 18:41 UTC (permalink / raw)
  To: gcc

I just tried the -Wshadow option and it seems to be a bit of overkill for
the problem I'm describing.

The std boost libs don't even compile with this option enabled so the option
is pretty much useless at this time.

My problem is more to do with the iso-compliancy of gcc's current
implementation of scopes for the "for" statement.  I was always under the
impression that the variables created within the for statement are part of
the body scope of the for statement, not in some kind of implicit "mini"
scope created around the body.  It seems wrong and can lead to bugs and
confusion.  I cannot think of one possible reason why the user would want to
"overwrite" the variables created in the for statement.

Regards

---------------------------------
Q-Games, Dylan Cuthbert.
http://www.q-games.com
P2P internet radio - http://www.peercast.org

"Dylan Cuthbert" <dylan@q-games.com> wrote in message
news:ain8oa$sqp$1@main.gmane.org...
>
> Hi there, I am compiling with -Wall and -Werror, surely that should
> include -Wshadow?  If it doesn't currently, then I suggest it should be.
>
> The scoping seems strange in gcc, is that the iso-standard?  ie. the for
> statement is making *two* scopes, one to scope the variables created
within
> its statement and the other for the body.
>
> Regards
>
> ---------------------------------
> Q-Games, Dylan Cuthbert.
> http://www.q-games.com
> P2P internet radio - http://www.peercast.org
>
> "Fergus Henderson" <fjh@cs.mu.OZ.AU> wrote in message
> news:20020805141033.GA22390@ceres.cs.mu.oz.au...
> > That's what `-Wshadow' is for.
> >
> > Were you compiling with `-Wshadow' enabled?
> >
> > On 05-Aug-2002, Dylan Cuthbert <dylan@q-games.com> wrote:
> > >
> > > for ( int i = 0; i < 10; i++ )
> > > {
> > >     int i = 10;       <===== shadowing the i variable
> > >     printf( "%d\n", i  );
> > > }
> > >
> > >
> > > To my understanding, the "int i" in the for statement is within the
> scope of
> > > the following curly brackets?
> > >
> > > If this isn't an error (ie. there is a special "mini" scope created
> around
> > > the for statement's scope), then it *definitely* *definitely* (pretty
> > > please) should throw up a warning.  I just spent a while looking at
some
> > > code before realising it was something as silly as this.
> > >
> > > Regards
> > >
> > > ---------------------------------
> > > Q-Games, Dylan Cuthbert.
> > > http://www.q-games.com
> > > P2P internet radio - http://www.peercast.org
> > >
> > >
> > >
> >
> > --
> > Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the
> pursuit
> > The University of Melbourne         |  of excellence is a lethal habit"
> > WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S.
Garp.
> >
>
>
>
>



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
  2002-08-05 18:41     ` Dylan Cuthbert
@ 2002-08-05 19:32       ` Fergus Henderson
  2002-08-05 19:40         ` Gabriel Dos Reis
  0 siblings, 1 reply; 8+ messages in thread
From: Fergus Henderson @ 2002-08-05 19:32 UTC (permalink / raw)
  To: Dylan Cuthbert; +Cc: gcc

On 06-Aug-2002, Dylan Cuthbert <dylan@q-games.com> wrote:
> I just tried the -Wshadow option and it seems to be a bit of overkill for
> the problem I'm describing.
>
> The std boost libs don't even compile with this option enabled so the option
> is pretty much useless at this time.

Well, -Wshadow only generates warnings, not errors, unless you compile
with -Werror.  Not everyone uses Boost, and not everyone uses -Werror.
So I don't agree that -Wshadow is useless.

However, on further investigation I see that the C++ standard requires
a diagnostic for examples like the one that you posted.
So g++ should be issuing an error, even without -Wshadow.

Please file a bug report (see <http://gcc.gnu.org/bugs.html>).

> My problem is more to do with the iso-compliancy of gcc's current
> implementation of scopes for the "for" statement.  I was always under the
> impression that the variables created within the for statement are part of
> the body scope of the for statement, not in some kind of implicit "mini"
> scope created around the body.  It seems wrong and can lead to bugs and
> confusion.  I cannot think of one possible reason why the user would want to
> "overwrite" the variables created in the for statement.

Your impression is not quite correct.
The standard says

 |    6.5 - Iteration statements [stmt.iter]
 ...
 |    -2- The substatement in an iteration-statement implicitly defines a
 |        local scope (basic.scope) which is entered and exited each time
 |        through the loop.

and

 |   6.5.3 - The for statement [stmt.for]
 |  
 |    -1- The for statement
 |  
 | for ( for-init-statement conditionopt ; expressionopt ) statement
 |  
 |    is equivalent to
 |  
 | {
 |         for-init-statement
 |         while ( condition ) {
 |                 statement
 |                 expression ;
 |         }
 | }
 |  
 |    except [... some exceptions that aren't revelant here ...].

So the the variables created within the for statement are not
part of the body scope of the for statement.

However, it is nevertheless an error to redeclare the same
name in the outermost scope of the loop body:

 |   3.3.2 - Local scope [basic.scope.local]
 |    -4- Names declared in the for-init-statement, and in the condition of
 |    if, while, for, and switch statements are local to the if, while, for,
 |    or switch statement (including the controlled statement), and shall
 |    not be redeclared in a subsequent condition of that statement nor in
 |    the outermost block (or, for the if statement, any of the outermost
 |    blocks) of the controlled statement; see stmt.select.

Since this is a requirement on the form of the program, programs which
violate this requirement are ill-formed, and a diagnostic is required.
g++ 3.1 fails to report any diagnostic, even with -ansi -pedantic.
So it does not conform to this part of the standard.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
  2002-08-05 19:32       ` Fergus Henderson
@ 2002-08-05 19:40         ` Gabriel Dos Reis
  2002-08-05 20:14           ` Dylan Cuthbert
  0 siblings, 1 reply; 8+ messages in thread
From: Gabriel Dos Reis @ 2002-08-05 19:40 UTC (permalink / raw)
  To: Fergus Henderson; +Cc: Dylan Cuthbert, gcc

Fergus Henderson <fjh@cs.mu.OZ.AU> writes:

[...]

| However, on further investigation I see that the C++ standard requires
| a diagnostic for examples like the one that you posted.
| So g++ should be issuing an error, even without -Wshadow.
| 
| Please file a bug report (see <http://gcc.gnu.org/bugs.html>).

At least two PRs were filled on the topic (one by myself, a loooong time
ago).  I don't know whether one more PR is necessary.

-- Gaby

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
  2002-08-05 19:40         ` Gabriel Dos Reis
@ 2002-08-05 20:14           ` Dylan Cuthbert
  2002-08-06  3:16             ` Gabriel Dos Reis
  0 siblings, 1 reply; 8+ messages in thread
From: Dylan Cuthbert @ 2002-08-05 20:14 UTC (permalink / raw)
  To: gcc

How long ago?

Also, to what mail are you referring?  It hasn't appeared on the gmane news
server, yet your reply to it has, very strange...

--
---------------------------------
Q-Games, Dylan Cuthbert.
http://www.q-games.com
P2P internet radio - http://www.peercast.org

"Gabriel Dos Reis" <gdr@integrable-solutions.net> wrote in message
news:m3r8hcg19y.fsf@soliton.integrable-solutions.net...
> Fergus Henderson <fjh@cs.mu.OZ.AU> writes:
>
> [...]
>
> | However, on further investigation I see that the C++ standard requires
> | a diagnostic for examples like the one that you posted.
> | So g++ should be issuing an error, even without -Wshadow.
> |
> | Please file a bug report (see <http://gcc.gnu.org/bugs.html>).
>
> At least two PRs were filled on the topic (one by myself, a loooong time
> ago).  I don't know whether one more PR is necessary.
>
> -- Gaby
>



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
  2002-08-05 20:14           ` Dylan Cuthbert
@ 2002-08-06  3:16             ` Gabriel Dos Reis
  0 siblings, 0 replies; 8+ messages in thread
From: Gabriel Dos Reis @ 2002-08-06  3:16 UTC (permalink / raw)
  To: Dylan Cuthbert; +Cc: gcc

"Dylan Cuthbert" <dylan@q-games.com> writes:

| How long ago?

One year ago.  See PR/2288.  One year is a long time in GCC
development cycle, IMHO.

| Also, to what mail are you referring? 

I'm afraid I dont' understand your question.  I'm replying to Fergus'
message 

     http://gcc.gnu.org/ml/gcc/2002-08/msg00308.html

-- Gaby

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2002-08-06 10:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-05  5:20 shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin) Dylan Cuthbert
2002-08-05  7:10 ` Fergus Henderson
2002-08-05 18:29   ` Dylan Cuthbert
2002-08-05 18:41     ` Dylan Cuthbert
2002-08-05 19:32       ` Fergus Henderson
2002-08-05 19:40         ` Gabriel Dos Reis
2002-08-05 20:14           ` Dylan Cuthbert
2002-08-06  3:16             ` Gabriel Dos Reis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).