Skip to content

Commit 539242a

Browse files
committed
Add a suggestion when using a type alias instead of trait alias
1 parent a5029ac commit 539242a

8 files changed

+61
-17
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
928928
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
929929
`type` alias";
930930
if let Some(span) = self.def_span(def_id) {
931-
err.span_help(span, msg);
931+
if let Ok(snip) = self.r.session.source_map().span_to_snippet(span) {
932+
// The span contains a type alias so we should be able to
933+
// replace `type` with `trait`.
934+
let snip = snip.replacen("type", "trait", 1);
935+
err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
936+
} else {
937+
err.span_help(span, msg);
938+
}
932939
} else {
933940
err.help(msg);
934941
}

src/test/ui/codemap_tests/two_files.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ LL | impl Bar for Baz { }
55
| ^^^ type aliases cannot be used as traits
66
|
77
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
8-
--> $DIR/two_files_data.rs:5:1
98
|
10-
LL | type Bar = dyn Foo;
11-
| ^^^^^^^^^^^^^^^^^^^
9+
LL | trait Bar = dyn Foo;
10+
|
1211

1312
error: aborting due to previous error
1413

src/test/ui/resolve/issue-3907.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ LL | impl Foo for S {
55
| ^^^ type aliases cannot be used as traits
66
|
77
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
8-
--> $DIR/issue-3907.rs:5:1
98
|
10-
LL | type Foo = dyn issue_3907::Foo;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
LL | trait Foo = dyn issue_3907::Foo;
10+
|
1211
help: consider importing this trait instead
1312
|
1413
LL | use issue_3907::Foo;

src/test/ui/resolve/issue-5035.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ LL | trait I {}
1111
| ------- similarly named trait `I` defined here
1212
LL | type K = dyn I;
1313
LL | impl K for isize {}
14-
| ^
15-
| |
16-
| type aliases cannot be used as traits
17-
| help: a trait with a similar name exists: `I`
14+
| ^ type aliases cannot be used as traits
1815
|
1916
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
20-
--> $DIR/issue-5035.rs:2:1
2117
|
22-
LL | type K = dyn I;
23-
| ^^^^^^^^^^^^^^^
18+
LL | trait K = dyn I;
19+
|
20+
help: a trait with a similar name exists
21+
|
22+
LL | impl I for isize {}
23+
| ^
2424

2525
error: aborting due to 2 previous errors
2626

src/test/ui/resolve/unboxed-closure-sugar-nonexistent-trait.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ LL | fn g<F:Typedef(isize) -> isize>(x: F) {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^ type aliases cannot be used as traits
1212
|
1313
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
14-
--> $DIR/unboxed-closure-sugar-nonexistent-trait.rs:4:1
1514
|
16-
LL | type Typedef = isize;
17-
| ^^^^^^^^^^^^^^^^^^^^^
15+
LL | trait Typedef = isize;
16+
|
1817

1918
error: aborting due to 2 previous errors
2019

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test of #43913.
2+
3+
// run-rustfix
4+
5+
#![feature(trait_alias)]
6+
#![allow(bare_trait_objects, dead_code)]
7+
8+
trait Strings = Iterator<Item=String>;
9+
10+
struct Struct<S: Strings>(S);
11+
//~^ ERROR: expected trait, found type alias `Strings`
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test of #43913.
2+
3+
// run-rustfix
4+
5+
#![feature(trait_alias)]
6+
#![allow(bare_trait_objects, dead_code)]
7+
8+
type Strings = Iterator<Item=String>;
9+
10+
struct Struct<S: Strings>(S);
11+
//~^ ERROR: expected trait, found type alias `Strings`
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0404]: expected trait, found type alias `Strings`
2+
--> $DIR/suggest-trait-alias-instead-of-type.rs:10:18
3+
|
4+
LL | struct Struct<S: Strings>(S);
5+
| ^^^^^^^ type aliases cannot be used as traits
6+
|
7+
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
8+
|
9+
LL | trait Strings = Iterator<Item=String>;
10+
|
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0404`.

0 commit comments

Comments
 (0)