From 5bf82ee1eff17463fc844dd28db72abbb6a94a36 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Mon, 25 Sep 2023 15:04:37 +0300 Subject: [PATCH] monomorphize opaque_cast --- .../rustc_codegen_cranelift/src/value_and_place.rs | 2 +- compiler/rustc_codegen_ssa/src/mir/place.rs | 4 +++- .../rustc_const_eval/src/interpret/projection.rs | 5 ++++- .../cross_inference_pattern_bug.rs | 2 +- .../opaque-cast-monormophize-2.rs | 13 +++++++++++++ .../opaque-cast-monormophize.rs | 13 +++++++++++++ 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/ui/type-alias-impl-trait/opaque-cast-monormophize-2.rs create mode 100644 tests/ui/type-alias-impl-trait/opaque-cast-monormophize.rs diff --git a/compiler/rustc_codegen_cranelift/src/value_and_place.rs b/compiler/rustc_codegen_cranelift/src/value_and_place.rs index ff95141ce90fb..d7a6319d539b1 100644 --- a/compiler/rustc_codegen_cranelift/src/value_and_place.rs +++ b/compiler/rustc_codegen_cranelift/src/value_and_place.rs @@ -679,7 +679,7 @@ impl<'tcx> CPlace<'tcx> { fx: &mut FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>, ) -> CPlace<'tcx> { - CPlace { inner: self.inner, layout: fx.layout_of(ty) } + CPlace { inner: self.inner, layout: fx.layout_of(fx.monomorphize(ty)) } } pub(crate) fn place_field( diff --git a/compiler/rustc_codegen_ssa/src/mir/place.rs b/compiler/rustc_codegen_ssa/src/mir/place.rs index a9ecbdc5f35ed..baf411c6f6777 100644 --- a/compiler/rustc_codegen_ssa/src/mir/place.rs +++ b/compiler/rustc_codegen_ssa/src/mir/place.rs @@ -463,7 +463,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { mir::ProjectionElem::Field(ref field, _) => { cg_base.project_field(bx, field.index()) } - mir::ProjectionElem::OpaqueCast(ty) => cg_base.project_type(bx, ty), + mir::ProjectionElem::OpaqueCast(ty) => { + cg_base.project_type(bx, self.monomorphize(ty)) + } mir::ProjectionElem::Index(index) => { let index = &mir::Operand::Copy(mir::Place::from(index)); let index = self.codegen_operand(bx, index); diff --git a/compiler/rustc_const_eval/src/interpret/projection.rs b/compiler/rustc_const_eval/src/interpret/projection.rs index 6c720ac4a574d..df781282e2810 100644 --- a/compiler/rustc_const_eval/src/interpret/projection.rs +++ b/compiler/rustc_const_eval/src/interpret/projection.rs @@ -316,7 +316,10 @@ where { use rustc_middle::mir::ProjectionElem::*; Ok(match proj_elem { - OpaqueCast(ty) => base.transmute(self.layout_of(ty)?, self)?, + OpaqueCast(ty) => base.transmute( + self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(ty)?)?, + self, + )?, Field(field, _) => self.project_field(base, field.index())?, Downcast(_, variant) => self.project_downcast(base, variant)?, Deref => self.deref_pointer(&base.to_op(self)?)?.into(), diff --git a/tests/ui/type-alias-impl-trait/cross_inference_pattern_bug.rs b/tests/ui/type-alias-impl-trait/cross_inference_pattern_bug.rs index 9a50c0f988a56..31fea42fa5d8a 100644 --- a/tests/ui/type-alias-impl-trait/cross_inference_pattern_bug.rs +++ b/tests/ui/type-alias-impl-trait/cross_inference_pattern_bug.rs @@ -1,5 +1,5 @@ // compile-flags: --edition=2021 -// check-pass +// build-pass #![feature(type_alias_impl_trait)] fn main() { diff --git a/tests/ui/type-alias-impl-trait/opaque-cast-monormophize-2.rs b/tests/ui/type-alias-impl-trait/opaque-cast-monormophize-2.rs new file mode 100644 index 0000000000000..a71a40de737c5 --- /dev/null +++ b/tests/ui/type-alias-impl-trait/opaque-cast-monormophize-2.rs @@ -0,0 +1,13 @@ +// build-pass + +#![feature(type_alias_impl_trait)] +const fn foo(x: T) { + type Opaque = impl Copy; + let foo: Opaque = (x, 2u32); + let (a, b): (T, u32) = foo; +} + +fn main() { + const CONST: () = foo::(42u32); + CONST +} diff --git a/tests/ui/type-alias-impl-trait/opaque-cast-monormophize.rs b/tests/ui/type-alias-impl-trait/opaque-cast-monormophize.rs new file mode 100644 index 0000000000000..0509e9b91833d --- /dev/null +++ b/tests/ui/type-alias-impl-trait/opaque-cast-monormophize.rs @@ -0,0 +1,13 @@ +// build-pass + +#![feature(type_alias_impl_trait)] + +fn foo(x: T) { + type Opaque = impl Copy; + let foo: &Opaque = &(x, 2u32); + let (a, b): (T, u32) = *foo; +} + +fn main() { + foo::(1); +}