Skip to content

Commit c4567c1

Browse files
committed
Implement has_ptr_meta without computing type layout
This matches type_has_metadata in cg_ssa and doesn't require computing the layout of the type. It is also a bit faster.
1 parent 697aa0a commit c4567c1

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/common.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ fn clif_pair_type_from_ty<'tcx>(
9898

9999
/// Is a pointer to this type a fat ptr?
100100
pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
101-
let ptr_ty = Ty::new_ptr(tcx, TypeAndMut { ty, mutbl: rustc_hir::Mutability::Not });
102-
match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi {
103-
Abi::Scalar(_) => false,
104-
Abi::ScalarPair(_, _) => true,
105-
abi => unreachable!("Abi of ptr to {:?} is {:?}???", ty, abi),
101+
if ty.is_sized(tcx, ParamEnv::reveal_all()) {
102+
return false;
103+
}
104+
105+
let tail = tcx.struct_tail_erasing_lifetimes(ty, ParamEnv::reveal_all());
106+
match tail.kind() {
107+
ty::Foreign(..) => false,
108+
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
109+
_ => bug!("unexpected unsized tail: {:?}", tail),
106110
}
107111
}
108112

0 commit comments

Comments
 (0)