From 8f2273e518c939f10c3ba891058cd7a43aedce23 Mon Sep 17 00:00:00 2001 From: Falco Hirschenberger Date: Fri, 11 Oct 2024 10:46:38 +0200 Subject: [PATCH] Fix #131471, range misleading field access Fixes #131471 by checking if the range-start is a literal. --- compiler/rustc_resolve/src/late.rs | 1 + tests/ui/range/misleading-field-access-hint.rs | 8 ++++++++ tests/ui/range/misleading-field-access-hint.stderr | 9 +++++++++ 3 files changed, 18 insertions(+) create mode 100644 tests/ui/range/misleading-field-access-hint.rs create mode 100644 tests/ui/range/misleading-field-access-hint.stderr diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index b84cbf9c629..98db36b12be 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -4011,6 +4011,7 @@ fn smart_resolve_path_fragment( let instead = res.is_some(); let suggestion = if let Some((start, end)) = this.diag_metadata.in_range && path[0].ident.span.lo() == end.span.lo() + && !matches!(start.kind, ExprKind::Lit(_)) { let mut sugg = "."; let mut span = start.span.between(end.span); diff --git a/tests/ui/range/misleading-field-access-hint.rs b/tests/ui/range/misleading-field-access-hint.rs new file mode 100644 index 00000000000..252f1a4833c --- /dev/null +++ b/tests/ui/range/misleading-field-access-hint.rs @@ -0,0 +1,8 @@ +// Check if rustc still displays the misleading hint to write `.` instead of `..` +fn main() { + let width = 10; + // ... + for _ in 0..w { + //~^ ERROR cannot find value `w` + } +} diff --git a/tests/ui/range/misleading-field-access-hint.stderr b/tests/ui/range/misleading-field-access-hint.stderr new file mode 100644 index 00000000000..9b112a5fdd8 --- /dev/null +++ b/tests/ui/range/misleading-field-access-hint.stderr @@ -0,0 +1,9 @@ +error[E0425]: cannot find value `w` in this scope + --> $DIR/misleading-field-access-hint.rs:5:17 + | +LL | for _ in 0..w { + | ^ not found in this scope + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0425`.