-
Notifications
You must be signed in to change notification settings - Fork 69
fix: allow mixing of implicit and explicit arguments #1698
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
Open
mhasel
wants to merge
6
commits into
master
Choose a base branch
from
mihr/mix-implicit-explicit-args
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d2baa1a
fix: allow mixing of implicit and explicit arguments
mhasel db4929c
Merge branch 'master' into mihr/mix-implicit-explicit-args
mhasel 564da0b
add comments
mhasel 016d6de
fmt
mhasel dc4d60e
Merge branch 'master' into mihr/mix-implicit-explicit-args
mhasel c6ea0da
update error description
mhasel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
compiler/plc_diagnostics/src/diagnostics/error_codes/E131.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Positional argument collides with later named argument | ||
|
|
||
| In a mixed implicit/explicit call, a positional argument would naturally fill the same | ||
| parameter that a *later* named argument also targets. The positional would spill into | ||
| the next free slot - a silent reinterpretation that is almost never what the caller | ||
| intended. This is an error because no legitimate code should depend on that | ||
| disambiguation. | ||
|
|
||
| ```iecst | ||
| FUNCTION myfunc : INT | ||
| VAR_INPUT | ||
| a : INT; | ||
| b : INT; | ||
| END_VAR | ||
| END_FUNCTION | ||
|
|
||
| PROGRAM main | ||
| VAR x : INT; END_VAR | ||
| // `1` sits in position 0 (param `a`), but `a := 10` also targets `a`. | ||
| x := myfunc(1, a := 10); | ||
| END_PROGRAM | ||
| ``` | ||
|
|
||
| Rewrite the call so the intent is unambiguous - either name both arguments or drop the | ||
| duplicate name. |
40 changes: 40 additions & 0 deletions
40
compiler/plc_diagnostics/src/diagnostics/error_codes/E132.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Mixing implicit and explicit call parameters | ||
|
|
||
| A call mixes positional (implicit) arguments with named (explicit) arguments. | ||
| The compiler accepts this, but the resolution is order-sensitive: named args | ||
| first claim their slots, and any remaining positional args fill the leftover | ||
| slots left-to-right. This is easy to misread and easy to break by reordering. | ||
|
|
||
| Example: | ||
|
|
||
| ```st | ||
| FUNCTION myfunc : INT | ||
| VAR_INPUT | ||
| a : INT; | ||
| b : INT; | ||
| c : INT; | ||
| END_VAR | ||
| END_FUNCTION | ||
|
|
||
| PROGRAM main | ||
| VAR x : INT; END_VAR | ||
| // `b := 20` claims slot 1; positional `1` fills slot 0 (a), `3` fills slot 2 (c). | ||
| x := myfunc(b := 20, 1, 3); | ||
| END_PROGRAM | ||
| ``` | ||
|
|
||
| Prefer one style per call - fully positional or fully named - so a reader can | ||
| see the mapping at a glance. If a mix is genuinely the clearest option (e.g. to | ||
| override one default in the middle of a long parameter list), name every | ||
| argument whose position isn't obvious. | ||
|
|
||
| To silence this warning, rewrite the call with a single convention: | ||
|
|
||
| ```st | ||
| x := myfunc(1, 20, 3); // all positional | ||
| x := myfunc(a := 1, b := 20, c := 3); // all named | ||
| ``` | ||
|
|
||
| See also `E131` — positional arg collides with later named arg — which catches | ||
| the more dangerous case where a positional argument's natural slot is also | ||
| named, and the resolver silently shifts it to a different parameter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idle thought, we can make our life much easier if the call lowerer would solve this:
But we need to make sure we still hit the validation.
not saying you should do it now, just thought it might make it easier