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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 29 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 29 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 5 additions & 11 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

0 commit comments

Comments
 (0)