Skip to content

Check CoerceUnsized impl validity before coercing #142976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
use rustc_infer::infer::relate::RelateResult;
use rustc_infer::infer::{Coercion, DefineOpaqueTypes, InferOk, InferResult};
use rustc_infer::traits::{
IfExpressionCause, MatchExpressionArmCause, Obligation, PredicateObligation,
IfExpressionCause, ImplSource, MatchExpressionArmCause, Obligation, PredicateObligation,
PredicateObligations, SelectionError,
};
use rustc_middle::span_bug;
Expand Down Expand Up @@ -704,6 +704,19 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
// be silent, as it causes a type mismatch later.
}

Ok(Some(ImplSource::UserDefined(impl_source))) => {
queue.extend(impl_source.nested);
// Certain incoherent `CoerceUnsized` implementations may cause ICEs,
// so check the impl's validity. Taint the body so that we don't try
// to evaluate these invalid coercions in CTFE. We only need to do this
// for local impls, since upstream impls should be valid.
if impl_source.impl_def_id.is_local()
&& let Err(guar) =
self.tcx.ensure_ok().coerce_unsized_info(impl_source.impl_def_id)
{
self.fcx.set_tainted_by_errors(guar);
}
}
Ok(Some(impl_source)) => queue.extend(impl_source.nested_obligations()),
}
}
Expand Down
12 changes: 0 additions & 12 deletions tests/crashes/126269.rs

This file was deleted.

18 changes: 0 additions & 18 deletions tests/crashes/126982.rs

This file was deleted.

7 changes: 0 additions & 7 deletions tests/crashes/131048.rs

This file was deleted.

9 changes: 0 additions & 9 deletions tests/crashes/134217.rs

This file was deleted.

12 changes: 0 additions & 12 deletions tests/crashes/138265.rs

This file was deleted.

13 changes: 13 additions & 0 deletions tests/ui/coercion/invalid-blanket-coerce-unsized-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Regression test minimized from #126982.
// We used to apply a coerce_unsized coercion to literally every argument since
// the blanket applied in literally all cases, even though it was incoherent.

#![feature(coerce_unsized)]

impl<A> std::ops::CoerceUnsized<A> for A {}
//~^ ERROR type parameter `A` must be used as the type parameter for some local type
//~| ERROR the trait `CoerceUnsized` may only be implemented for a coercion between structures

const C: usize = 1;

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/coercion/invalid-blanket-coerce-unsized-impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0210]: type parameter `A` must be used as the type parameter for some local type (e.g., `MyStruct<A>`)
--> $DIR/invalid-blanket-coerce-unsized-impl.rs:7:6
|
LL | impl<A> std::ops::CoerceUnsized<A> for A {}
| ^ type parameter `A` must be used as the type parameter for some local type
|
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
= note: only traits defined in the current crate can be implemented for a type parameter

error[E0377]: the trait `CoerceUnsized` may only be implemented for a coercion between structures
--> $DIR/invalid-blanket-coerce-unsized-impl.rs:7:1
|
LL | impl<A> std::ops::CoerceUnsized<A> for A {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0210, E0377.
For more information about an error, try `rustc --explain E0210`.
Loading