Skip to content

Commit 904458f

Browse files
Shallowly bail from coerce_unsized more
1 parent 8242b55 commit 904458f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,31 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
534534
return Err(TypeError::Mismatch);
535535
}
536536

537+
// These targets are known to never be RHS in `LHS: CoerceUnsized<RHS>`.
538+
// That's because these are built-in types for which a core-provided impl
539+
// doesn't exist, and for which a user-written impl is invalid.
540+
match target.kind() {
541+
ty::Bool
542+
| ty::Char
543+
| ty::Int(_)
544+
| ty::Uint(_)
545+
| ty::Float(_)
546+
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
547+
| ty::Str
548+
| ty::Array(_, _)
549+
| ty::Slice(_)
550+
| ty::FnDef(_, _)
551+
| ty::FnPtr(_, _)
552+
| ty::Dynamic(_, _, _)
553+
| ty::Closure(_, _)
554+
| ty::CoroutineClosure(_, _)
555+
| ty::Coroutine(_, _)
556+
| ty::CoroutineWitness(_, _)
557+
| ty::Never
558+
| ty::Tuple(_) => return Err(TypeError::Mismatch),
559+
_ => {}
560+
}
561+
537562
let traits =
538563
(self.tcx.lang_items().unsize_trait(), self.tcx.lang_items().coerce_unsized_trait());
539564
let (Some(unsize_did), Some(coerce_unsized_did)) = traits else {

0 commit comments

Comments
 (0)