Opened 12 years ago

Closed 12 years ago

#1833 closed defect (fixed)

Local name shadowing and assignment to inputs

Reported by: adabe588@… Owned by: openmodelicadevelopers@…
Priority: high Milestone: 1.9.0
Component: Frontend Version: trunk
Keywords: Cc:

Description

When calling this test function no warning is given that x and y don't have the correct types. Are the input variables used in the match instead? The program exits silently.

package P

function test
  input Integer x;
  input String y;
algorithm
  _ := match (x,y)
    local String x; Integer y;
    case (x,y) then ();
  end match;
end test;

end P;

Change History (8)

comment:1 by Martin Sjölund, 12 years ago

Component: BackendFrontend
Owner: changed from somebody to openmodelicadevelopers@…
Status: newassigned

If noone objects in the next x minutes (where x is the time it will take me to fix this), I will remove the possibility to match against input arguments (assignment to input is not valid Modelica) and consider also making name shadowing an error in MetaModelica (duplicate elements are disallowed in Modelica anyway).

comment:2 by adabe588@…, 12 years ago

Sounds very good to me!

comment:3 by Martin Sjölund, 12 years ago

How to detect these things:

Index: Compiler/FrontEnd/Static.mo
===================================================================
--- Compiler/FrontEnd/Static.mo	(revision 13057)
+++ Compiler/FrontEnd/Static.mo	(working copy)
@@ -327,7 +327,7 @@
         Error.addSourceMessage(Error.ASSIGN_READONLY_ERROR,
           {"input", cr_str}, inInfo);
       then
-        fail();
+        ();
 
     else ();
 
Index: Compiler/FrontEnd/Patternm.mo
===================================================================
--- Compiler/FrontEnd/Patternm.mo	(revision 13056)
+++ Compiler/FrontEnd/Patternm.mo	(working copy)
@@ -162,7 +162,7 @@
   output Env.Cache outCache;
   output DAE.Pattern pattern;
 algorithm
-  (outCache,pattern) := elabPattern2(cache,env,lhs,ty,info,Error.getNumErrorMessages(),allowTopLevelInputs);
+  (outCache,pattern) := elabPattern2(cache,env,lhs,ty,info,Error.getNumErrorMessages(),false);
 end elabPattern;
 
 protected function elabPattern2
omc MainTest.mos | grep input | sort > log && wc -l log

comment:4 by Martin Sjölund, 12 years ago

The following script removed 98% of all occurrences of matching to an input variable:

#!/bin/bash

while read line; do
  FILE=`echo $line | cut -d: -f1 | cut "-d[" -f2`
  LINE1=`echo $line | cut -d: -f2 | cut -d: -f3`
  LINE2=`echo $line | cut -d: -f3 | cut -d: -f4 | cut -d- -f2`
  PAT=`echo $line | cut "-d " -f9`
  for LINE in `seq $LINE1 $LINE2`; do
    echo "Processing $FILE $LINE using $PAT"
    if sed -n "$LINE"p "$FILE" | egrep -q "equation|then|else"; then
      sed -i "$LINE""s/case $PAT/case _/g" "$FILE"
      sed -i "$LINE""s/case *($PAT)/case _/g" "$FILE"
      sed -i "$LINE""s/case *($PAT,/case (_,/g" "$FILE"
      sed -i "$LINE""s/ *$PAT *then/ _ then/g" "$FILE"
      sed -i "$LINE""s/ *, *$PAT) *then/,_) then/g" "$FILE"
      sed -i "$LINE""s/ *$PAT *equation/ _ equation/g" "$FILE"
      sed -i "$LINE""s/ *, *$PAT) *equation/,_) equation/g" "$FILE"
      sed -i "$LINE""s/_ *, *$PAT as */_,/g" "$FILE"
      sed -i "$LINE""s/_ *, *$PAT/_,_/g" "$FILE"
      sed -i "$LINE""s/  *$PAT *, *_/_,_/g" "$FILE"
      sed -i "$LINE""s/,$PAT,_/,_,_/g" "$FILE"
      sed -i "$LINE""s/,$PAT, _/,_,_/g" "$FILE"
      sed -i "$LINE""s/_,$PAT,/_,_,/g" "$FILE"
      sed -i "$LINE""s/$PAT as //g" "$FILE"
    else
      sed -i "$LINE""s/,$PAT,/,_,/g" "$FILE"
      sed -i "$LINE""s/, *$PAT *,/, _,/g" "$FILE"
      sed -i "$LINE""s/($PAT,/(_,/g" "$FILE"
      sed -i "$LINE""s/( $PAT,/(_,/g" "$FILE"
      sed -i "$LINE""s/,$PAT)/,_)/g" "$FILE"
      sed -i "$LINE""s/, *$PAT)/, _)/g" "$FILE"
      sed -i "$LINE""s/   $PAT)/   _)/g" "$FILE"
      sed -i "$LINE""s/   $PAT,/   _,/g" "$FILE"
      sed -i "$LINE""s/ ${PAT}\$/ _/g" "$FILE"
      sed -i "$LINE""s/case($PAT)/case _/g" "$FILE"
      sed -i "$LINE""s/($PAT)/_/g" "$FILE"
      sed -i "$LINE""s/$PAT  *as //g" "$FILE"
    fi
  done
done < log

comment:5 by Martin Sjölund, 12 years ago

This will be the testcase for the original bug-report (it tests what was actually wrong in a better way):

model CheckInputScope

package P

function test
  input Integer x;
algorithm
  _ := match (x,y)
    local Integer y;
    case (x,y) then ();
  end match;
end test;

end P;

algorithm
  P.test(1);
end CheckInputScope;

comment:6 by Martin Sjölund, 12 years ago

This is detected in r13060, now to also detect the shadowing elements

comment:7 by Martin Sjölund, 12 years ago

The reason the duplicates weren't detected was that the check came after unused local declarations were removed

comment:8 by Martin Sjölund, 12 years ago

Resolution: fixed
Status: assignedclosed
Summary: Warning for local name shadowing isn't always triggeredLocal name shadowing and assignment to inputs

Fixed in r13062

Note: See TracTickets for help on using tickets.