Skip to content

Commit 2a1dd35

Browse files
Rollup merge of #127988 - estebank:dupe-derive-params, r=fmease
Do not ICE with incorrect empty suggestion When we have two types with the same name, one without type parameters and the other with type parameters and a derive macro, we were before incorrectly suggesting to remove type parameters from the former, which ICEd because we were suggesting to remove nothing. We now gate against this. The output is still not perfect. E0107 should explicitly detect this case and provide better context, but for now let's avoid the ICE. Fix #108748.
2 parents 4722ad1 + 682c5f4 commit 2a1dd35

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,18 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
10481048
},
10491049
);
10501050

1051-
err.span_suggestion(span, msg, "", Applicability::MaybeIncorrect);
1051+
if span.is_empty() {
1052+
// HACK: Avoid ICE when types with the same name with `derive`s are in the same scope:
1053+
// struct NotSM;
1054+
// #[derive(PartialEq, Eq)]
1055+
// struct NotSM<T>(T);
1056+
// With the above code, the suggestion would be to remove the generics of the first
1057+
// `NotSM`, which doesn't *have* generics, so we would suggest to remove no code with
1058+
// no code, which would trigger an `assert!` later. Ideally, we would do something a
1059+
// bit more principled. See closed PR #109082.
1060+
} else {
1061+
err.span_suggestion(span, msg, "", Applicability::MaybeIncorrect);
1062+
}
10521063
} else if redundant_lifetime_args && redundant_type_or_const_args {
10531064
remove_lifetime_args(err);
10541065
remove_type_or_const_args(err);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Here, there are two types with the same name. One of these has a `derive` annotation, but in the
2+
// expansion these `impl`s are associated to the the *other* type. There is a suggestion to remove
3+
// unneded type parameters, but because we're now point at a type with no type parameters, the
4+
// suggestion would suggest removing code from an empty span, which would ICE in nightly.
5+
//
6+
// issue: rust-lang/rust#108748
7+
8+
struct NotSM;
9+
10+
#[derive(PartialEq, Eq)]
11+
//~^ ERROR struct takes 0 generic arguments
12+
//~| ERROR struct takes 0 generic arguments
13+
//~| ERROR struct takes 0 generic arguments
14+
//~| ERROR struct takes 0 generic arguments
15+
struct NotSM<T>(T);
16+
//~^ ERROR the name `NotSM` is defined multiple times
17+
//~| ERROR no field `0`
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
error[E0428]: the name `NotSM` is defined multiple times
2+
--> $DIR/multiple-types-with-same-name-and-derive.rs:15:1
3+
|
4+
LL | struct NotSM;
5+
| ------------- previous definition of the type `NotSM` here
6+
...
7+
LL | struct NotSM<T>(T);
8+
| ^^^^^^^^^^^^^^^^^^^ `NotSM` redefined here
9+
|
10+
= note: `NotSM` must be defined only once in the type namespace of this module
11+
12+
error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
13+
--> $DIR/multiple-types-with-same-name-and-derive.rs:10:10
14+
|
15+
LL | #[derive(PartialEq, Eq)]
16+
| ^^^^^^^^^ expected 0 generic arguments
17+
|
18+
note: struct defined here, with 0 generic parameters
19+
--> $DIR/multiple-types-with-same-name-and-derive.rs:8:8
20+
|
21+
LL | struct NotSM;
22+
| ^^^^^
23+
24+
error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
25+
--> $DIR/multiple-types-with-same-name-and-derive.rs:10:10
26+
|
27+
LL | #[derive(PartialEq, Eq)]
28+
| ^^^^^^^^^ expected 0 generic arguments
29+
|
30+
note: struct defined here, with 0 generic parameters
31+
--> $DIR/multiple-types-with-same-name-and-derive.rs:8:8
32+
|
33+
LL | struct NotSM;
34+
| ^^^^^
35+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
36+
37+
error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
38+
--> $DIR/multiple-types-with-same-name-and-derive.rs:10:21
39+
|
40+
LL | #[derive(PartialEq, Eq)]
41+
| ^^ expected 0 generic arguments
42+
|
43+
note: struct defined here, with 0 generic parameters
44+
--> $DIR/multiple-types-with-same-name-and-derive.rs:8:8
45+
|
46+
LL | struct NotSM;
47+
| ^^^^^
48+
49+
error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
50+
--> $DIR/multiple-types-with-same-name-and-derive.rs:10:10
51+
|
52+
LL | #[derive(PartialEq, Eq)]
53+
| ^^^^^^^^^ expected 0 generic arguments
54+
|
55+
note: struct defined here, with 0 generic parameters
56+
--> $DIR/multiple-types-with-same-name-and-derive.rs:8:8
57+
|
58+
LL | struct NotSM;
59+
| ^^^^^
60+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
61+
62+
error[E0609]: no field `0` on type `&NotSM`
63+
--> $DIR/multiple-types-with-same-name-and-derive.rs:15:17
64+
|
65+
LL | struct NotSM<T>(T);
66+
| ^ unknown field
67+
68+
error: aborting due to 6 previous errors
69+
70+
Some errors have detailed explanations: E0107, E0428, E0609.
71+
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)