From e6c324c2e70288ca0df86efd0957ef2c5b66f39c Mon Sep 17 00:00:00 2001 From: dos-reis Date: Sun, 13 Jun 2010 01:33:35 +0000 Subject: * interp/compiler.boot (complainIfShadowing): New. (compStepIterator): Use it to warn about loop variable shadowing declaration in enclosing scope. (compIterator): Likewise. --- src/ChangeLog | 7 +++++++ src/interp/compiler.boot | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChangeLog b/src/ChangeLog index 99d8b460..e9dd5d4b 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2010-06-12 Gabriel Dos Reis + + * interp/compiler.boot (complainIfShadowing): New. + (compStepIterator): Use it to warn about loop variable shadowing + declaration in enclosing scope. + (compIterator): Likewise. + 2010-06-12 Gabriel Dos Reis * algebra/kl.spad.pamphlet (Kernel): Don't clear the on diff --git a/src/interp/compiler.boot b/src/interp/compiler.boot index 87d07c89..ceae230f 100644 --- a/src/interp/compiler.boot +++ b/src/interp/compiler.boot @@ -2362,12 +2362,19 @@ compIntegerValue(x,e) == comp(x,$PositiveInteger,e) or comp(x,$NonNegativeInteger,e) or compOrCroak(x,$Integer,e) - + +++ Issue a diagnostic if `x' names a loop variable with a matching +++ declaration or definition in the enclosing scope. +complainIfShadowing(x,e) == + if getmode(x,e) ~= nil then + stackWarning('"loop variable %1b shadows variable from enclosing scope",[x]) + ++ Attempt to compile a `for' iterator of the form ++ for index in start..final by inc ++ where the bound `final' may be missing. compStepIterator(index,start,final,inc,e) == checkVariableName index + complainIfShadowing(index,e) $formalArgList := [index,:$formalArgList] [start,startMode,e] := compIntegerValue(start,e) or return stackMessage('"start value of index: %1b must be an integer",[start]) @@ -2389,6 +2396,7 @@ compIterator(it,e) == -- ??? Allow for declared iterator variable. it is ["IN",x,y] => checkVariableName x + complainIfShadowing(x,e) --these two lines must be in this order, to get "for f in list f" --to give an error message if f is undefined [y',m,e]:= comp(y,$EmptyMode,e) or return nil @@ -2403,6 +2411,7 @@ compIterator(it,e) == [["IN",x,y''],e] it is ["ON",x,y] => checkVariableName x + complainIfShadowing(x,e) $formalArgList:= [x,:$formalArgList] [y',m,e]:= comp(y,$EmptyMode,e) or return nil [mOver,mUnder]:= -- cgit v1.2.3