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

Name clash in star imports should result in error when the name is used #5963

Merged
merged 35 commits into from May 11, 2024

Conversation

jjcnn
Copy link
Contributor

@jjcnn jjcnn commented May 6, 2024

Description

Fixes #5940.

This PR deals with the situation where the same name is imported by multiple star imports, e.g.,

// a.sw
struct X = ...

// b.sw
struct X = ...

// main.sw
use a::*;
use b::*;

So far we have resolved this name clash by letting the latter import shadow the former, which is incorrect.

The correct behavior is to allow the import without error, but to report an error if the unqualified name (i.e., X) is used (qualified names a::X and b::X are legal)`. This PR fixes this problem.

Note that if X is imported multiple times, but each time is bound to the same entity (e.g., because it is imported both through core:;prelude and core::codec), then this should not cause an error.

Note also that there is a problem with the implicit imports from the core and std preludes, which in some cases causes the implementation to think that a name is bound to multiple different entities, even though the entities are actually the same. As a workaround anytime a name is imported from either std or core when it is already imported from std or core, the new binding replaces the old one instead of being added as a new binding.

Unfortunately this workaround means that it is not currently possible to redefine and use a name that exists in std or core, e.g., to create a specialized version of assert_eq as is done in one of our tests. To use such a redefinition one must use a qualified name for the entity, which seems like an acceptable workaround. (Note that this only applies if the redefined entity is imported using a star import - if imported using an item import (use a::X;), then there is no issue).

Once #3487 is implemented this workaround should no longer be necessary.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@jjcnn jjcnn changed the title Jjcnn/name clash on star imports Name clash in star imports should result in error when the name is used May 6, 2024
Copy link

github-actions bot commented May 6, 2024

Benchmark for c1e6d19

Click to view benchmark
Test Base PR %
code_action 5.4±0.17ms 5.5±0.05ms +1.85%
code_lens 288.3±7.10ns 330.2±9.12ns +14.53%
compile 6.9±0.10s 6.9±0.10s 0.00%
completion 4.8±0.07ms 5.0±0.07ms +4.17%
did_change_with_caching 6.2±0.07s 6.4±0.09s +3.23%
document_symbol 981.8±16.61µs 940.0±19.48µs -4.26%
format 89.0±0.90ms 87.7±1.57ms -1.46%
goto_definition 349.0±5.93µs 354.0±8.95µs +1.43%
highlight 8.8±0.04ms 9.1±0.26ms +3.41%
hover 591.3±6.84µs 599.1±21.20µs +1.32%
idents_at_position 119.7±0.59µs 122.9±1.22µs +2.67%
inlay_hints 649.4±14.43µs 665.1±18.14µs +2.42%
on_enter 494.3±34.88ns 481.5±15.18ns -2.59%
parent_decl_at_position 3.6±0.07ms 3.7±0.06ms +2.78%
prepare_rename 352.7±5.46µs 352.9±6.14µs +0.06%
rename 9.3±0.10ms 9.7±0.12ms +4.30%
semantic_tokens 1052.6±19.50µs 1059.6±27.19µs +0.67%
token_at_position 342.6±1.56µs 416.4±1.96µs +21.54%
tokens_at_position 3.6±0.04ms 3.7±0.03ms +2.78%
tokens_for_file 418.4±3.70µs 415.5±2.45µs -0.69%
traverse 51.3±3.03ms 51.6±1.30ms +0.58%

Copy link

github-actions bot commented May 7, 2024

Benchmark for f0511dd

Click to view benchmark
Test Base PR %
code_action 5.6±0.08ms 5.3±0.05ms -5.36%
code_lens 287.3±6.69ns 299.0±7.52ns +4.07%
compile 6.7±0.06s 6.8±0.04s +1.49%
completion 5.0±0.14ms 4.9±0.20ms -2.00%
did_change_with_caching 6.4±0.11s 6.3±0.10s -1.56%
document_symbol 1013.1±39.18µs 945.0±22.95µs -6.72%
format 88.9±1.27ms 87.6±1.19ms -1.46%
goto_definition 348.7±10.44µs 350.9±6.77µs +0.63%
highlight 9.0±0.17ms 8.8±0.15ms -2.22%
hover 581.3±8.94µs 590.5±13.44µs +1.58%
idents_at_position 119.9±1.02µs 122.2±1.70µs +1.92%
inlay_hints 648.8±23.07µs 641.7±16.75µs -1.09%
on_enter 474.5±10.27ns 484.3±15.61ns +2.07%
parent_decl_at_position 3.7±0.04ms 3.6±0.06ms -2.70%
prepare_rename 349.6±6.61µs 353.7±15.48µs +1.17%
rename 9.7±0.41ms 9.4±0.32ms -3.09%
semantic_tokens 1064.4±21.32µs 1043.8±17.00µs -1.94%
token_at_position 344.8±3.16µs 353.3±4.92µs +2.47%
tokens_at_position 3.7±0.03ms 3.6±0.08ms -2.70%
tokens_for_file 418.5±4.32µs 418.3±1.94µs -0.05%
traverse 51.9±1.56ms 50.1±1.10ms -3.47%

Copy link

github-actions bot commented May 7, 2024

Benchmark for 156a15b

Click to view benchmark
Test Base PR %
code_action 5.5±0.09ms 5.3±0.10ms -3.64%
code_lens 292.3±11.23ns 297.2±7.42ns +1.68%
compile 6.6±0.08s 6.6±0.08s 0.00%
completion 4.9±0.06ms 4.8±0.02ms -2.04%
did_change_with_caching 6.0±0.05s 6.1±0.04s +1.67%
document_symbol 1013.5±12.05µs 997.7±28.03µs -1.56%
format 88.2±1.26ms 88.2±0.82ms 0.00%
goto_definition 353.9±5.83µs 354.8±4.70µs +0.25%
highlight 9.0±0.17ms 8.7±0.14ms -3.33%
hover 575.6±6.29µs 586.1±13.29µs +1.82%
idents_at_position 119.1±0.56µs 118.8±0.26µs -0.25%
inlay_hints 653.0±26.82µs 639.3±23.46µs -2.10%
on_enter 481.0±11.99ns 483.1±12.28ns +0.44%
parent_decl_at_position 3.7±0.04ms 3.6±0.03ms -2.70%
prepare_rename 354.7±6.32µs 356.2±6.40µs +0.42%
rename 9.5±0.18ms 9.3±0.21ms -2.11%
semantic_tokens 1042.1±22.36µs 1054.4±24.99µs +1.18%
token_at_position 342.3±2.15µs 338.9±2.63µs -0.99%
tokens_at_position 3.7±0.04ms 3.6±0.02ms -2.70%
tokens_for_file 414.8±2.01µs 412.0±2.36µs -0.68%
traverse 50.0±2.11ms 49.3±1.17ms -1.40%

Copy link

github-actions bot commented May 7, 2024

Benchmark for 3d7f425

Click to view benchmark
Test Base PR %
code_action 5.5±0.10ms 5.5±0.12ms 0.00%
code_lens 288.7±9.15ns 340.8±13.68ns +18.05%
compile 6.7±0.07s 6.8±0.13s +1.49%
completion 4.9±0.11ms 4.9±0.03ms 0.00%
did_change_with_caching 6.2±0.05s 6.2±0.05s 0.00%
document_symbol 1030.3±41.08µs 933.2±15.04µs -9.42%
format 91.6±0.73ms 87.7±1.03ms -4.26%
goto_definition 351.3±7.13µs 348.7±5.82µs -0.74%
highlight 9.0±0.19ms 9.0±0.05ms 0.00%
hover 587.2±10.79µs 589.1±11.55µs +0.32%
idents_at_position 119.5±0.35µs 118.7±0.66µs -0.67%
inlay_hints 648.8±20.44µs 653.7±30.69µs +0.76%
on_enter 476.3±9.82ns 487.9±15.55ns +2.44%
parent_decl_at_position 3.7±0.02ms 3.7±0.03ms 0.00%
prepare_rename 348.2±6.35µs 346.8±3.62µs -0.40%
rename 9.5±0.04ms 9.6±0.13ms +1.05%
semantic_tokens 1045.1±19.05µs 1027.8±18.05µs -1.66%
token_at_position 339.3±3.26µs 340.8±3.24µs +0.44%
tokens_at_position 3.7±0.03ms 3.7±0.01ms 0.00%
tokens_for_file 422.5±2.27µs 416.0±4.29µs -1.54%
traverse 50.4±1.81ms 51.7±2.34ms +2.58%

Copy link

github-actions bot commented May 8, 2024

Benchmark for a345357

Click to view benchmark
Test Base PR %
code_action 5.9±0.20ms 5.4±0.06ms -8.47%
code_lens 300.8±9.49ns 338.5±14.76ns +12.53%
compile 7.2±0.06s 7.1±0.10s -1.39%
completion 5.4±0.14ms 5.0±0.13ms -7.41%
did_change_with_caching 6.5±0.10s 6.5±0.10s 0.00%
document_symbol 958.9±22.80µs 1034.7±17.24µs +7.90%
format 93.8±1.73ms 89.5±1.16ms -4.58%
goto_definition 360.9±7.66µs 353.0±7.07µs -2.19%
highlight 9.2±0.27ms 8.9±0.12ms -3.26%
hover 594.5±11.45µs 689.0±6.89µs +15.90%
idents_at_position 120.4±0.68µs 119.5±1.23µs -0.75%
inlay_hints 660.0±13.15µs 651.4±32.46µs -1.30%
on_enter 476.4±15.99ns 502.5±14.62ns +5.48%
parent_decl_at_position 3.8±0.07ms 3.6±0.05ms -5.26%
prepare_rename 361.2±7.37µs 355.7±8.34µs -1.52%
rename 10.0±0.18ms 9.4±0.41ms -6.00%
semantic_tokens 1079.1±19.50µs 1058.8±12.38µs -1.88%
token_at_position 353.9±3.71µs 346.3±2.62µs -2.15%
tokens_at_position 3.7±0.05ms 3.6±0.03ms -2.70%
tokens_for_file 423.7±2.60µs 418.3±2.71µs -1.27%
traverse 53.0±1.73ms 54.0±2.29ms +1.89%

@tritao
Copy link
Contributor

tritao commented May 9, 2024

Left a couple suggestions, but overall really nice improvements here @jjcnn, great stuff.

@jjcnn
Copy link
Contributor Author

jjcnn commented May 10, 2024

Also this is a breaking change and should be labeled as such.

Is it? I think of it as a bugfix.

In any case the change will not change the behavior of programs that successfully compile. Compilation will only fail some programs that used to compile without error.

@jjcnn jjcnn dismissed stale reviews from JoshuaBatty and IGI-111 via 48fb955 May 10, 2024 09:32
@jjcnn
Copy link
Contributor Author

jjcnn commented May 10, 2024

Review comments addressed.

There is one outstanding bug: When the name is bound to an enum variant, we report path_to_enum::variant_name as a valid path. The correct path is path_to_enum::enum_name::variant_name.

I'll fix this issue later today, and update the failing test accordingly.

Copy link

Benchmark for a188b6b

Click to view benchmark
Test Base PR %
code_action 5.2±0.08ms 5.2±0.05ms 0.00%
code_lens 286.0±3.42ns 294.4±7.77ns +2.94%
compile 3.0±0.05s 3.0±0.05s 0.00%
completion 4.6±0.08ms 4.6±0.02ms 0.00%
did_change_with_caching 2.9±0.04s 2.8±0.03s -3.45%
document_symbol 1017.3±49.62µs 1022.7±27.93µs +0.53%
format 90.7±1.24ms 90.5±1.16ms -0.22%
goto_definition 361.4±6.15µs 371.6±16.37µs +2.82%
highlight 8.7±0.19ms 8.7±0.13ms 0.00%
hover 484.5±3.70µs 488.8±5.21µs +0.89%
idents_at_position 122.7±1.06µs 123.1±0.89µs +0.33%
inlay_hints 672.7±23.58µs 658.3±20.19µs -2.14%
on_enter 487.7±3.66ns 505.2±13.06ns +3.59%
parent_decl_at_position 3.6±0.02ms 3.6±0.04ms 0.00%
prepare_rename 364.4±5.30µs 365.9±9.82µs +0.41%
rename 9.3±0.31ms 9.3±0.19ms 0.00%
semantic_tokens 1038.9±14.87µs 1036.2±26.02µs -0.26%
token_at_position 349.2±2.55µs 359.5±2.82µs +2.95%
tokens_at_position 3.6±0.08ms 3.6±0.03ms 0.00%
tokens_for_file 416.6±2.96µs 416.4±4.56µs -0.05%
traverse 41.5±0.88ms 41.6±0.87ms +0.24%

@jjcnn
Copy link
Contributor Author

jjcnn commented May 10, 2024

Final issues fixed.

tritao
tritao previously approved these changes May 10, 2024
Copy link

Benchmark for e16c46d

Click to view benchmark
Test Base PR %
code_action 5.3±0.04ms 5.4±0.02ms +1.89%
code_lens 296.3±6.94ns 335.5±8.40ns +13.23%
compile 3.0±0.04s 3.0±0.04s 0.00%
completion 4.7±0.07ms 4.7±0.05ms 0.00%
did_change_with_caching 2.9±0.04s 2.9±0.06s 0.00%
document_symbol 972.5±54.26µs 942.2±23.15µs -3.12%
format 72.0±0.91ms 72.4±1.55ms +0.56%
goto_definition 356.5±4.78µs 360.5±6.56µs +1.12%
highlight 9.0±0.16ms 9.1±0.11ms +1.11%
hover 472.4±6.01µs 481.8±6.46µs +1.99%
idents_at_position 122.2±0.27µs 122.5±0.64µs +0.25%
inlay_hints 658.3±8.58µs 656.4±11.11µs -0.29%
on_enter 503.6±16.87ns 484.2±8.30ns -3.85%
parent_decl_at_position 3.7±0.03ms 3.7±0.03ms 0.00%
prepare_rename 357.1±5.36µs 356.4±4.25µs -0.20%
rename 9.5±0.02ms 9.6±0.03ms +1.05%
semantic_tokens 1012.9±8.95µs 1024.0±14.13µs +1.10%
token_at_position 354.4±2.04µs 355.7±2.63µs +0.37%
tokens_at_position 3.7±0.28ms 3.7±0.05ms 0.00%
tokens_for_file 420.9±11.80µs 426.2±1.93µs +1.26%
traverse 41.6±1.68ms 41.3±0.70ms -0.72%

@IGI-111 IGI-111 enabled auto-merge (squash) May 11, 2024 21:42
@IGI-111 IGI-111 merged commit 5a56a58 into master May 11, 2024
35 checks passed
@IGI-111 IGI-111 deleted the jjcnn/name-clash-on-star-imports branch May 11, 2024 21:56
Copy link

Benchmark for ffeda73

Click to view benchmark
Test Base PR %
code_action 5.4±0.06ms 5.4±0.08ms 0.00%
code_lens 338.8±10.19ns 332.5±10.89ns -1.86%
compile 3.1±0.04s 3.0±0.06s -3.23%
completion 4.7±0.02ms 4.8±0.07ms +2.13%
did_change_with_caching 2.9±0.04s 2.9±0.04s 0.00%
document_symbol 945.9±18.51µs 949.3±20.67µs +0.36%
format 72.7±1.42ms 71.5±1.16ms -1.65%
goto_definition 369.0±4.09µs 369.3±11.79µs +0.08%
highlight 9.1±0.12ms 9.2±0.12ms +1.10%
hover 483.1±7.51µs 487.4±5.33µs +0.89%
idents_at_position 123.5±1.12µs 123.7±1.23µs +0.16%
inlay_hints 667.0±25.13µs 670.3±24.83µs +0.49%
on_enter 490.7±14.65ns 486.9±10.60ns -0.77%
parent_decl_at_position 3.7±0.02ms 3.8±0.08ms +2.70%
prepare_rename 359.6±6.87µs 366.5±6.74µs +1.92%
rename 9.7±0.11ms 9.7±0.14ms 0.00%
semantic_tokens 1046.8±23.64µs 1059.8±10.74µs +1.24%
token_at_position 354.4±3.32µs 360.4±3.08µs +1.69%
tokens_at_position 3.7±0.03ms 3.8±0.04ms +2.70%
tokens_for_file 435.4±2.40µs 433.7±2.09µs -0.39%
traverse 42.4±1.43ms 42.5±1.16ms +0.24%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking May cause existing user code to break. Requires a minor or major release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ambiguous star imports resolved incorrectly
4 participants