Skip to content
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
6 changes: 6 additions & 0 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,9 @@ impl ImproperCTypesDefinitions {
return true;
} else if let Adt(adt_def, _) = ty.kind()
&& adt_def.is_struct()
&& adt_def.repr().c()
&& !adt_def.repr().packed()
&& adt_def.repr().align.is_none()
Copy link
Member

Choose a reason for hiding this comment

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

Does this affect the strcuts without repr(C)? I guess this should be like this:

&& adt_def.is_struct()
&& (!adt_def.repr().c() || (!adt_def.repr().packed() && adt_def.repr().align.is_none()))

Copy link
Member

@workingjubilee workingjubilee Mar 29, 2025

Choose a reason for hiding this comment

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

That is incorrect, this should only matter for repr(C). The && means that repr(C) must be satisfied first before we concern ourselves with the rest, which is correct.

Copy link
Contributor Author

@amy-kwan amy-kwan Mar 30, 2025

Choose a reason for hiding this comment

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

Thanks @SparrowLii for the review and Thanks @workingjubilee for the reply - yes, this should only affect structures annotated with repr(C).

{
let struct_variant = adt_def.variant(VariantIdx::ZERO);
// Within a nested struct, all fields are examined to correctly
Expand All @@ -1659,8 +1662,11 @@ impl ImproperCTypesDefinitions {
item: &'tcx hir::Item<'tcx>,
) {
let adt_def = cx.tcx.adt_def(item.owner_id.to_def_id());
// repr(C) structs also with packed or aligned representation
// should be ignored.
if adt_def.repr().c()
&& !adt_def.repr().packed()
&& adt_def.repr().align.is_none()
&& cx.tcx.sess.target.os == "aix"
&& !adt_def.all_fields().next().is_none()
{
Expand Down
26 changes: 25 additions & 1 deletion tests/ui/layout/reprc-power-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,29 @@ pub struct I {
d: f32,
e: f64,
}

#[repr(C)]
pub struct J {
a: u8,
b: I,
}
// The lint also ignores diagnosing #[repr(align(n))].
#[repr(C, align(8))]
pub struct K {
a: u8,
b: u8,
c: f64,
d: f32,
e: f64,
}
#[repr(C)]
pub struct L {
a: u8,
b: K,
}
#[repr(C, align(8))]
pub struct M {
a: u8,
b: K,
c: L,
}
fn main() { }
Loading