Skip to content

Commit

Permalink
Implement has_ptr_meta without computing type layout
Browse files Browse the repository at this point in the history
This matches type_has_metadata in cg_ssa and doesn't require computing
the layout of the type. It is also a bit faster.
  • Loading branch information
bjorn3 committed Dec 18, 2023
1 parent 697aa0a commit c4567c1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ fn clif_pair_type_from_ty<'tcx>(

/// Is a pointer to this type a fat ptr?
pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
let ptr_ty = Ty::new_ptr(tcx, TypeAndMut { ty, mutbl: rustc_hir::Mutability::Not });
match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi {
Abi::Scalar(_) => false,
Abi::ScalarPair(_, _) => true,
abi => unreachable!("Abi of ptr to {:?} is {:?}???", ty, abi),
if ty.is_sized(tcx, ParamEnv::reveal_all()) {
return false;
}

let tail = tcx.struct_tail_erasing_lifetimes(ty, ParamEnv::reveal_all());
match tail.kind() {
ty::Foreign(..) => false,
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
_ => bug!("unexpected unsized tail: {:?}", tail),
}
}

Expand Down

0 comments on commit c4567c1

Please sign in to comment.