Skip to content

Commit 71c64ed

Browse files
committed
Fail candidate assembly for erroneous types
Trait predicates for types which have errors may still evaluate to OK leading to downstream ICEs. Now we return a selection error for such types in candidate assembly and thereby prevent such issues
1 parent 2805aed commit 71c64ed

35 files changed

+614
-47
lines changed

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
8787
} else if lang_items.sized_trait() == Some(def_id) {
8888
// Sized is never implementable by end-users, it is
8989
// always automatically computed.
90+
91+
// Error type cannot possibly implement `Sized` (fixes #123154)
92+
if let ty::Error(_) = obligation.predicate.skip_binder().self_ty().kind() {
93+
return Err(SelectionError::Unimplemented);
94+
}
95+
9096
let sized_conditions = self.sized_conditions(obligation);
9197
self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates);
9298
} else if lang_items.unsize_trait() == Some(def_id) {

compiler/rustc_ty_utils/src/ty.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ fn adt_sized_constraint<'tcx>(
9292

9393
let constraint_ty = sized_constraint_for_ty(tcx, tail_ty)?;
9494
if constraint_ty.references_error() {
95-
return None;
95+
let e = tcx.dcx().span_delayed_bug(DUMMY_SP, "ADT tail has error, but no error reported");
96+
return Some(ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
9697
}
9798

9899
// perf hack: if there is a `constraint_ty: Sized` bound, then we know

tests/ui/associated-inherent-types/issue-109071.no_gate.stderr

+29-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,35 @@ LL | type Item = &[T];
4343
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
4444
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4545

46-
error: aborting due to 4 previous errors
46+
error[E0223]: ambiguous associated type
47+
--> $DIR/issue-109071.rs:15:22
48+
|
49+
LL | fn T() -> Option<Self::Item> {}
50+
| ^^^^^^^^^^
51+
|
52+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
53+
help: use fully-qualified syntax
54+
|
55+
LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
56+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57+
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
58+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59+
60+
error[E0223]: ambiguous associated type
61+
--> $DIR/issue-109071.rs:15:22
62+
|
63+
LL | fn T() -> Option<Self::Item> {}
64+
| ^^^^^^^^^^
65+
|
66+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
67+
help: use fully-qualified syntax
68+
|
69+
LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
70+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
72+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
74+
error: aborting due to 6 previous errors
4775

4876
Some errors have detailed explanations: E0107, E0223, E0637, E0658.
4977
For more information about an error, try `rustc --explain E0107`.

tests/ui/associated-inherent-types/issue-109071.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ impl<T> Windows { //~ ERROR: missing generics for struct `Windows`
1414
impl<T> Windows<T> {
1515
fn T() -> Option<Self::Item> {}
1616
//~^ ERROR: ambiguous associated type
17+
//~| ERROR: ambiguous associated type
18+
//~| ERROR: ambiguous associated type
1719
}
1820

1921
fn main() {}

tests/ui/associated-inherent-types/issue-109071.with_gate.stderr

+29-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,35 @@ LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
3333
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
3434
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3535

36-
error: aborting due to 3 previous errors
36+
error[E0223]: ambiguous associated type
37+
--> $DIR/issue-109071.rs:15:22
38+
|
39+
LL | fn T() -> Option<Self::Item> {}
40+
| ^^^^^^^^^^
41+
|
42+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
43+
help: use fully-qualified syntax
44+
|
45+
LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
46+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47+
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
48+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49+
50+
error[E0223]: ambiguous associated type
51+
--> $DIR/issue-109071.rs:15:22
52+
|
53+
LL | fn T() -> Option<Self::Item> {}
54+
| ^^^^^^^^^^
55+
|
56+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
57+
help: use fully-qualified syntax
58+
|
59+
LL | fn T() -> Option<<Windows<T> as IntoAsyncIterator>::Item> {}
60+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61+
LL | fn T() -> Option<<Windows<T> as IntoIterator>::Item> {}
62+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63+
64+
error: aborting due to 5 previous errors
3765

3866
Some errors have detailed explanations: E0107, E0223, E0637.
3967
For more information about an error, try `rustc --explain E0107`.

tests/ui/closures/issue-78720.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fn server() -> impl {
22
//~^ ERROR at least one trait must be specified
3-
//~| ERROR type annotations needed
43
().map2(|| "")
54
}
65

tests/ui/closures/issue-78720.stderr

+5-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn server() -> impl {
55
| ^^^^
66

77
error[E0412]: cannot find type `F` in this scope
8-
--> $DIR/issue-78720.rs:14:12
8+
--> $DIR/issue-78720.rs:13:12
99
|
1010
LL | _func: F,
1111
| ^
@@ -22,14 +22,8 @@ help: you might be missing a type parameter
2222
LL | struct Map2<Segment2, F> {
2323
| +++
2424

25-
error[E0282]: type annotations needed
26-
--> $DIR/issue-78720.rs:1:16
27-
|
28-
LL | fn server() -> impl {
29-
| ^^^^ cannot infer type
30-
3125
error[E0308]: mismatched types
32-
--> $DIR/issue-78720.rs:8:39
26+
--> $DIR/issue-78720.rs:7:39
3327
|
3428
LL | fn map2<F>(self, f: F) -> Map2<F> {}
3529
| ^^ expected `Map2<F>`, found `()`
@@ -38,7 +32,7 @@ LL | fn map2<F>(self, f: F) -> Map2<F> {}
3832
found unit type `()`
3933

4034
error[E0277]: the size for values of type `Self` cannot be known at compilation time
41-
--> $DIR/issue-78720.rs:8:16
35+
--> $DIR/issue-78720.rs:7:16
4236
|
4337
LL | fn map2<F>(self, f: F) -> Map2<F> {}
4438
| ^^^^ doesn't have a size known at compile-time
@@ -53,7 +47,7 @@ help: function arguments must have a statically known size, borrowed types alway
5347
LL | fn map2<F>(&self, f: F) -> Map2<F> {}
5448
| +
5549

56-
error: aborting due to 5 previous errors
50+
error: aborting due to 4 previous errors
5751

58-
Some errors have detailed explanations: E0277, E0282, E0308, E0412.
52+
Some errors have detailed explanations: E0277, E0308, E0412.
5953
For more information about an error, try `rustc --explain E0277`.

tests/ui/const-generics/const-arg-type-arg-misordered.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ type Array<T, const N: usize> = [T; N];
22

33
fn foo<const N: usize>() -> Array<N, ()> {
44
//~^ ERROR constant provided when a type was expected
5+
//~| ERROR constant provided when a type was expected
56
unimplemented!()
67
}
78

tests/ui/const-generics/const-arg-type-arg-misordered.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ error[E0747]: constant provided when a type was expected
44
LL | fn foo<const N: usize>() -> Array<N, ()> {
55
| ^
66

7-
error: aborting due to 1 previous error
7+
error[E0747]: constant provided when a type was expected
8+
--> $DIR/const-arg-type-arg-misordered.rs:3:35
9+
|
10+
LL | fn foo<const N: usize>() -> Array<N, ()> {
11+
| ^
12+
|
13+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
14+
15+
error: aborting due to 2 previous errors
816

917
For more information about this error, try `rustc --explain E0747`.

tests/ui/const-generics/generic_const_exprs/issue-102768.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const _: () = {
1313
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
1414
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
1515
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
16+
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
17+
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
18+
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
19+
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
1620
//~| ERROR `X` cannot be made into an object
1721
};
1822

tests/ui/const-generics/generic_const_exprs/issue-102768.stderr

+65-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,71 @@ LL | type Y<'a>;
107107
| ^ ...because it contains the generic associated type `Y`
108108
= help: consider moving `Y` to another trait
109109

110-
error: aborting due to 7 previous errors
110+
error[E0107]: associated type takes 1 lifetime argument but 0 lifetime arguments were supplied
111+
--> $DIR/issue-102768.rs:9:30
112+
|
113+
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
114+
| ^ expected 1 lifetime argument
115+
|
116+
note: associated type defined here, with 1 lifetime parameter: `'a`
117+
--> $DIR/issue-102768.rs:5:10
118+
|
119+
LL | type Y<'a>;
120+
| ^ --
121+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
122+
help: add missing lifetime argument
123+
|
124+
LL | fn f2<'a>(arg: Box<dyn X<Y<'_, 1> = &'a ()>>) {}
125+
| +++
126+
127+
error[E0107]: associated type takes 0 generic arguments but 1 generic argument was supplied
128+
--> $DIR/issue-102768.rs:9:30
129+
|
130+
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
131+
| ^--- help: remove these generics
132+
| |
133+
| expected 0 generic arguments
134+
|
135+
note: associated type defined here, with 0 generic parameters
136+
--> $DIR/issue-102768.rs:5:10
137+
|
138+
LL | type Y<'a>;
139+
| ^
140+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
141+
142+
error[E0107]: associated type takes 1 lifetime argument but 0 lifetime arguments were supplied
143+
--> $DIR/issue-102768.rs:9:30
144+
|
145+
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
146+
| ^ expected 1 lifetime argument
147+
|
148+
note: associated type defined here, with 1 lifetime parameter: `'a`
149+
--> $DIR/issue-102768.rs:5:10
150+
|
151+
LL | type Y<'a>;
152+
| ^ --
153+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
154+
help: add missing lifetime argument
155+
|
156+
LL | fn f2<'a>(arg: Box<dyn X<Y<'_, 1> = &'a ()>>) {}
157+
| +++
158+
159+
error[E0107]: associated type takes 0 generic arguments but 1 generic argument was supplied
160+
--> $DIR/issue-102768.rs:9:30
161+
|
162+
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
163+
| ^--- help: remove these generics
164+
| |
165+
| expected 0 generic arguments
166+
|
167+
note: associated type defined here, with 0 generic parameters
168+
--> $DIR/issue-102768.rs:5:10
169+
|
170+
LL | type Y<'a>;
171+
| ^
172+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
173+
174+
error: aborting due to 11 previous errors
111175

112176
Some errors have detailed explanations: E0038, E0107.
113177
For more information about an error, try `rustc --explain E0038`.

tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ impl Opcode2 {
1313
pub fn example2(msg_type: Opcode2) -> impl FnMut(&[u8]) {
1414
move |i| match msg_type {
1515
Opcode2::OP2 => unimplemented!(),
16-
//~^ ERROR could not evaluate constant pattern
1716
}
1817
}
1918

tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ help: you might be missing a type parameter
1717
LL | pub struct Opcode2<S>(&'a S);
1818
| +++
1919

20-
error: could not evaluate constant pattern
21-
--> $DIR/ice-type-mismatch-when-copying-112824.rs:15:9
22-
|
23-
LL | Opcode2::OP2 => unimplemented!(),
24-
| ^^^^^^^^^^^^
25-
26-
error: aborting due to 3 previous errors
20+
error: aborting due to 2 previous errors
2721

2822
Some errors have detailed explanations: E0261, E0412.
2923
For more information about an error, try `rustc --explain E0261`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #123154
2+
3+
struct AA {
4+
pub data: [&usize]
5+
//~^ ERROR missing lifetime specifier
6+
}
7+
8+
impl AA {
9+
const fn new() -> Self { }
10+
//~^ ERROR mismatched types
11+
}
12+
13+
static ST: AA = AA::new();
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0106]: missing lifetime specifier
2+
--> $DIR/ice-unsized-struct-const-eval-123154.rs:4:16
3+
|
4+
LL | pub data: [&usize]
5+
| ^ expected named lifetime parameter
6+
|
7+
help: consider introducing a named lifetime parameter
8+
|
9+
LL ~ struct AA<'a> {
10+
LL ~ pub data: [&'a usize]
11+
|
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/ice-unsized-struct-const-eval-123154.rs:9:23
15+
|
16+
LL | const fn new() -> Self { }
17+
| --- ^^^^ expected `AA`, found `()`
18+
| |
19+
| implicitly returns `()` as its body has no tail or `return` expression
20+
21+
error: aborting due to 2 previous errors
22+
23+
Some errors have detailed explanations: E0106, E0308.
24+
For more information about an error, try `rustc --explain E0106`.

tests/ui/generic-associated-types/gat-in-trait-path-undeclared-lifetime.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ fn main() {
88
//~| ERROR: binding for associated type `Y` references lifetime
99
//~| ERROR: binding for associated type `Y` references lifetime
1010
//~| ERROR: binding for associated type `Y` references lifetime
11+
//~| ERROR: binding for associated type `Y` references lifetime
12+
//~| ERROR: binding for associated type `Y` references lifetime
1113
//~| ERROR: the trait `X` cannot be made into an object
1214
}

tests/ui/generic-associated-types/gat-in-trait-path-undeclared-lifetime.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,23 @@ LL | type Y<'x>;
5151
| ^ ...because it contains the generic associated type `Y`
5252
= help: consider moving `Y` to another trait
5353

54-
error: aborting due to 5 previous errors
54+
error[E0582]: binding for associated type `Y` references lifetime `'a`, which does not appear in the trait input types
55+
--> $DIR/gat-in-trait-path-undeclared-lifetime.rs:6:33
56+
|
57+
LL | fn _f(arg : Box<dyn for<'a> X<Y<'x> = &'a [u32]>>) {}
58+
| ^^^^^^^^^^^^^^^^^
59+
|
60+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
61+
62+
error[E0582]: binding for associated type `Y` references lifetime `'a`, which does not appear in the trait input types
63+
--> $DIR/gat-in-trait-path-undeclared-lifetime.rs:6:33
64+
|
65+
LL | fn _f(arg : Box<dyn for<'a> X<Y<'x> = &'a [u32]>>) {}
66+
| ^^^^^^^^^^^^^^^^^
67+
|
68+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
69+
70+
error: aborting due to 7 previous errors
5571

5672
Some errors have detailed explanations: E0038, E0261, E0582.
5773
For more information about an error, try `rustc --explain E0038`.

tests/ui/generic-associated-types/gat-trait-path-parenthesised-args.rs

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ fn foo<'a>(arg: Box<dyn X<Y('a) = &'a ()>>) {}
1111
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
1212
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
1313
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
14+
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
15+
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
16+
//~| ERROR associated type takes 0 generic arguments but 1 generic argument
17+
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
18+
//~| ERROR at least one trait is required
1419
//~| ERROR at least one trait is required
1520
//~| ERROR: the trait `X` cannot be made into an object
1621

@@ -20,6 +25,8 @@ fn bar<'a>(arg: Box<dyn X<Y() = ()>>) {}
2025
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
2126
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
2227
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
28+
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
29+
//~| ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
2330
//~| ERROR: the trait `X` cannot be made into an object
2431

2532
fn main() {}

0 commit comments

Comments
 (0)