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

parameter hoisting changes program behaviour #1481

Open
zerbina opened this issue Dec 25, 2024 · 0 comments
Open

parameter hoisting changes program behaviour #1481

zerbina opened this issue Dec 25, 2024 · 0 comments
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler

Comments

@zerbina
Copy link
Collaborator

zerbina commented Dec 25, 2024

The parameter hoisting necessary for default parameter expressions referencing other parameters changes the evaluation order of the call arguments.

Example

proc a(x: int, y: int, z = y + 1) =
  discard

proc b(x: int, y: int, z = y + x) = # the order of usage is significant!
  discard

a((echo "a"; 1), (echo "b"; 2)) # 1
b((echo "c"; 1), (echo "d"; 2)) # 2

Actual Output

b
a
d
c

Expected Output

a
b
c
d

Additional Information

The underlying issue is with how the parameter hoisting commits the argument expression for the hoisted parameters to temporaries:

call(x = a, y = b, z = y + 1) # `y` needs to be hoisted

# is transformed into

let
  :tmp = b
call(a, :tmp, :tmp + 1)

If a has side-effects, the transformation changes the program's meaning (case number 1).

In case number 2, both parameter are hoisted, but in the order of in which they're used and not in the order in which the parameter are specified.

To preserve the meaning of the program, if a parameter is hoisted, all preceding parameters need to be hoisted too. In addition, parameters always need to be hoisted from left to right.

@zerbina zerbina added bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler labels Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

No branches or pull requests

1 participant