Repository: rust-lang/rust
File: compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs (Rust)

Code before:
                                    return self.dcx().span_delayed_bug(
                                        span, "silent safe transmute error"
                                    );
                                }
                                GetSafeTransmuteErrorAndReason::Default => {
                                    (default_err_msg(), None)
                                }
                                GetSafeTransmuteErrorAndReason::Error {
                                    err_msg,
                                    safe_transmute_explanation,
                                } => (err_msg, safe_transmute_explanation),
                            }
                        } else {
                            (default_err_msg(), None)
                        };

                        let mut err = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);

                        let trait_def_id = main_trait_predicate.def_id();
                        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!(
                                        "consider casting the `{found_ty_str}` value to `{cast_ty_str}`",
                                    ),
                                );
                            }
                        }

                        *err.long_ty_path() = long_ty_file;

                        let mut suggested = false;
                        let mut noted_missing_impl = false;
                        if is_try_conversion || is_question_mark {
                            (suggested, noted_missing_impl) = self.try_conversion_context(&obligation, main_trait_predicate, &mut err);
                        }

                        suggested |= self.detect_negative_literal(
                            &obligation,
                            main_trait_predicate,
                            &mut err,

Review comment (bug):
It shouldn't. There's a more involved underlying bug here

Code after:
                                    return self.dcx().span_delayed_bug(
                                        span, "silent safe transmute error"
                                    );
                                }
                                GetSafeTransmuteErrorAndReason::Default => {
                                    (default_err_msg(), None)
                                }
                                GetSafeTransmuteErrorAndReason::Error {
                                    err_msg,
                                    safe_transmute_explanation,
                                } => (err_msg, safe_transmute_explanation),
                            }
                        } else {
                            (default_err_msg(), None)
                        };

                        let mut err = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);

                        let trait_def_id = main_trait_predicate.def_id();
                        if self.tcx.is_diagnostic_item(sym::From, trait_def_id)
                            || self.tcx.is_diagnostic_item(sym::TryFrom, trait_def_id)
                        {
                            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;
                        let mut noted_missing_impl = false;