Skip to content

Commit 2d43a8b

Browse files
authored
Rollup merge of #139200 - xizheyin:issue-139174, r=compiler-errors
Skip suggest impl or dyn when poly trait is not a real trait Fixes #139174 When `poly_trait_ref` is not a real trait, we should stop suggesting `impl` and `dyn` to avoid false positives. 3 cases were added to the ui test. https://github.com/rust-lang/rust/blob/0b45675cfcec57f30a3794e1a1e18423aa9cf200/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs#L88-L93 In the first commit, I submitted the test and passed it. In the second commit, I modified the code and we can see the changes in the test. r? compiler
2 parents 2311b34 + 12604fa commit 2d43a8b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
8686
"expected a type, found a trait"
8787
);
8888
if self_ty.span.can_be_used_for_suggestions()
89+
&& poly_trait_ref.trait_ref.trait_def_id().is_some()
8990
&& !self.maybe_suggest_impl_trait(self_ty, &mut diag)
9091
&& !self.maybe_suggest_dyn_trait(self_ty, sugg, &mut diag)
9192
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@compile-flags: --edition 2021
2+
3+
fn f<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> usize {
4+
//~^ ERROR expected trait, found builtin type `usize`
5+
//~| ERROR expected a type, found a trait [E0782]
6+
0
7+
}
8+
9+
fn create_adder<'a>(x: i32) -> usize + 'a {
10+
//~^ ERROR expected trait, found builtin type `usize`
11+
//~| ERROR expected a type, found a trait [E0782]
12+
move |y| x + y
13+
}
14+
15+
struct Struct<'a>{
16+
x: usize + 'a,
17+
//~^ ERROR expected trait, found builtin type `usize`
18+
//~| ERROR expected a type, found a trait [E0782]
19+
}
20+
21+
22+
fn main() {
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0404]: expected trait, found builtin type `usize`
2+
--> $DIR/suggestion-trait-object-issue-139174.rs:3:36
3+
|
4+
LL | fn f<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> usize {
5+
| ^^^^^ not a trait
6+
7+
error[E0404]: expected trait, found builtin type `usize`
8+
--> $DIR/suggestion-trait-object-issue-139174.rs:9:32
9+
|
10+
LL | fn create_adder<'a>(x: i32) -> usize + 'a {
11+
| ^^^^^ not a trait
12+
13+
error[E0404]: expected trait, found builtin type `usize`
14+
--> $DIR/suggestion-trait-object-issue-139174.rs:16:8
15+
|
16+
LL | x: usize + 'a,
17+
| ^^^^^ not a trait
18+
19+
error[E0782]: expected a type, found a trait
20+
--> $DIR/suggestion-trait-object-issue-139174.rs:3:36
21+
|
22+
LL | fn f<'a>(x: Box<dyn Fn() -> Option<usize + 'a>>) -> usize {
23+
| ^^^^^^^^^^
24+
25+
error[E0782]: expected a type, found a trait
26+
--> $DIR/suggestion-trait-object-issue-139174.rs:9:32
27+
|
28+
LL | fn create_adder<'a>(x: i32) -> usize + 'a {
29+
| ^^^^^^^^^^
30+
31+
error[E0782]: expected a type, found a trait
32+
--> $DIR/suggestion-trait-object-issue-139174.rs:16:8
33+
|
34+
LL | x: usize + 'a,
35+
| ^^^^^^^^^^
36+
37+
error: aborting due to 6 previous errors
38+
39+
Some errors have detailed explanations: E0404, E0782.
40+
For more information about an error, try `rustc --explain E0404`.

0 commit comments

Comments
 (0)