Skip to content

Improve &pin reference-pattern suggestions#156087

Open
P8L1 wants to merge 1 commit intorust-lang:mainfrom
P8L1:improve-pin-pattern-suggestions
Open

Improve &pin reference-pattern suggestions#156087
P8L1 wants to merge 1 commit intorust-lang:mainfrom
P8L1:improve-pin-pattern-suggestions

Conversation

@P8L1
Copy link
Copy Markdown

@P8L1 P8L1 commented May 2, 2026

View all comments

This fills in the pin_ergonomics FIXME in borrow_pat_suggestion by making the diagnostic prefix account for both Pinnedness and Mutability.

Previously, the type-position suggestion path used ordinary reference spelling, which can spell & and &mut but cannot correctly spell pinned reference-pattern suggestions such as &pin const and &pin mut.

This is a diagnostic-only change. It adds focused UI coverage for both pinned const and pinned mut reference-pattern suggestions.

@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 May 2, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 2, 2026

r? @mu001999

rustbot has assigned @mu001999.
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 73 candidates
  • Random selection from 21 candidates

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_hir_typeck/src/pat.rs Outdated
@mu001999
Copy link
Copy Markdown
Member

mu001999 commented May 2, 2026

r? Kivooeo

@rustbot rustbot assigned Kivooeo and unassigned mu001999 May 2, 2026
@P8L1 P8L1 force-pushed the improve-pin-pattern-suggestions branch from 1b1b070 to 61cf5f2 Compare May 2, 2026 14:00
@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented May 2, 2026

two things i think should be improved

  1. currently it make suggestion to a code, that requires pin_ergonomics feature to work, so imo this suggestion should only emits when this feature is enabled (i don't know if there is a way to check so)
  2. i don't sure if this code only works on nightly or not? this suggestion have zero meaning on stable

so, if we can't check 1 point then i'd say to wait on stabilization of this feature, and then fix that fixme

@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented May 2, 2026

@rustbot authot

@P8L1
Copy link
Copy Markdown
Author

P8L1 commented May 3, 2026

two things i think should be improved

  1. currently it make suggestion to a code, that requires pin_ergonomics feature to work, so imo this suggestion should only emits when this feature is enabled (i don't know if there is a way to check so)
  2. i don't sure if this code only works on nightly or not? this suggestion have zero meaning on stable

so, if we can't check 1 point then i'd say to wait on stabilization of this feature, and then fix that fixme

Thanks for the suggestion. I gated the type-position &pin suggestion on pin_ergonomics, so crates without the feature fall back to the existing non-pin reference suggestion instead.

Comment thread tests/ui/pin-ergonomics/ref-pat-suggestions.rs
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@P8L1 P8L1 requested a review from Kivooeo May 3, 2026 15:28
@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented May 4, 2026

This diff could be simplified, looks slightly over-engineered. Here's a leaner approach:

diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs
index d7f15222376..604beb6da3c 100644
--- a/compiler/rustc_hir_typeck/src/pat.rs
+++ b/compiler/rustc_hir_typeck/src/pat.rs
@@ -1407,6 +1407,11 @@ fn borrow_pat_suggestion(&self, err: &mut Diag<'_>, pat: &Pat<'_>) {
             };
 
             match binding_parent {
+                // FIXME(pin_ergnomics): we currenty skip suggestion if feature is not enabled
+                // because the correct suggestion requeiers feature to be enabled
+                // so we want only ask user to enable feature
+                hir::Node::Param(hir::Param { ty_span, pat, .. })
+                    if pat.span != *ty_span && !tcx.features().pin_ergonomics() => {}
                 // Check that there is explicit type (ie this is not a closure param with inferred type)
                 // so we don't suggest moving something to the type that does not exist
                 hir::Node::Param(hir::Param { ty_span, pat, .. }) if pat.span != *ty_span => {
@@ -1414,7 +1419,7 @@ fn borrow_pat_suggestion(&self, err: &mut Diag<'_>, pat: &Pat<'_>) {
                         format!("to take parameter `{binding}` by reference, move `&{pin_and_mut}` to the type"),
                         vec![
                             (pat.span.until(inner.span), "".to_owned()),
-                            (ty_span.shrink_to_lo(), mutbl.ref_prefix_str().to_owned()),
+                            (ty_span.shrink_to_lo(), format!("&{}", pinned.prefix_str(mutbl))),
                         ],
                         Applicability::MachineApplicable
                     );

@P8L1
Copy link
Copy Markdown
Author

P8L1 commented May 4, 2026

This diff could be simplified, looks slightly over-engineered. Here's a leaner approach:

diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs
index d7f15222376..604beb6da3c 100644
--- a/compiler/rustc_hir_typeck/src/pat.rs
+++ b/compiler/rustc_hir_typeck/src/pat.rs
@@ -1407,6 +1407,11 @@ fn borrow_pat_suggestion(&self, err: &mut Diag<'_>, pat: &Pat<'_>) {
             };
 
             match binding_parent {
+                // FIXME(pin_ergnomics): we currenty skip suggestion if feature is not enabled
+                // because the correct suggestion requeiers feature to be enabled
+                // so we want only ask user to enable feature
+                hir::Node::Param(hir::Param { ty_span, pat, .. })
+                    if pat.span != *ty_span && !tcx.features().pin_ergonomics() => {}
                 // Check that there is explicit type (ie this is not a closure param with inferred type)
                 // so we don't suggest moving something to the type that does not exist
                 hir::Node::Param(hir::Param { ty_span, pat, .. }) if pat.span != *ty_span => {
@@ -1414,7 +1419,7 @@ fn borrow_pat_suggestion(&self, err: &mut Diag<'_>, pat: &Pat<'_>) {
                         format!("to take parameter `{binding}` by reference, move `&{pin_and_mut}` to the type"),
                         vec![
                             (pat.span.until(inner.span), "".to_owned()),
-                            (ty_span.shrink_to_lo(), mutbl.ref_prefix_str().to_owned()),
+                            (ty_span.shrink_to_lo(), format!("&{}", pinned.prefix_str(mutbl))),
                         ],
                         Applicability::MachineApplicable
                     );

Thanks, simplified this along those lines.

I kept the feature-gate arm limited to pinned reference patterns so ordinary & / &mut suggestions are not suppressed when pin_ergonomics is disabled. The pinned type-position suggestion is skipped without the feature gate, and the &pin mut / &pin const suggestions are still emitted when the feature is enabled.

I also kept the stabilization FIXME and re-blessed the affected UI output.

Comment thread compiler/rustc_hir_typeck/src/pat.rs Outdated
@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented May 4, 2026

@bors squash

@rust-bors

This comment has been minimized.

* Improve diagnostics for pinned reference patterns
* Gate pin pattern suggestion on pin ergonomics
* Document pin ergonomics suggestion gate
* Remove unnecessary incomplete_features allow

Co-authored-by: Redddy <midzy0228@gmail.com>
* Remove redundant incomplete_features allow

UI tests already allow incomplete_features by default, so the explicit allow is unnecessary.
* Update ref-pat-suggestions stderr

Bless line-number changes after removing the redundant incomplete_features allow.
* Simplify pinned ref pattern suggestion gating
* Fix formatting
* More formatting fixes
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 4, 2026

🔨 9 commits were squashed into 14d414b.

@rust-bors rust-bors Bot force-pushed the improve-pin-pattern-suggestions branch from 0907979 to 14d414b Compare May 4, 2026 20:24
@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented May 4, 2026

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 4, 2026

📌 Commit 14d414b has been approved by Kivooeo

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 4, 2026
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 5, 2026
…ns, r=Kivooeo

Improve `&pin` reference-pattern suggestions

This fills in the `pin_ergonomics` FIXME in `borrow_pat_suggestion` by making the diagnostic prefix account for both `Pinnedness` and `Mutability`.

Previously, the type-position suggestion path used ordinary reference spelling, which can spell `&` and `&mut` but cannot correctly spell pinned reference-pattern suggestions such as `&pin const` and `&pin mut`.

This is a diagnostic-only change. It adds focused UI coverage for both pinned const and pinned mut reference-pattern suggestions.
rust-bors Bot pushed a commit that referenced this pull request May 5, 2026
Rollup of 16 pull requests

Successful merges:

 - #155848 ([doc]: Revert `core::io::ErrorKind` doc changes)
 - #155855 (Remove unnecessary `get_unchecked`)
 - #155543 (docs(unstable-book): Document const generics features)
 - #155962 (`rustc`: `target_features`: allow for `cfg`-only stable `target_features`)
 - #156043 (c-variadic: gate `va_arg` on `c_variadic_experimental_arch`)
 - #156082 (Move tests associated types)
 - #156087 (Improve `&pin` reference-pattern suggestions)
 - #156092 (Clean up `TyCtxt::needs_crate_hash` usage and rename it to `needs_hir_hash`.)
 - #156103 (Fix E0040 suggestion for explicit `Drop::drop` UFCS calls)
 - #156104 (Relax `T: Sized` bound on `try_as_dyn` / `try_as_dyn_mut`)
 - #156122 (Add a `doc_cfg` regression test to ensure foreign types impls are working as expected)
 - #156128 (.mailmap: prefer matching just based on commit emails)
 - #156135 (Remove most uses of def_id_to_node_id)
 - #156152 (Update books)
 - #156154 (tests: mark import UI tests as check-pass)
 - #156162 (Update `browser-ui-test` version to `0.23.5`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants