Don't ICE on illegal dyn* casts

This commit is contained in:
Michael Goulet
2024-12-21 23:41:50 +00:00
parent 54dcff104b
commit f67a739611
5 changed files with 52 additions and 18 deletions

View File

@@ -721,13 +721,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
use rustc_middle::ty::cast::IntTy::*;
if self.cast_ty.is_dyn_star() {
if fcx.tcx.features().dyn_star() {
span_bug!(self.span, "should be handled by `coerce`");
} else {
// Report "casting is invalid" rather than "non-primitive cast"
// if the feature is not enabled.
return Err(CastError::IllegalCast);
}
// This coercion will fail if the feature is not enabled, OR
// if the coercion is (currently) illegal (e.g. `dyn* Foo + Send`
// to `dyn* Foo`). Report "casting is invalid" rather than
// "non-primitive cast".
return Err(CastError::IllegalCast);
}
let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))

View File

@@ -737,8 +737,10 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
return Err(TypeError::Mismatch);
}
if let ty::Dynamic(a_data, _, _) = a.kind()
&& let ty::Dynamic(b_data, _, _) = b.kind()
// FIXME(dyn_star): We should probably allow things like casting from
// `dyn* Foo + Send` to `dyn* Foo`.
if let ty::Dynamic(a_data, _, ty::DynStar) = a.kind()
&& let ty::Dynamic(b_data, _, ty::DynStar) = b.kind()
&& a_data.principal_def_id() == b_data.principal_def_id()
{
return self.unify_and(a, b, |_| vec![]);