Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint: use transparent_newtype_field to avoid ICE #74340

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,23 +531,23 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
match ty.kind {
ty::FnPtr(_) => true,
ty::Ref(..) => true,
ty::Adt(field_def, substs) if field_def.repr.transparent() && !field_def.is_union() => {
for field in field_def.all_fields() {
let field_ty = self.cx.tcx.normalize_erasing_regions(
self.cx.param_env,
field.ty(self.cx.tcx, substs),
);
if field_ty.is_zst(self.cx.tcx, field.did) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does is_zst even do? I'm surprised to see that method on Ty, and suspicious of how it might be implemented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it in an earlier PR (or I just made it with existing logic, I forget), it’s predominantly used in lints which care about transparent types. Its logic is the same as where transparent types are checked in typeck (but it wasn’t trivial to factor it out there too) - it basically gets the layout of the type and calls is_zst on that, returning false if any of the intermediate stuff fails.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Ty should have methods that interact with layout, feels like an abstraction violation.
If something relies on layout that should be clear (i.e. is_zst callers should use layout_of themselves - not to mention that e.g. LateLintContext should have better layout_of integration than doing it on Ty)

continue;
}
ty::Adt(def, substs) if def.repr.transparent() && !def.is_union() => {
let guaranteed_nonnull_optimization = self
.cx
.tcx
.get_attrs(def.did)
.iter()
.any(|a| a.check_name(sym::rustc_nonnull_optimization_guaranteed));

if guaranteed_nonnull_optimization {
return true;
}

let attrs = self.cx.tcx.get_attrs(field_def.did);
if attrs
.iter()
.any(|a| a.check_name(sym::rustc_nonnull_optimization_guaranteed))
|| self.ty_is_known_nonnull(field_ty)
{
return true;
for variant in &def.variants {
if let Some(field) = variant.transparent_newtype_field(self.cx.tcx) {
if self.ty_is_known_nonnull(field.ty(self.cx.tcx, substs)) {
return true;
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/lint/lint-ctypes-73747.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass

#[repr(transparent)]
struct NonNullRawComPtr<T: ComInterface> {
inner: std::ptr::NonNull<<T as ComInterface>::VTable>,
}

trait ComInterface {
type VTable;
}

extern "C" fn invoke<T: ComInterface>(_: Option<NonNullRawComPtr<T>>) {}

fn main() {}