-
Notifications
You must be signed in to change notification settings - Fork 52
Laurel: field access on Any-typed values #1421
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
julesmt
wants to merge
2
commits into
reviewed-kbd-will-merge-to-main
Choose a base branch
from
laurel/any-composite-bridge
base: reviewed-kbd-will-merge-to-main
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
2 commits
Select commit
Hold shift + click to select a range
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
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
115 changes: 115 additions & 0 deletions
115
StrataTest/Languages/Laurel/AnyBridgeFieldResolutionTest.lean
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,115 @@ | ||
| /- | ||
| Copyright Strata Contributors | ||
| SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| -/ | ||
|
|
||
| /- | ||
| Regression tests for `Any`-typed field resolution: the cross-scope fallback in | ||
| `resolveFieldRef` fires only for `Any` targets, so a concrete target can't bind | ||
| an unrelated composite's field. | ||
| -/ | ||
|
|
||
| import StrataTest.Util.TestLaurel | ||
|
|
||
| open StrataTest.Util | ||
| open Strata | ||
|
|
||
| /-! ## `meow` (on `Cat`) is not a field of `Dog`, so `d#meow` is rejected -/ | ||
|
|
||
| #eval testLaurelResolution <| | ||
| #strata | ||
| program Laurel; | ||
| composite Dog { var bark: int } | ||
| composite Cat { var meow: int } | ||
| procedure test() opaque { | ||
| var d: Dog := new Dog; | ||
| var x: int := d#meow | ||
| // ^^^^ error: Resolution failed: 'meow' is not defined | ||
| }; | ||
| #end | ||
|
|
||
| /-! ## Correct field access on the same composites still resolves -/ | ||
|
|
||
| #eval testLaurelResolution <| | ||
| #strata | ||
| program Laurel; | ||
| composite Dog { var bark: int } | ||
| composite Cat { var meow: int } | ||
| procedure test() opaque { | ||
| var d: Dog := new Dog; | ||
| var x: int := d#bark | ||
| }; | ||
| #end | ||
|
|
||
| /-! ## An `Any`-typed target resolves a field cross-scope (gate TRUE branch) -/ | ||
|
|
||
| #eval testLaurelResolution <| | ||
| #strata | ||
| program Laurel; | ||
| datatype Any { from_Reference(as_ref: int) } | ||
| composite Dog { var bark: int } | ||
| procedure test() opaque { | ||
| var a: Any := from_Reference(0); | ||
| var x: int := a#bark | ||
| }; | ||
| #end | ||
|
|
||
| /-! ## An `Any`-typed target accessing a field defined on no composite still errors -/ | ||
|
|
||
| #eval testLaurelResolution <| | ||
| #strata | ||
| program Laurel; | ||
| datatype Any { from_Reference(as_ref: int) } | ||
| composite Dog { var bark: int } | ||
| procedure test() opaque { | ||
| var a: Any := from_Reference(0); | ||
| var x: int := a#nope | ||
| // ^^^^ error: Resolution failed: 'nope' is not defined | ||
| }; | ||
| #end | ||
|
|
||
| /-! ## A field write/read round-trip verifies -/ | ||
|
|
||
| #eval testLaurel <| | ||
| #strata | ||
| program Laurel; | ||
| composite Dog { var bark: int } | ||
| procedure test() opaque { | ||
| var d: Dog := new Dog; | ||
| d#bark := 7; | ||
| assert d#bark == 7; | ||
| d#bark := d#bark + 1; | ||
| assert d#bark == 8 | ||
| }; | ||
| #end | ||
|
|
||
| /-! ## Two instances have independent fields (no aliasing collapse) -/ | ||
|
|
||
| #eval testLaurel <| | ||
| #strata | ||
| program Laurel; | ||
| composite Dog { var bark: int } | ||
| procedure test() opaque { | ||
| var d1: Dog := new Dog; | ||
| var d2: Dog := new Dog; | ||
| d1#bark := 1; | ||
| d2#bark := 2; | ||
| assert d1#bark == 1; | ||
| assert d2#bark == 2 | ||
| }; | ||
| #end | ||
|
|
||
| /-! ## A false field assertion is caught by the verifier -/ | ||
|
|
||
| #eval testLaurel <| | ||
| #strata | ||
| program Laurel; | ||
| composite Dog { var bark: int } | ||
| procedure test() opaque { | ||
| var d: Dog := new Dog; | ||
| d#bark := 1; | ||
| assert d#bark == 2 | ||
| //^^^^^^^^^^^^^^^^^^ error: assertion could not be proved | ||
| }; | ||
| #end |
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.
anyis not a Laurel concept. It should really be renamed toPythonDynamicValue. We shouldn't refer to it from any Laurel code.The correct way to get field reads/writes on Any typed objects is to introduce a wrapper method around field read/write that unpacks the Any to get a composite out of it.