Skip to content

Commit 279e5c1

Browse files
authored
Unrolled build for #142976
Rollup merge of #142976 - compiler-errors:coerce-ice, r=fee1-dead Check CoerceUnsized impl validity before coercing Self-explanatory from the title. Fixes #126982 Fixes #131048 Fixes #134217 Fixes #126269 Fixes #138265
2 parents 0fa4ec6 + 1c9f795 commit 279e5c1

File tree

8 files changed

+46
-59
lines changed

8 files changed

+46
-59
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
4646
use rustc_infer::infer::relate::RelateResult;
4747
use rustc_infer::infer::{Coercion, DefineOpaqueTypes, InferOk, InferResult};
4848
use rustc_infer::traits::{
49-
IfExpressionCause, MatchExpressionArmCause, Obligation, PredicateObligation,
49+
IfExpressionCause, ImplSource, MatchExpressionArmCause, Obligation, PredicateObligation,
5050
PredicateObligations, SelectionError,
5151
};
5252
use rustc_middle::span_bug;
@@ -704,6 +704,19 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
704704
// be silent, as it causes a type mismatch later.
705705
}
706706

707+
Ok(Some(ImplSource::UserDefined(impl_source))) => {
708+
queue.extend(impl_source.nested);
709+
// Certain incoherent `CoerceUnsized` implementations may cause ICEs,
710+
// so check the impl's validity. Taint the body so that we don't try
711+
// to evaluate these invalid coercions in CTFE. We only need to do this
712+
// for local impls, since upstream impls should be valid.
713+
if impl_source.impl_def_id.is_local()
714+
&& let Err(guar) =
715+
self.tcx.ensure_ok().coerce_unsized_info(impl_source.impl_def_id)
716+
{
717+
self.fcx.set_tainted_by_errors(guar);
718+
}
719+
}
707720
Ok(Some(impl_source)) => queue.extend(impl_source.nested_obligations()),
708721
}
709722
}

tests/crashes/126269.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/crashes/126982.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/crashes/131048.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/crashes/134217.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/crashes/138265.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test minimized from #126982.
2+
// We used to apply a coerce_unsized coercion to literally every argument since
3+
// the blanket applied in literally all cases, even though it was incoherent.
4+
5+
#![feature(coerce_unsized)]
6+
7+
impl<A> std::ops::CoerceUnsized<A> for A {}
8+
//~^ ERROR type parameter `A` must be used as the type parameter for some local type
9+
//~| ERROR the trait `CoerceUnsized` may only be implemented for a coercion between structures
10+
11+
const C: usize = 1;
12+
13+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0210]: type parameter `A` must be used as the type parameter for some local type (e.g., `MyStruct<A>`)
2+
--> $DIR/invalid-blanket-coerce-unsized-impl.rs:7:6
3+
|
4+
LL | impl<A> std::ops::CoerceUnsized<A> for A {}
5+
| ^ type parameter `A` must be used as the type parameter for some local type
6+
|
7+
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
8+
= note: only traits defined in the current crate can be implemented for a type parameter
9+
10+
error[E0377]: the trait `CoerceUnsized` may only be implemented for a coercion between structures
11+
--> $DIR/invalid-blanket-coerce-unsized-impl.rs:7:1
12+
|
13+
LL | impl<A> std::ops::CoerceUnsized<A> for A {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to 2 previous errors
17+
18+
Some errors have detailed explanations: E0210, E0377.
19+
For more information about an error, try `rustc --explain E0210`.

0 commit comments

Comments
 (0)