Opened 14 years ago

Last modified 14 years ago

#1522 closed defect (fixed)

Optimization of default function argument values causes incorrect results when a value is given

Reported by: Pavol Privitzer Owned by: Pavol Privitzer
Priority: high Milestone:
Component: Version:
Keywords: Cc: Pavol Privitzer, Per Östlund

Description

For model:

model bugFun
  function myFun
    input Real[1, :] x = [0,1]; 
    output Real y;
  protected
    Real[1, size(x,2)] locX;
    Integer index;
  algorithm
    index :=1;
    while (index <= size(x,2)) loop
      locX[1,index] := x[1,index];
      index := index + 1;
    end while;
          
    y := locX[1,size(x,2)];

  end myFun;
    
  Real res1;
  Real res2;
equation
  res1 = myFun( {{1,2,3}});
  res2 = myFun( {{1,2,3,4,5}});
end bugFun;

are expected values

  res1 = 3 
  res2 = 5

but both are 2
The problem is an agressive optimization where the

  y := locX[1,size(x,2)];

is replaced (in generated code) by

  y := locX[1,2];

because of the default value of x[] ...
When one removes the default value of x, everything works fine ...

Change History (2)

comment:1 by Per Östlund, 14 years ago

Added code tags.

comment:2 by Per Östlund, 14 years ago

Fixed in revision 9005, see test case mofiles/FunctionEval12. size of any array in a function is now considered to be a variable expression, which is not always correct. This might lead to less efficient code, but since this problem would require a lot of work to fix correctly we'll go with correct over efficient for now.

Note: See TracTickets for help on using tickets.