diff --git a/compiler/rustc_hir_typeck/src/intrinsicck.rs b/compiler/rustc_hir_typeck/src/intrinsicck.rs index e009be599e3..18ea454f82c 100644 --- a/compiler/rustc_hir_typeck/src/intrinsicck.rs +++ b/compiler/rustc_hir_typeck/src/intrinsicck.rs @@ -70,13 +70,13 @@ fn skeleton_string<'tcx>( fn check_transmute<'tcx>( tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, - from: Ty<'tcx>, - to: Ty<'tcx>, + from: Unnormalized<'tcx, Ty<'tcx>>, + to: Unnormalized<'tcx, Ty<'tcx>>, hir_id: HirId, ) -> Result<(), ErrorGuaranteed> { let span = tcx.hir_span(hir_id); let normalize = |ty| { - if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, Unnormalized::new_wip(ty)) { + if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, ty) { ty } else { Ty::new_error_with_message( @@ -253,6 +253,7 @@ pub(crate) fn check_transmutes(tcx: TyCtxt<'_>, owner: LocalDefId) -> Result<(), let typing_env = ty::TypingEnv::codegen(tcx, owner); let mut result = Ok(()); for &(from, to, hir_id) in &typeck_results.transmutes_to_check { + let (to, from) = ty::set_aliases_to_non_rigid(tcx, (to, from)).unzip(); result = result.and(check_transmute(tcx, typing_env, from, to, hir_id)); } result diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index 85235906c38..d81eabc032f 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -60,7 +60,7 @@ pub fn ensure_wf<'tcx>( } fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, kinds: &[RustcDumpLayoutKind]) { - let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id); + let typing_env = ty::TypingEnv::codegen(tcx, item_def_id); let ty = tcx.type_of(item_def_id).instantiate_identity().skip_norm_wip(); let span = tcx.def_span(item_def_id.to_def_id()); if !ensure_wf(tcx, typing_env, ty, item_def_id, span) { diff --git a/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.next.stderr b/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.next.stderr new file mode 100644 index 00000000000..52aa7cbcab5 --- /dev/null +++ b/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.next.stderr @@ -0,0 +1,8 @@ +error: size: Size(1 bytes) + --> $DIR/dump-layout-opaque-coroutine.rs:19:1 + | +LL | type Foo = impl Sized; + | ^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.old.stderr b/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.old.stderr new file mode 100644 index 00000000000..52aa7cbcab5 --- /dev/null +++ b/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.old.stderr @@ -0,0 +1,8 @@ +error: size: Size(1 bytes) + --> $DIR/dump-layout-opaque-coroutine.rs:19:1 + | +LL | type Foo = impl Sized; + | ^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.rs b/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.rs new file mode 100644 index 00000000000..cb95c2f6747 --- /dev/null +++ b/tests/ui/traits/next-solver/opaques/dump-layout-opaque-coroutine.rs @@ -0,0 +1,25 @@ +//@ revisions: old next +//@[next] compile-flags: -Znext-solver +//@ edition: 2024 + +// Regression test for the second example in +// https://github.com/rust-lang/trait-system-refactor-initiative/issues/283. +// +// `rustc_dump_layout` normalizes `Foo` to the coroutine returned by `get`. Coroutine layouts can +// only be computed in `TypingMode::Codegen`, using `TypingMode::PostAnalysis` makes `layout_of` +// return error instead of reporting the expected layout. This was in both trait solvers. + +#![feature(rustc_attrs)] +#![feature(type_alias_impl_trait)] +#![expect(internal_features)] + +async fn get() {} + +#[rustc_dump_layout(size)] +type Foo = impl Sized; +//~^ ERROR: size: Size(1 bytes) + +#[define_opaque(Foo)] +fn main() { + let _: Foo = get(); +} diff --git a/tests/ui/traits/next-solver/opaques/transmute-async-fn-return-type.rs b/tests/ui/traits/next-solver/opaques/transmute-async-fn-return-type.rs new file mode 100644 index 00000000000..237abb98349 --- /dev/null +++ b/tests/ui/traits/next-solver/opaques/transmute-async-fn-return-type.rs @@ -0,0 +1,13 @@ +//@ check-pass +//@ edition: 2024 +//@ compile-flags: -Znext-solver + +// Regression test for https://github.com/rust-lang/trait-system-refactor-initiative/issues/283. +// +// `check_transmutes` must make the alias non-rigid when crossing the typing-mode boundary. + +async fn get(_r: std::marker::PhantomData) {} +fn main() { + let v = get(loop {}); + unsafe { v = std::mem::transmute(0_u8) } +}