Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect scoping of if(){}else{} #1068

Closed
p5pRT opened this issue Jan 22, 2000 · 4 comments
Closed

Incorrect scoping of if(){}else{} #1068

p5pRT opened this issue Jan 22, 2000 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Jan 22, 2000

Migrated from rt.perl.org#2020 (status was 'resolved')

Searchable as RT2020$

@p5pRT
Copy link
Author

p5pRT commented Jan 22, 2000

From mjtg@cus.cam.ac.uk

produces spurious warnings

"my" variable $x masks earlier declaration in same scope at - line 2.
"my" variable $x masks earlier declaration in same scope at - line 4.

Mike Guy

% perl -V
Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration​:
  Platform​:
  osname=solaris, osvers=2.6, archname=sun4-solaris
  uname=''
  hint=previous, useposix=true, d_sigaction=define
  usethreads=undef useperlio=undef d_sfio=undef
  Compiler​:
  cc='gcc', optimize='-O', gccversion=2.7.2.3
  cppflags='-I/usr/local/include -I/opt/local/include -DREG_INFTY=22786'
  ccflags ='-I/usr/local/include -I/opt/local/include -DREG_INFTY=22786'
  stdchar='unsigned char', d_stdstdio=define, usevfork=false
  intsize=4, longsize=4, ptrsize=4, doublesize=8
  d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
  alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries​:
  ld='gcc', ldflags =' -L/usr/local/lib -L/opt/local/lib'
  libpth=/usr/local/lib /opt/local/lib /lib /usr/lib /usr/ccs/lib
  libs=-lsocket -lnsl -ldl -lm -lc -lcrypt
  libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
  cccdlflags='-fpic', lddlflags='-G -L/usr/local/lib -L/opt/local/lib'

Characteristics of this binary (from libperl)​:
  Locally applied patches​:
  debug.list.file
  kill.0
  list.assign
  perlfaq8.typo
  Built under solaris
  Compiled at Oct 13 1999 04​:22​:25
  @​INC​:
  /home/mjtg/perl5.005_03/lib
  /home/mjtg/perl5.005_03/lib
  /home/mjtg/perl5.005_03/lib
  .

@p5pRT
Copy link
Author

p5pRT commented Jan 22, 2000

From [Unknown Contact. See original ticket]

On Sat, Jan 22, 2000 at 02​:25​:00PM +0000, M.J.T. Guy wrote​:

if \(my $x = 1\, 0\) \{
    my $x = 2;
\} else \{
    my $x = 3;
\}

produces spurious warnings

"my" variable $x masks earlier declaration in same scope at - line 2.
"my" variable $x masks earlier declaration in same scope at - line 4.

The only reason why I would use if (my $x = 1, 0) is precisely to get it
scoped like perl seems to do here. That is​: I expect to be able to see
$x in both the "if" and the "else" branch, but not outside of them.

--
The idea is that the first face shown to people is one they can readily
accept - a more traditional logo. The lunacy element is only revealed
subsequently, via the LunaDude. [excerpted from the Lunatech Identity Manual]

@p5pRT
Copy link
Author

p5pRT commented Jan 22, 2000

From @TimToady

Bart Schuller writes​:
: On Sat, Jan 22, 2000 at 02​:25​:00PM +0000, M.J.T. Guy wrote​:
: > if (my $x = 1, 0) {
: > my $x = 2;
: > } else {
: > my $x = 3;
: > }
: >
: > produces spurious warnings
: >
: > "my" variable $x masks earlier declaration in same scope at - line 2.
: > "my" variable $x masks earlier declaration in same scope at - line 4.
:
: The only reason why I would use if (my $x = 1, 0) is precisely to get it
: scoped like perl seems to do here. That is​: I expect to be able to see
: $x in both the "if" and the "else" branch, but not outside of them.

I think the point of it is that the first declaration is within an
implicit block around everything, and therefore the inner blocks are
not in fact the "same scope", so the warnings are spurious. It should
be equivalent to saying​:

  { my $x;
  if ($x = 1, 0) {
  my $x = 2;
  } else {
  my $x = 3;
  }
  }

and it isn't.

That being said, I'm not sure we should remove the warning, since the
outer block is implicit, and the presence of such a redeclaration is in
all likelihood an error. I imagine it was an error that caused the
problem to be noticed in the first place, no?

But the message is still slightly false.

Larry

@p5pRT
Copy link
Author

p5pRT commented Jan 22, 2000

From @gsar

On Sat, 22 Jan 2000 10​:14​:05 PST, Larry Wall wrote​:

That being said, I'm not sure we should remove the warning, since the
outer block is implicit, and the presence of such a redeclaration is in
all likelihood an error. I imagine it was an error that caused the
problem to be noticed in the first place, no?

But the message is still slightly false.

I recall my original reasoning was that people wouldn't be declaring a
lexical in the conditional if they didn't expect to use the same
lexical in both branches of the conditional/loop. But I guess
we should allow for people doing things such as​:

  while (my $x = foo()) {
  my $x = bar($x);
  }
  continue {
  print $x;
  }

and the like.

So here's a patch.

Sarathy
gsar@​ActiveState.com

Inline Patch
-----------------------------------8<-----------------------------------
Change 4847 by gsar@auger on 2000/01/23 04:47:25

	don't warn about masked lexical in C<if (my $x = 1) { my $x; }>,
	C<while (my $x = foo()) { my $x = bar(); }> etc.

Affected files ...

... //depot/perl/op.c#241 edit

Differences ...

==== //depot/perl/op.c#241 (text) ====
Index: perl/op.c
--- perl/op.c.~1~	Sat Jan 22 20:56:09 2000
+++ perl/op.c	Sat Jan 22 20:56:09 2000
@@ -2008,12 +2008,11 @@
     int retval = PL_savestack_ix;
 
     SAVEI32(PL_comppad_name_floor);
-    if (full) {
-	if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
-	    PL_comppad_name_floor = PL_comppad_name_fill;
-	else
-	    PL_comppad_name_floor = 0;
-    }
+    PL_comppad_name_floor = AvFILLp(PL_comppad_name);
+    if (full)
+	PL_comppad_name_fill = PL_comppad_name_floor;
+    if (PL_comppad_name_floor < 0)
+	PL_comppad_name_floor = 0;
     SAVEI32(PL_min_intro_pending);
     SAVEI32(PL_max_intro_pending);
     PL_min_intro_pending = 0;
@@ -2028,8 +2027,6 @@
         PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
         SAVEFREESV(PL_compiling.cop_warnings) ;
     }
-
-
     return retval;
 }
 
End of Patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant