Skip to content

Commit dbe7ed3

Browse files
authored
Rollup merge of rust-lang#74340 - davidtwco:issue-73747-improper-ctypes-defns-is-zst-with-params, r=pnkfelix
lint: use `transparent_newtype_field` to avoid ICE Fixes rust-lang#73747. This PR re-uses the `transparent_newtype_field` function instead of manually calling `is_zst` on normalized fields to determine which field in a transparent type is the non-zero-sized field, thus avoiding an ICE.
2 parents c4fcf5a + cccc310 commit dbe7ed3

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/librustc_lint/types.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -531,23 +531,23 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
531531
match ty.kind {
532532
ty::FnPtr(_) => true,
533533
ty::Ref(..) => true,
534-
ty::Adt(field_def, substs) if field_def.repr.transparent() && !field_def.is_union() => {
535-
for field in field_def.all_fields() {
536-
let field_ty = self.cx.tcx.normalize_erasing_regions(
537-
self.cx.param_env,
538-
field.ty(self.cx.tcx, substs),
539-
);
540-
if field_ty.is_zst(self.cx.tcx, field.did) {
541-
continue;
542-
}
534+
ty::Adt(def, substs) if def.repr.transparent() && !def.is_union() => {
535+
let guaranteed_nonnull_optimization = self
536+
.cx
537+
.tcx
538+
.get_attrs(def.did)
539+
.iter()
540+
.any(|a| a.check_name(sym::rustc_nonnull_optimization_guaranteed));
541+
542+
if guaranteed_nonnull_optimization {
543+
return true;
544+
}
543545

544-
let attrs = self.cx.tcx.get_attrs(field_def.did);
545-
if attrs
546-
.iter()
547-
.any(|a| a.check_name(sym::rustc_nonnull_optimization_guaranteed))
548-
|| self.ty_is_known_nonnull(field_ty)
549-
{
550-
return true;
546+
for variant in &def.variants {
547+
if let Some(field) = variant.transparent_newtype_field(self.cx.tcx) {
548+
if self.ty_is_known_nonnull(field.ty(self.cx.tcx, substs)) {
549+
return true;
550+
}
551551
}
552552
}
553553

src/test/ui/lint/lint-ctypes-73747.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
3+
#[repr(transparent)]
4+
struct NonNullRawComPtr<T: ComInterface> {
5+
inner: std::ptr::NonNull<<T as ComInterface>::VTable>,
6+
}
7+
8+
trait ComInterface {
9+
type VTable;
10+
}
11+
12+
extern "C" fn invoke<T: ComInterface>(_: Option<NonNullRawComPtr<T>>) {}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)