Skip to content

Commit

Permalink
Rollup merge of rust-lang#100643 - TaKO8Ki:point-at-type-parameter-sh…
Browse files Browse the repository at this point in the history
…adowing-another-type, r=estebank

Point at a type parameter shadowing another type

This patch fixes a part of rust-lang#97459.
  • Loading branch information
matthiaskrgr committed Aug 17, 2022
2 parents a003ad6 + 5a848c7 commit 33a451c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
12 changes: 12 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>,
}
Expand All @@ -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.
Expand Down Expand Up @@ -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,
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0423]: expected value, found type parameter `T`
--> $DIR/issue-69654.rs:5:25
|
LL | impl<T> Bar<T> 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
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/lexical-scopes.stderr
Original file line number Diff line number Diff line change
@@ -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<T>() {
| - found this type pararmeter
LL | let t = T { i: 0 };
| ^ not a struct, variant or union type

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
trait Foo<T> {
fn foo(&self, name: T) -> usize;
}

struct Bar {
baz: Baz,
}

struct Baz {
num: usize,
}

impl<Baz> Foo<Baz> 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() {}
Original file line number Diff line number Diff line change
@@ -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<Baz> Foo<Baz> 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`.
4 changes: 3 additions & 1 deletion src/test/ui/span/issue-35987.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0404]: expected trait, found type parameter `Add`
--> $DIR/issue-35987.rs:5:21
|
LL | impl<T: Clone, Add> Add for Foo<T> {
| ^^^ not a trait
| --- ^^^ not a trait
| |
| found this type pararmeter
|
help: consider importing this trait instead
|
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/bench_data/glorious_old_parser
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ impl<'a> Parser<'a> {
err.span_suggestion(
span,
"declare the type after the parameter binding",
String::from("<identifier>: <type>"),
"<identifier>: <type>",
Applicability::HasPlaceholders,
);
} else if require_name && is_trait_item {
Expand Down

0 comments on commit 33a451c

Please sign in to comment.