From 1886aef0355cbb4666368a0b74609006b2221045 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 17 Aug 2022 04:53:06 +0900 Subject: [PATCH 1/2] point at a type parameter shadowing another type --- .../rustc_resolve/src/late/diagnostics.rs | 12 +++++++++++ .../generic_const_exprs/issue-69654.stderr | 4 +++- src/test/ui/lexical-scopes.stderr | 2 ++ ...t-type-parameter-shadowing-another-type.rs | 21 +++++++++++++++++++ ...pe-parameter-shadowing-another-type.stderr | 12 +++++++++++ src/test/ui/span/issue-35987.stderr | 4 +++- 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs create mode 100644 src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index cb133841bca5a..e1f2f8ae09797 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -161,6 +161,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { msg: String, fallback_label: String, span: Span, + span_label: Option<(Span, &'a str)>, could_be_expr: bool, suggestion: Option<(Span, &'a str, String)>, } @@ -172,6 +173,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { msg: format!("expected {}, found {} `{}`", expected, res.descr(), path_str), fallback_label: format!("not a {expected}"), span, + span_label: match res { + Res::Def(kind, def_id) if kind == DefKind::TyParam => { + self.def_span(def_id).map(|span| (span, "found this type pararmeter")) + } + _ => None, + }, could_be_expr: match res { Res::Def(DefKind::Fn, _) => { // Verify whether this is a fn call or an Fn used as a type. @@ -251,6 +258,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { format!("not found in {mod_str}") }, span: item_span, + span_label: None, could_be_expr: false, suggestion, } @@ -262,6 +270,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span); + if let Some((span, label)) = base_error.span_label { + err.span_label(span, label); + } + if let Some(sugg) = base_error.suggestion { err.span_suggestion_verbose(sugg.0, sugg.1, sugg.2, Applicability::MaybeIncorrect); } diff --git a/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr b/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr index 7a083733a2cd1..5ad457d547a69 100644 --- a/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr +++ b/src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr @@ -2,7 +2,9 @@ error[E0423]: expected value, found type parameter `T` --> $DIR/issue-69654.rs:5:25 | LL | impl Bar for [u8; T] {} - | ^ not a value + | - ^ not a value + | | + | found this type pararmeter error[E0599]: the function or associated item `foo` exists for struct `Foo<_>`, but its trait bounds were not satisfied --> $DIR/issue-69654.rs:17:10 diff --git a/src/test/ui/lexical-scopes.stderr b/src/test/ui/lexical-scopes.stderr index 3b2a062c1c254..ad11f72a31d37 100644 --- a/src/test/ui/lexical-scopes.stderr +++ b/src/test/ui/lexical-scopes.stderr @@ -1,6 +1,8 @@ error[E0574]: expected struct, variant or union type, found type parameter `T` --> $DIR/lexical-scopes.rs:3:13 | +LL | fn f() { + | - found this type pararmeter LL | let t = T { i: 0 }; | ^ not a struct, variant or union type diff --git a/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs new file mode 100644 index 0000000000000..bd496875e80b9 --- /dev/null +++ b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.rs @@ -0,0 +1,21 @@ +trait Foo { + fn foo(&self, name: T) -> usize; +} + +struct Bar { + baz: Baz, +} + +struct Baz { + num: usize, +} + +impl Foo for Bar { + fn foo(&self, _name: Baz) -> usize { + match self.baz { + Baz { num } => num, //~ ERROR expected struct, variant or union type, found type parameter `Baz` + } + } +} + +fn main() {} diff --git a/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr new file mode 100644 index 0000000000000..d9c404e94acb4 --- /dev/null +++ b/src/test/ui/resolve/point-at-type-parameter-shadowing-another-type.stderr @@ -0,0 +1,12 @@ +error[E0574]: expected struct, variant or union type, found type parameter `Baz` + --> $DIR/point-at-type-parameter-shadowing-another-type.rs:16:13 + | +LL | impl Foo for Bar { + | --- found this type pararmeter +... +LL | Baz { num } => num, + | ^^^ not a struct, variant or union type + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0574`. diff --git a/src/test/ui/span/issue-35987.stderr b/src/test/ui/span/issue-35987.stderr index 2bc3ff4c3b619..ea9c4c82c3610 100644 --- a/src/test/ui/span/issue-35987.stderr +++ b/src/test/ui/span/issue-35987.stderr @@ -2,7 +2,9 @@ error[E0404]: expected trait, found type parameter `Add` --> $DIR/issue-35987.rs:5:21 | LL | impl Add for Foo { - | ^^^ not a trait + | --- ^^^ not a trait + | | + | found this type pararmeter | help: consider importing this trait instead | From 5a848c701b98bf99b7dd480dc95bf1227b134e55 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 17 Aug 2022 04:58:26 +0900 Subject: [PATCH 2/2] avoid a `&str` to `String` conversion --- src/tools/rust-analyzer/bench_data/glorious_old_parser | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/rust-analyzer/bench_data/glorious_old_parser b/src/tools/rust-analyzer/bench_data/glorious_old_parser index 7e900dfeb1eeb..764893daa12ad 100644 --- a/src/tools/rust-analyzer/bench_data/glorious_old_parser +++ b/src/tools/rust-analyzer/bench_data/glorious_old_parser @@ -1988,7 +1988,7 @@ impl<'a> Parser<'a> { err.span_suggestion( span, "declare the type after the parameter binding", - String::from(": "), + ": ", Applicability::HasPlaceholders, ); } else if require_name && is_trait_item {