-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
transform: avoid field-name collisions for bound methods #28787
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
medvednikov
wants to merge
4
commits into
master
Choose a base branch
from
fix/28784-bound-method-field-collision
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
02f41a9
transform: avoid unrelated field types for known struct selectors
medvednikov b919416
transform: test receiver-scoped selector type inference
medvednikov cfd8f7f
tests: cover optional callbacks sharing bound method names
medvednikov ff12b02
transform: preserve nested promoted field types
medvednikov 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
73 changes: 73 additions & 0 deletions
73
vlib/v/tests/options/option_fn_bound_method_field_name_collision_test.v
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,73 @@ | ||
| struct Model {} | ||
|
|
||
| struct Process { | ||
| mut: | ||
| on_done ?fn () | ||
| } | ||
|
|
||
| fn (m Model) run(mut p Process) bool { | ||
| p.on_done = m.on_done | ||
| return p.do_work() | ||
| } | ||
|
|
||
| fn (m Model) on_done() {} | ||
|
|
||
| fn (p Process) do_work() bool { | ||
| if f := p.on_done { | ||
| f() | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| fn test_optional_void_callback_with_same_named_method() { | ||
| m := Model{} | ||
| mut p := Process{} | ||
| assert !p.do_work() | ||
| assert m.run(mut p) | ||
| assert p.do_work() | ||
| p.on_done = none | ||
| assert !p.do_work() | ||
| } | ||
|
|
||
| struct ValueModel { | ||
| value int | ||
| } | ||
|
|
||
| struct ValueProcess { | ||
| mut: | ||
| on_value ?fn () int | ||
| } | ||
|
|
||
| fn (m ValueModel) run(mut p ValueProcess) int { | ||
| p.on_value = m.on_value | ||
| return p.do_work() | ||
| } | ||
|
|
||
| fn (m ValueModel) on_value() int { | ||
| return m.value | ||
| } | ||
|
|
||
| fn (p ValueProcess) do_work() int { | ||
| if f := p.on_value { | ||
| return f() | ||
| } | ||
| return -1 | ||
| } | ||
|
|
||
| fn test_optional_callback_preserves_receiver_with_same_named_method() { | ||
| first := ValueModel{ | ||
| value: 42 | ||
| } | ||
| second := ValueModel{ | ||
| value: 17 | ||
| } | ||
| mut p := ValueProcess{} | ||
| assert p.do_work() == -1 | ||
| assert first.run(mut p) == 42 | ||
| assert p.do_work() == 42 | ||
| assert second.run(mut p) == 17 | ||
| assert p.do_work() == 17 | ||
| p.on_value = none | ||
| assert p.do_work() == -1 | ||
| } |
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,39 @@ | ||
| module transform | ||
|
|
||
| import v.flat | ||
|
|
||
| fn test_known_struct_selector_does_not_inherit_unrelated_field_type() { | ||
| for receiver_type in ['Model', '&Model', 'Unregistered'] { | ||
| mut a := flat.FlatAst.new() | ||
| receiver := a.add_val(.ident, 'm') | ||
| start := a.children.len | ||
| a.children << receiver | ||
| selector := a.add_node(flat.Node{ | ||
| kind: .selector | ||
| value: 'on_done' | ||
| children_start: start | ||
| children_count: 1 | ||
| }) | ||
| t := Transformer{ | ||
| a: &a | ||
| cur_module: 'main' | ||
| var_types: [ | ||
| VarTypeBinding{ | ||
| name: 'm' | ||
| typ: receiver_type | ||
| }, | ||
| ] | ||
| structs: { | ||
| 'Model': StructInfo{} | ||
| } | ||
| unique_fields: { | ||
| 'on_done': '?fn ()' | ||
| } | ||
| } | ||
| // Model has no on_done field: a method selector must not acquire the | ||
| // optional callback type belonging to another struct. Keep the fallback | ||
| // for receivers whose struct information is unavailable. | ||
| expected := if receiver_type == 'Unregistered' { '?fn ()' } else { '' } | ||
| assert t.resolve_selector_type(a.nodes[int(selector)]) == expected | ||
| } | ||
| } |
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.
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.
When a field is promoted through two or more embedded structs, this return incorrectly classifies it as a possible method and produces no type.
embedded_field_for_promoted_fieldinvlib/v/transform/struct.vchecks only whether the immediate embedded struct directly declares the field, so a valid selector such asBaz.xinvlib/v/tests/structs/nested_struct_embed_selector_test.v(Baz -> Bar -> Foo.x) reaches this branch; before this change, the unique-field fallback returnedint. Direct callers ofresolve_selector_type, including specialized array/loop inference paths, can consequently lose the field type when a checker annotation is unavailable, so the known-struct early return should occur only after a recursive promoted-field lookup.Useful? React with 👍 / 👎.
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.
Addressed in ff12b02.
Replaced the one-level promoted-field lookup with the existing recursive
struct_field_path_for_fieldhelper, then resolve the field type from the final owning struct. Pointer embeddings are unwrapped for the owner lookup, and the known-struct guard still prevents borrowing an unrelated same-named field's type.Added a checker-free regression covering eight combinations of value/pointer receivers, value/pointer embeddings, and scalar/array fields, plus nearer-field and receiver-field shadowing. The original method/field collision regression remains in place.
Verified that the committed diff matches the prepared two-file patch. V compilation, formatting, and runtime tests remain unrun locally: the sandbox has no V compiler, and bootstrap downloads fail DNS resolution. Leaving the thread open for re-review.