v3: keep same-named types from using str methods of other modules - #28936
Merged
Merged
Conversation
Importing both `toml` and `x.json2` failed to compile: `toml.Any.string()` stringified its `[]Any` variant through `json2.[]Any.str`, emitting a copy of `[]toml.Any` into `[]json2.Any` that the C compiler rejected. Method lookup falls back to `receiver_method_suffix_index`, which is keyed by short receiver spellings (`[]Any.str`, `Any.str`). When only one module declares such a method, any same-named type from another module resolved to it. Reject suffix-index hits whose declaring module differs from the module of the receiver's struct, sum type or enum (arrays and maps use their element/value type), in the checker and the transformer. Aliases, interfaces and structs with embedded fields keep the old lookup, since they can inherit methods from other modules. The transformer also qualifies bare receiver types declared in the current module before asking the checker.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A program importing both
tomlandx.json2failed to compile with V3:Root cause:
toml.Any.string()falls back toa.str(), which stringifies the[]Anyvariant. Method lookup then consultsreceiver_method_suffix_index, which is keyed by short receiver spellings ([]Any.str,Any.str). Onlyjson2declares such methods, so[]toml.Anyresolved tojson2.[]Any.strand the transformer emitted an element-copy conversion from[]toml.Anyto[]json2.Any. The same lookup also letbmod.Anypickamod.Any.str, and made an explicittoml_array.str()fail in the checker withcannot use receiver `[]toml.Any` as `[]json2.Any`.Fix:
types:suffix_indexed_method_fits_receiverrejects a suffix-index hit when it was declared in a different module from the receiver's struct, sum type or enum. Arrays and maps are checked by their element/value type. The check applies inconcrete_method_signature_keyand in the checker'sunique_receiver_method_suffix_matchcallers. Aliases, interfaces and structs with embedded fields keep the old lookup, because they can legitimately inherit methods declared in other modules (e.g. an embeddedveb.Context).transform:resolve_receiver_method_for_typeapplies the same check to its suffix match and to thedeclared_receiver_methodfallback. The fallback is only rejected when the index shows another module registered the method. Bare receiver types declared in the current module (Anyinsidetoml) are qualified before asking the checker, so they don't resolve through a stale checker module.Validation
vlib/v/tests/same_name_types_str_methods/:withstrandnostrmirrorjson2.Anyandtoml.Any. The test covers sum type, array, map, struct and enum stringification (.str(), interpolation, a module-side.str()fallback).toml_and_json2_test.vis the reported program../v -silent test vlib/v/transform/: 17/17 pass../v -silent test vlib/toml/ vlib/json2/: 106 passed, 1 skipped../v -silent test vlib/v/types/: 30/31 pass.checker_ownership_alias_test.vhangs in the-d ownershipprescan on master too, in other checkouts as well../v -silent vlib/v/compiler_errors_test.v: 1720 passed. The one failure,anon_struct_private_field_err, also fails on master../v -silent test vlib/v/tests/: 2285 passed, 13 failed. All 13 fail the same way on master:builtin_overflow_test,check_in_is_consistency_test,enum_bitfield_test,enum_from_generic_static_method_test,failing_tests_test,fn_call_mut_ref_args_test,fn_with_opt_or_res_of_multi_return_test,for_in_containers_of_fixed_array_test,for_in_ref_arr_test,struct_aligned_test,struct_heap_large_fixed_array_test,vls/goto_def_test,vls/autocomplete_module_test.