Skip to content

Commit 33a451c

Browse files
authored
Rollup merge of #100643 - TaKO8Ki:point-at-type-parameter-shadowing-another-type, r=estebank
Point at a type parameter shadowing another type This patch fixes a part of #97459.
2 parents a003ad6 + 5a848c7 commit 33a451c

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+12
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
161161
msg: String,
162162
fallback_label: String,
163163
span: Span,
164+
span_label: Option<(Span, &'a str)>,
164165
could_be_expr: bool,
165166
suggestion: Option<(Span, &'a str, String)>,
166167
}
@@ -172,6 +173,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
172173
msg: format!("expected {}, found {} `{}`", expected, res.descr(), path_str),
173174
fallback_label: format!("not a {expected}"),
174175
span,
176+
span_label: match res {
177+
Res::Def(kind, def_id) if kind == DefKind::TyParam => {
178+
self.def_span(def_id).map(|span| (span, "found this type pararmeter"))
179+
}
180+
_ => None,
181+
},
175182
could_be_expr: match res {
176183
Res::Def(DefKind::Fn, _) => {
177184
// 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> {
251258
format!("not found in {mod_str}")
252259
},
253260
span: item_span,
261+
span_label: None,
254262
could_be_expr: false,
255263
suggestion,
256264
}
@@ -262,6 +270,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
262270

263271
self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);
264272

273+
if let Some((span, label)) = base_error.span_label {
274+
err.span_label(span, label);
275+
}
276+
265277
if let Some(sugg) = base_error.suggestion {
266278
err.span_suggestion_verbose(sugg.0, sugg.1, sugg.2, Applicability::MaybeIncorrect);
267279
}

src/test/ui/const-generics/generic_const_exprs/issue-69654.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0423]: expected value, found type parameter `T`
22
--> $DIR/issue-69654.rs:5:25
33
|
44
LL | impl<T> Bar<T> for [u8; T] {}
5-
| ^ not a value
5+
| - ^ not a value
6+
| |
7+
| found this type pararmeter
68

79
error[E0599]: the function or associated item `foo` exists for struct `Foo<_>`, but its trait bounds were not satisfied
810
--> $DIR/issue-69654.rs:17:10

src/test/ui/lexical-scopes.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0574]: expected struct, variant or union type, found type parameter `T`
22
--> $DIR/lexical-scopes.rs:3:13
33
|
4+
LL | fn f<T>() {
5+
| - found this type pararmeter
46
LL | let t = T { i: 0 };
57
| ^ not a struct, variant or union type
68

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
trait Foo<T> {
2+
fn foo(&self, name: T) -> usize;
3+
}
4+
5+
struct Bar {
6+
baz: Baz,
7+
}
8+
9+
struct Baz {
10+
num: usize,
11+
}
12+
13+
impl<Baz> Foo<Baz> for Bar {
14+
fn foo(&self, _name: Baz) -> usize {
15+
match self.baz {
16+
Baz { num } => num, //~ ERROR expected struct, variant or union type, found type parameter `Baz`
17+
}
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0574]: expected struct, variant or union type, found type parameter `Baz`
2+
--> $DIR/point-at-type-parameter-shadowing-another-type.rs:16:13
3+
|
4+
LL | impl<Baz> Foo<Baz> for Bar {
5+
| --- found this type pararmeter
6+
...
7+
LL | Baz { num } => num,
8+
| ^^^ not a struct, variant or union type
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0574`.

src/test/ui/span/issue-35987.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0404]: expected trait, found type parameter `Add`
22
--> $DIR/issue-35987.rs:5:21
33
|
44
LL | impl<T: Clone, Add> Add for Foo<T> {
5-
| ^^^ not a trait
5+
| --- ^^^ not a trait
6+
| |
7+
| found this type pararmeter
68
|
79
help: consider importing this trait instead
810
|

src/tools/rust-analyzer/bench_data/glorious_old_parser

+1-1
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ impl<'a> Parser<'a> {
19881988
err.span_suggestion(
19891989
span,
19901990
"declare the type after the parameter binding",
1991-
String::from("<identifier>: <type>"),
1991+
"<identifier>: <type>",
19921992
Applicability::HasPlaceholders,
19931993
);
19941994
} else if require_name && is_trait_item {

0 commit comments

Comments
 (0)