Skip to content

Recover from incorrect replacement of & for types and expressions - #162376

Open
KevinA-cpu wants to merge 1 commit into
rust-lang:mainfrom
KevinA-cpu:c-style-reference-recovery
Open

Recover from incorrect replacement of & for types and expressions#162376
KevinA-cpu wants to merge 1 commit into
rust-lang:mainfrom
KevinA-cpu:c-style-reference-recovery

Conversation

@KevinA-cpu

@KevinA-cpu KevinA-cpu commented Sep 6, 2026

Copy link
Copy Markdown
Contributor
  • Suggest from T& to &T
  • Suggest from mut& to &mut
  • Suggest from mut &'a u8 to &'a mut u8
  • Suggest from expr& to &expr
  • Suggest from expr &mut to &mut expr

Guards:

  • check for may_recover() to avoid breaking macros
  • can_begin_type/can_begin_expr to avoid breaking u8 & u8 and tests/ui/consts/closure-type-error-during-const-eval-66706.rs
  • Binary/Cast guards are in-place to prevent making wrong suggestions in complicated lhs

Does not touch the following areas:

#101487

@rustbot

rustbot commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 6, 2026
@rustbot

rustbot commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 21 candidates

@rustbot

This comment has been minimized.

- Suggest from `T&` to `&T`
- Suggest from `mut&` to `&mut`
- Suggest from `mut &'a u8` to `&'a mut u8`
- Suggest from `expr&` to `&expr`
- Suggest from `expr &mut` to `&mut expr`

Guards:
- check for may_recover() to avoid breaking macros
- can_begin_type/can_begin_expr to avoid breaking `u8 & u8` and tests/ui/consts/closure-type-error-during-const-eval-66706.rs
- Binary/Cast guards are in-place to prevent making wrong suggestions in complicated `lhs`

Does not touch the following areas:

- const& T: overlapping with open issue 146122
- i32&&: C++'s T&& is an rvalue reference and Rust doesn't have the same thing currently
- u8 &mut
- x as u8&
@KevinA-cpu
KevinA-cpu force-pushed the c-style-reference-recovery branch from bba988b to bea1b70 Compare September 6, 2026 15:19

@fmease fmease Sep 6, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We're in an expression context here, so "reference types" isn't correct terminology. You're looking for "borrow expression".

However, stepping back, expr& is not a thing in C/C++, so I'm not so sure if it's worth trying to recover from it. Do you think that anybody is really going to accidentally type expr& when they meant &expr? Re. expr &mut I'm 99.9% confident nobody has ever written that by accident.

View changes since the review

@fmease fmease Sep 6, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm wondering whether we should say &mut /* Type */ instead to make it crystal clear what is a placeholder and what isn't.

View changes since the review

@fmease fmease Sep 6, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for making sure this is well-tested. However, personally I think it's a tinge excessive for such a niche recovery 🤷

View changes since the review

@nnethercote

Copy link
Copy Markdown
Contributor

r? @fmease

@rustbot rustbot assigned fmease and unassigned nnethercote Sep 6, 2026
@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 7, 2026
@rust-bors

rust-bors Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #162269) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants