Skip to content

Commit ba935ac

Browse files
committed
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`.
1 parent ad27045 commit ba935ac

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

compiler/rustc_codegen_ssa/src/traits/type_.rs

+2-11
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;
@@ -86,16 +86,7 @@ pub trait DerivedTypeCodegenMethods<'tcx>:
8686
}
8787

8888
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-
}
89+
self.tcx().type_has_metadata(ty, self.typing_env())
9990
}
10091
}
10192

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)