1
Fork 0

Rollup merge of #131112 - jswrenn:fix-130413, r=compiler-errors

TransmuteFrom: Gracefully handle unnormalized types and normalization errors

~~Refactor to share code between `TransmuteFrom`'s trait selection and error reporting code paths. Additionally normalizes the source and destination types, and gracefully handles normalization errors.~~

Fixes #130413

r​? `@compiler-errors`
This commit is contained in:
Matthias Krüger 2024-10-03 21:52:45 +02:00 committed by GitHub
commit 33b4947554
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 70 additions and 56 deletions

View file

@ -245,6 +245,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span, "silent safe transmute error"
);
}
GetSafeTransmuteErrorAndReason::Default => {
(err_msg, None)
}
GetSafeTransmuteErrorAndReason::Error {
err_msg,
safe_transmute_explanation,
@ -2226,6 +2229,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
) -> GetSafeTransmuteErrorAndReason {
use rustc_transmute::Answer;
// We don't assemble a transmutability candidate for types that are generic
// and we should have ambiguity for types that still have non-region infer.
if obligation.predicate.has_non_region_param() || obligation.has_non_region_infer() {
return GetSafeTransmuteErrorAndReason::Default;
}
// Erase regions because layout code doesn't particularly care about regions.
let trait_ref =
self.tcx.erase_regions(self.tcx.instantiate_bound_regions_with_erased(trait_ref));
@ -2248,6 +2257,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let dst = trait_ref.args.type_at(0);
let src = trait_ref.args.type_at(1);
let err_msg = format!("`{src}` cannot be safely transmuted into `{dst}`");
match rustc_transmute::TransmuteTypeEnv::new(self.infcx).is_transmutable(

View file

@ -43,6 +43,7 @@ pub struct ImplCandidate<'tcx> {
enum GetSafeTransmuteErrorAndReason {
Silent,
Default,
Error { err_msg: String, safe_transmute_explanation: Option<String> },
}