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

References to mutable values #5688

Merged
merged 12 commits into from
Mar 11, 2024
Merged

Conversation

ironcev
Copy link
Member

@ironcev ironcev commented Mar 4, 2024

Description

This PR implements references to mutable values, &mut T, as defined in references. The overall effort related to references is tracked in #5063.

References to mutable values:

  • can be created using &mut
  • can be used in type system wherever a type is expected
  • coerce to references (&mut T -> &T) and thus can be used wherever "regular" references are expected
  • can be passed to and returned from functions

The last point is a step in direction of replacing ref mut function parameters with either & or &mut.

References to mutable values cannot be taken on constants and immutable variables (see errors below). (Note that we slightly departure from the Rust counterparts here. Rust allows &mut on constants but issues a warning.)

Additionally, the PR implements Diagnostic for the ConflictingImplsForTraitAndType and shows the already existing, conflicting implementation of the trait.

Demo

For more demos, see the tests in this PR.

let mut x = 123u32;
let r_m_x: &mut u32 = &mut x;

fn fun(p: &mut T) -> &mut T { ... }

impl Foo for &mut &mut T { ... }

type RefMutRefMut = &mut &mut u64;

References to mutable values cannot reference constants

Trait is already implemented for type

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.

@ironcev ironcev self-assigned this Mar 4, 2024
@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged language feature Core language features visible to end users compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Mar 4, 2024
Copy link

github-actions bot commented Mar 4, 2024

Benchmark for 5a5cc93

Click to view benchmark
Test Base PR %
code_action 5.3±0.13ms 5.1±0.02ms -3.77%
code_lens 291.2±7.66ns 289.3±9.08ns -0.65%
compile 4.3±0.13s 4.3±0.08s 0.00%
completion 4.9±0.13ms 4.7±0.03ms -4.08%
did_change_with_caching 3.8±0.09s 3.9±0.15s +2.63%
document_symbol 998.1±43.80µs 952.6±23.94µs -4.56%
format 75.2±1.64ms 75.9±0.73ms +0.93%
goto_definition 368.7±6.69µs 364.7±5.66µs -1.08%
highlight 9.1±0.07ms 8.7±0.02ms -4.40%
hover 546.4±9.19µs 539.3±10.30µs -1.30%
idents_at_position 121.3±1.18µs 122.4±0.38µs +0.91%
inlay_hints 668.8±8.97µs 661.6±12.11µs -1.08%
on_enter 479.6±5.73ns 486.3±17.28ns +1.40%
parent_decl_at_position 3.7±0.03ms 3.6±0.03ms -2.70%
prepare_rename 364.4±6.19µs 368.3±17.63µs +1.07%
rename 9.5±0.16ms 9.1±0.22ms -4.21%
semantic_tokens 1061.7±19.11µs 1075.6±67.37µs +1.31%
token_at_position 357.2±1.50µs 365.2±5.43µs +2.24%
tokens_at_position 3.7±0.05ms 3.6±0.04ms -2.70%
tokens_for_file 412.4±1.96µs 423.0±6.24µs +2.57%
traverse 38.1±0.90ms 38.6±1.64ms +1.31%

Copy link

github-actions bot commented Mar 5, 2024

Benchmark for ac57c50

Click to view benchmark
Test Base PR %
code_action 5.2±0.02ms 5.2±0.02ms 0.00%
code_lens 289.6±7.53ns 289.0±5.10ns -0.21%
compile 4.3±0.08s 4.2±0.07s -2.33%
completion 4.8±0.02ms 4.7±0.15ms -2.08%
did_change_with_caching 3.7±0.10s 3.7±0.12s 0.00%
document_symbol 954.0±7.04µs 955.5±9.13µs +0.16%
format 73.2±1.16ms 74.1±0.86ms +1.23%
goto_definition 366.3±6.82µs 364.3±5.16µs -0.55%
highlight 9.0±0.07ms 8.7±0.04ms -3.33%
hover 543.6±3.79µs 540.0±5.32µs -0.66%
idents_at_position 121.8±0.27µs 123.9±0.78µs +1.72%
inlay_hints 688.1±33.50µs 658.4±28.13µs -4.32%
on_enter 478.5±11.99ns 496.1±10.89ns +3.68%
parent_decl_at_position 3.7±0.03ms 3.6±0.08ms -2.70%
prepare_rename 363.6±7.99µs 358.5±3.91µs -1.40%
rename 9.4±0.23ms 9.1±0.30ms -3.19%
semantic_tokens 1045.2±20.31µs 1060.9±14.73µs +1.50%
token_at_position 359.9±3.41µs 359.5±1.73µs -0.11%
tokens_at_position 3.7±0.03ms 3.6±0.17ms -2.70%
tokens_for_file 414.5±1.99µs 417.8±3.78µs +0.80%
traverse 37.4±0.79ms 37.3±1.18ms -0.27%

@ironcev ironcev marked this pull request as ready for review March 5, 2024 12:54
@ironcev ironcev requested review from a team March 5, 2024 12:54
Copy link

github-actions bot commented Mar 6, 2024

Benchmark for 634c19b

Click to view benchmark
Test Base PR %
code_action 5.3±0.09ms 5.1±0.05ms -3.77%
code_lens 287.2±10.56ns 288.2±12.28ns +0.35%
compile 4.3±0.06s 4.2±0.10s -2.33%
completion 4.8±0.03ms 4.7±0.16ms -2.08%
did_change_with_caching 3.7±0.06s 3.6±0.07s -2.70%
document_symbol 952.7±10.67µs 1040.5±37.06µs +9.22%
format 72.5±1.79ms 73.5±0.80ms +1.38%
goto_definition 359.0±4.91µs 357.5±5.43µs -0.42%
highlight 9.2±0.05ms 8.7±0.24ms -5.43%
hover 538.2±10.60µs 537.3±11.75µs -0.17%
idents_at_position 121.6±0.36µs 122.9±0.99µs +1.07%
inlay_hints 649.6±26.07µs 655.8±17.86µs +0.95%
on_enter 499.7±16.07ns 487.8±16.51ns -2.38%
parent_decl_at_position 3.7±0.03ms 3.6±0.03ms -2.70%
prepare_rename 354.1±6.14µs 356.0±5.19µs +0.54%
rename 9.4±0.23ms 9.0±0.16ms -4.26%
semantic_tokens 1034.3±11.55µs 1053.6±24.29µs +1.87%
token_at_position 353.8±1.75µs 354.3±2.74µs +0.14%
tokens_at_position 3.7±0.11ms 3.6±0.20ms -2.70%
tokens_for_file 421.0±3.61µs 408.8±2.36µs -2.90%
traverse 37.4±1.32ms 37.2±1.08ms -0.53%

Copy link
Member

@sdankel sdankel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work!

@ironcev ironcev enabled auto-merge (squash) March 7, 2024 09:38
Copy link

github-actions bot commented Mar 7, 2024

Benchmark for 82b6d9b

Click to view benchmark
Test Base PR %
code_action 5.5±0.19ms 5.2±0.10ms -5.45%
code_lens 287.8±7.86ns 291.6±8.84ns +1.32%
compile 5.9±0.08s 5.9±0.07s 0.00%
completion 5.0±0.13ms 4.9±0.51ms -2.00%
did_change_with_caching 5.5±0.04s 5.4±0.06s -1.82%
document_symbol 1023.6±13.01µs 957.1±32.75µs -6.50%
format 73.1±0.88ms 69.9±0.45ms -4.38%
goto_definition 368.5±5.16µs 361.9±6.02µs -1.79%
highlight 9.2±0.17ms 8.8±0.21ms -4.35%
hover 593.1±5.77µs 601.1±28.18µs +1.35%
idents_at_position 123.7±0.35µs 123.3±1.76µs -0.32%
inlay_hints 676.4±11.11µs 724.0±23.61µs +7.04%
on_enter 503.5±12.47ns 487.2±28.75ns -3.24%
parent_decl_at_position 3.8±0.02ms 3.6±0.02ms -5.26%
prepare_rename 367.8±7.29µs 357.7±5.69µs -2.75%
rename 9.6±0.08ms 9.3±0.89ms -3.12%
semantic_tokens 1066.4±11.09µs 1045.8±12.15µs -1.93%
token_at_position 362.6±2.63µs 358.0±7.32µs -1.27%
tokens_at_position 3.8±0.03ms 3.6±0.04ms -5.26%
tokens_for_file 421.3±1.39µs 409.4±3.38µs -2.82%
traverse 45.8±2.33ms 44.7±2.14ms -2.40%

@IGI-111 IGI-111 requested a review from a team March 7, 2024 10:24
Copy link

github-actions bot commented Mar 7, 2024

Benchmark for 3fb405c

Click to view benchmark
Test Base PR %
code_action 5.4±0.03ms 5.3±0.08ms -1.85%
code_lens 287.6±7.60ns 292.0±16.58ns +1.53%
compile 6.1±0.14s 5.9±0.08s -3.28%
completion 5.2±0.79ms 4.8±0.10ms -7.69%
did_change_with_caching 5.4±0.04s 5.4±0.05s 0.00%
document_symbol 999.7±28.36µs 1051.8±29.64µs +5.21%
format 71.8±1.70ms 70.3±1.46ms -2.09%
goto_definition 366.0±7.83µs 366.1±4.03µs +0.03%
highlight 9.1±0.14ms 8.8±0.19ms -3.30%
hover 595.2±16.19µs 592.2±11.64µs -0.50%
idents_at_position 122.5±0.42µs 122.9±1.24µs +0.33%
inlay_hints 674.3±29.04µs 663.7±16.77µs -1.57%
on_enter 492.3±20.26ns 490.2±17.72ns -0.43%
parent_decl_at_position 3.7±0.05ms 3.6±0.11ms -2.70%
prepare_rename 359.9±7.32µs 363.8±7.77µs +1.08%
rename 9.5±0.16ms 9.3±0.25ms -2.11%
semantic_tokens 1050.2±12.01µs 1047.3±13.06µs -0.28%
token_at_position 363.7±2.32µs 357.6±1.97µs -1.68%
tokens_at_position 3.7±0.06ms 3.6±0.04ms -2.70%
tokens_for_file 415.7±1.68µs 408.6±2.47µs -1.71%
traverse 45.9±1.28ms 44.5±1.22ms -3.05%

@IGI-111 IGI-111 requested a review from a team March 7, 2024 13:52
Copy link

github-actions bot commented Mar 7, 2024

Benchmark for b20d013

Click to view benchmark
Test Base PR %
code_action 5.4±0.05ms 5.2±0.13ms -3.70%
code_lens 288.0±8.84ns 294.5±9.97ns +2.26%
compile 6.0±0.08s 5.9±0.08s -1.67%
completion 5.0±0.16ms 4.8±0.03ms -4.00%
did_change_with_caching 5.3±0.06s 5.4±0.03s +1.89%
document_symbol 959.9±15.13µs 1002.7±35.29µs +4.46%
format 71.1±1.59ms 72.6±1.29ms +2.11%
goto_definition 370.3±5.58µs 363.8±8.49µs -1.76%
highlight 9.2±0.28ms 8.8±0.43ms -4.35%
hover 595.9±24.28µs 597.2±22.86µs +0.22%
idents_at_position 121.9±0.48µs 121.3±0.53µs -0.49%
inlay_hints 667.0±12.84µs 656.1±32.67µs -1.63%
on_enter 491.1±21.36ns 492.9±16.91ns +0.37%
parent_decl_at_position 3.7±0.04ms 3.6±0.02ms -2.70%
prepare_rename 358.7±9.44µs 364.2±5.51µs +1.53%
rename 9.6±0.29ms 9.2±0.08ms -4.17%
semantic_tokens 1027.7±26.50µs 1049.2±17.76µs +2.09%
token_at_position 358.6±3.11µs 356.8±9.69µs -0.50%
tokens_at_position 3.7±0.02ms 3.6±0.02ms -2.70%
tokens_for_file 414.5±3.04µs 417.8±21.61µs +0.80%
traverse 44.7±1.69ms 45.2±1.41ms +1.12%

Copy link

github-actions bot commented Mar 8, 2024

Benchmark for ce5d910

Click to view benchmark
Test Base PR %
code_action 5.4±0.15ms 5.3±0.06ms -1.85%
code_lens 290.3±8.28ns 290.9±8.48ns +0.21%
compile 5.7±0.08s 5.7±0.12s 0.00%
completion 4.9±0.06ms 4.7±0.10ms -4.08%
did_change_with_caching 5.2±0.02s 5.2±0.05s 0.00%
document_symbol 985.5±11.95µs 947.0±20.27µs -3.91%
format 69.8±1.68ms 69.2±0.70ms -0.86%
goto_definition 363.9±4.80µs 363.9±3.62µs 0.00%
highlight 9.1±0.26ms 8.7±0.14ms -4.40%
hover 583.5±5.55µs 595.0±20.22µs +1.97%
idents_at_position 122.0±0.21µs 122.9±0.82µs +0.74%
inlay_hints 661.7±22.80µs 671.0±33.83µs +1.41%
on_enter 496.2±19.34ns 495.8±13.00ns -0.08%
parent_decl_at_position 3.7±0.03ms 3.6±0.03ms -2.70%
prepare_rename 360.2±4.00µs 357.4±6.23µs -0.78%
rename 9.5±0.24ms 9.2±0.20ms -3.16%
semantic_tokens 1039.5±13.54µs 1064.5±18.47µs +2.41%
token_at_position 350.8±2.38µs 359.7±2.50µs +2.54%
tokens_at_position 3.7±0.03ms 3.6±0.02ms -2.70%
tokens_for_file 405.8±1.83µs 412.8±6.92µs +1.72%
traverse 42.5±1.40ms 42.7±1.63ms +0.47%

@ironcev ironcev requested a review from sdankel March 11, 2024 08:30
Copy link

Benchmark for 8eb28e3

Click to view benchmark
Test Base PR %
code_action 5.4±0.13ms 5.4±0.13ms 0.00%
code_lens 296.9±8.08ns 333.5±9.92ns +12.33%
compile 5.8±0.08s 5.9±0.10s +1.72%
completion 5.1±0.32ms 5.0±0.21ms -1.96%
did_change_with_caching 5.4±0.06s 5.3±0.07s -1.85%
document_symbol 960.6±40.06µs 1021.2±41.36µs +6.31%
format 71.5±1.55ms 69.3±1.37ms -3.08%
goto_definition 376.4±7.21µs 365.6±9.60µs -2.87%
highlight 9.2±0.25ms 9.1±0.17ms -1.09%
hover 598.4±18.31µs 586.9±10.90µs -1.92%
idents_at_position 123.9±0.41µs 122.3±0.91µs -1.29%
inlay_hints 674.0±21.57µs 674.2±18.71µs +0.03%
on_enter 503.3±36.21ns 499.0±14.57ns -0.85%
parent_decl_at_position 3.7±0.04ms 3.7±0.06ms 0.00%
prepare_rename 367.9±5.48µs 370.1±5.47µs +0.60%
rename 9.6±0.22ms 9.6±0.54ms 0.00%
semantic_tokens 1040.2±12.47µs 1069.5±18.84µs +2.82%
token_at_position 365.3±3.71µs 359.3±3.37µs -1.64%
tokens_at_position 3.7±0.05ms 3.7±0.08ms 0.00%
tokens_for_file 423.5±10.05µs 451.2±2.54µs +6.54%
traverse 44.8±1.99ms 44.9±1.69ms +0.22%

Copy link

Benchmark for f1c358d

Click to view benchmark
Test Base PR %
code_action 5.4±0.08ms 5.4±0.02ms 0.00%
code_lens 303.9±8.84ns 340.7±7.60ns +12.11%
compile 6.1±0.09s 6.0±0.08s -1.64%
completion 4.9±0.07ms 4.9±0.17ms 0.00%
did_change_with_caching 5.5±0.07s 5.5±0.04s 0.00%
document_symbol 994.8±33.35µs 980.4±62.81µs -1.45%
format 72.3±1.01ms 70.3±1.00ms -2.77%
goto_definition 361.3±4.50µs 364.4±6.29µs +0.86%
highlight 9.1±0.12ms 9.1±0.20ms 0.00%
hover 612.3±11.24µs 587.2±5.96µs -4.10%
idents_at_position 121.9±0.39µs 122.0±1.20µs +0.08%
inlay_hints 666.0±14.61µs 667.4±43.74µs +0.21%
on_enter 487.2±14.06ns 493.4±12.66ns +1.27%
parent_decl_at_position 3.7±0.03ms 3.7±0.03ms 0.00%
prepare_rename 361.8±4.86µs 364.1±8.07µs +0.64%
rename 9.6±0.15ms 9.5±0.16ms -1.04%
semantic_tokens 1084.5±9.73µs 1056.6±26.25µs -2.57%
token_at_position 360.7±3.12µs 356.2±3.83µs -1.25%
tokens_at_position 3.7±0.03ms 3.7±0.03ms 0.00%
tokens_for_file 421.1±1.87µs 411.6±2.32µs -2.26%
traverse 45.4±2.82ms 45.4±1.49ms 0.00%

@ironcev ironcev merged commit f8e40eb into master Mar 11, 2024
32 checks passed
@ironcev ironcev deleted the ironcev/references-to-mutable-values-cont branch March 11, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged language feature Core language features visible to end users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants