Avoid ICE in From/TryFrom diagnostic under -Znext-solver

This commit is contained in:
THARUN 2026-02-15 19:02:44 +05:30
parent ce0bf0b22b
commit b55673bd76
3 changed files with 51 additions and 13 deletions

View file

@ -282,23 +282,31 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if self.tcx.is_diagnostic_item(sym::From, trait_def_id)
|| self.tcx.is_diagnostic_item(sym::TryFrom, trait_def_id)
{
let found_ty = leaf_trait_predicate.skip_binder().trait_ref.args.type_at(1);
let ty = main_trait_predicate.skip_binder().self_ty();
if let Some(cast_ty) = self.find_explicit_cast_type(
obligation.param_env,
found_ty,
ty,
) {
let found_ty_str = self.tcx.short_string(found_ty, &mut long_ty_file);
let cast_ty_str = self.tcx.short_string(cast_ty, &mut long_ty_file);
err.help(
format!(
let trait_ref = leaf_trait_predicate.skip_binder().trait_ref;
// Defensive: next-solver may produce fewer args than expected.
if trait_ref.args.len() > 1 {
let found_ty = trait_ref.args.type_at(1);
let ty = main_trait_predicate.skip_binder().self_ty();
if let Some(cast_ty) = self.find_explicit_cast_type(
obligation.param_env,
found_ty,
ty,
) {
let found_ty_str =
self.tcx.short_string(found_ty, &mut long_ty_file);
let cast_ty_str =
self.tcx.short_string(cast_ty, &mut long_ty_file);
err.help(format!(
"consider casting the `{found_ty_str}` value to `{cast_ty_str}`",
),
);
));
}
}
}
*err.long_ty_path() = long_ty_file;
let mut suggested = false;

View file

@ -0,0 +1,9 @@
//@compile-flags: -Znext-solver=globally
//@check-fail
fn check<T: Iterator>() {
<f32 as From<<T as Iterator>::Item>>::from;
//~^ ERROR the trait bound `f32: From<<T as Iterator>::Item>` is not satisfied
}
fn main() {}

View file

@ -0,0 +1,21 @@
error[E0277]: the trait bound `f32: From<<T as Iterator>::Item>` is not satisfied
--> $DIR/next-solver-ice.rs:5:6
|
LL | <f32 as From<<T as Iterator>::Item>>::from;
| ^^^ the nightly-only, unstable trait `ZeroablePrimitive` is not implemented for `f32`
|
= help: the following other types implement trait `ZeroablePrimitive`:
i128
i16
i32
i64
i8
isize
u128
u16
and 4 others
= note: required for `f32` to implement `From<<T as Iterator>::Item>`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.