Skip to content

Commit a9f5a2a

Browse files
authored
Rollup merge of rust-lang#137601 - davidtwco:deduplicate-type-has-metadata, r=fmease,bjorn3
ssa/mono: deduplicate `type_has_metadata` The implementation of the `type_has_metadata` function is duplicated in `rustc_codegen_ssa` and `rustc_monomorphize`, so move this to `rustc_middle`.
2 parents e18ac65 + 5afa6a1 commit a9f5a2a

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_abi::Primitive::{Int, Pointer};
22
use rustc_abi::{Align, BackendRepr, FieldsShape, Size, TagEncoding, VariantIdx, Variants};
33
use rustc_middle::mir::PlaceTy;
44
use rustc_middle::mir::interpret::Scalar;
5-
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
5+
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
66
use rustc_middle::ty::{self, Ty};
77
use rustc_middle::{bug, mir};
88
use tracing::{debug, instrument};
@@ -168,7 +168,11 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
168168
};
169169
let val = PlaceValue {
170170
llval,
171-
llextra: if bx.cx().type_has_metadata(field.ty) { self.val.llextra } else { None },
171+
llextra: if bx.cx().tcx().type_has_metadata(field.ty, bx.cx().typing_env()) {
172+
self.val.llextra
173+
} else {
174+
None
175+
},
172176
align: effective_field_align,
173177
};
174178
val.with_type(field)

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::assert_matches::assert_matches;
33
use arrayvec::ArrayVec;
44
use rustc_abi::{self as abi, FIRST_VARIANT, FieldIdx};
55
use rustc_middle::ty::adjustment::PointerCoercion;
6-
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
6+
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
77
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
88
use rustc_middle::{bug, mir, span_bug};
99
use rustc_session::config::OptLevel;
@@ -878,7 +878,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
878878

879879
let ty = cg_place.layout.ty;
880880
assert!(
881-
if bx.cx().type_has_metadata(ty) {
881+
if bx.cx().tcx().type_has_metadata(ty, bx.cx().typing_env()) {
882882
matches!(val, OperandValue::Pair(..))
883883
} else {
884884
matches!(val, OperandValue::Immediate(..))

compiler/rustc_codegen_ssa/src/traits/type_.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_abi::{AddressSpace, Float, Integer, Reg};
22
use rustc_middle::bug;
3+
use rustc_middle::ty::Ty;
34
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, TyAndLayout};
4-
use rustc_middle::ty::{self, Ty};
55
use rustc_target::callconv::{ArgAbi, CastTarget, FnAbi};
66

77
use super::BackendTypes;
@@ -84,19 +84,6 @@ pub trait DerivedTypeCodegenMethods<'tcx>:
8484
fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
8585
ty.is_freeze(self.tcx(), self.typing_env())
8686
}
87-
88-
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
89-
if ty.is_sized(self.tcx(), self.typing_env()) {
90-
return false;
91-
}
92-
93-
let tail = self.tcx().struct_tail_for_codegen(ty, self.typing_env());
94-
match tail.kind() {
95-
ty::Foreign(..) => false,
96-
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
97-
_ => bug!("unexpected unsized tail: {:?}", tail),
98-
}
99-
}
10087
}
10188

10289
impl<'tcx, T> DerivedTypeCodegenMethods<'tcx> for T where

compiler/rustc_middle/src/ty/util.rs

+14
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ impl<'tcx> TyCtxt<'tcx> {
208208
tcx.struct_tail_raw(ty, |ty| tcx.normalize_erasing_regions(typing_env, ty), || {})
209209
}
210210

211+
/// Returns true if a type has metadata.
212+
pub fn type_has_metadata(self, ty: Ty<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> bool {
213+
if ty.is_sized(self, typing_env) {
214+
return false;
215+
}
216+
217+
let tail = self.struct_tail_for_codegen(ty, typing_env);
218+
match tail.kind() {
219+
ty::Foreign(..) => false,
220+
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
221+
_ => bug!("unexpected unsized tail: {:?}", tail),
222+
}
223+
}
224+
211225
/// Returns the deeply last field of nested structures, or the same type if
212226
/// not a structure at all. Corresponds to the only possible unsized field,
213227
/// and its type can be used to determine unsizing strategy.

compiler/rustc_monomorphize/src/collector.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -1043,18 +1043,7 @@ fn find_vtable_types_for_unsizing<'tcx>(
10431043
) -> (Ty<'tcx>, Ty<'tcx>) {
10441044
let ptr_vtable = |inner_source: Ty<'tcx>, inner_target: Ty<'tcx>| {
10451045
let typing_env = ty::TypingEnv::fully_monomorphized();
1046-
let type_has_metadata = |ty: Ty<'tcx>| -> bool {
1047-
if ty.is_sized(tcx.tcx, typing_env) {
1048-
return false;
1049-
}
1050-
let tail = tcx.struct_tail_for_codegen(ty, typing_env);
1051-
match tail.kind() {
1052-
ty::Foreign(..) => false,
1053-
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
1054-
_ => bug!("unexpected unsized tail: {:?}", tail),
1055-
}
1056-
};
1057-
if type_has_metadata(inner_source) {
1046+
if tcx.type_has_metadata(inner_source, typing_env) {
10581047
(inner_source, inner_target)
10591048
} else {
10601049
tcx.struct_lockstep_tails_for_codegen(inner_source, inner_target, typing_env)

0 commit comments

Comments
 (0)