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

Improve diagnostic for E0691 #98071

Closed
Closed
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
76 changes: 54 additions & 22 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::util::{Discr, IntTypeExt};
use rustc_middle::ty::{self, ParamEnv, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable};
use rustc_middle::ty::{
self, Array, ParamEnv, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
};
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
use rustc_span::symbol::sym;
use rustc_span::{self, Span};
Expand Down Expand Up @@ -1204,17 +1206,17 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
for attr in tcx.get_attrs(def.did(), sym::repr) {
for r in attr::parse_repr_attr(&tcx.sess, attr) {
if let attr::ReprPacked(pack) = r
&& let Some(repr_pack) = repr.pack
&& pack as u64 != repr_pack.bytes()
{
struct_span_err!(
tcx.sess,
sp,
E0634,
"type has conflicting packed representation hints"
)
.emit();
}
&& let Some(repr_pack) = repr.pack
&& pack as u64 != repr_pack.bytes()
{
struct_span_err!(
tcx.sess,
sp,
E0634,
"type has conflicting packed representation hints"
)
.emit();
}
}
}
if repr.align.is_some() {
Expand Down Expand Up @@ -1327,28 +1329,58 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtD
let layout = tcx.layout_of(param_env.and(ty));
// We are currently checking the type this field came from, so it must be local
let span = tcx.hir().span_if_local(field.did).unwrap();
let zero_sized_array = match ty.kind() {
Array(_, len) => len.try_eval_usize(tcx, param_env) == Some(0),
_ => false,
};
let zst = layout.map_or(false, |layout| layout.is_zst());
let align1 = layout.map_or(false, |layout| layout.align.abi.bytes() == 1);
(span, zst, align1)
let align = layout.ok().map(|layout| layout.align.abi.bytes());
(span, zst, zero_sized_array, align)
});

let non_zst_fields =
field_infos.clone().filter_map(|(span, zst, _align1)| if !zst { Some(span) } else { None });
field_infos.clone().filter_map(
|(span, zst, zero_sized_array, _align)| {
if !zst && !zero_sized_array { Some(span) } else { None }
},
);
let non_zst_count = non_zst_fields.clone().count();
if non_zst_count >= 2 {
bad_non_zero_sized_fields(tcx, adt, non_zst_count, non_zst_fields, sp);
}
for (span, zst, align1) in field_infos {
if zst && !align1 {
struct_span_err!(

for (span, zst, zero_sized_array, align) in field_infos {
if (zst || zero_sized_array) && align != Some(1) {
let mut err = struct_span_err!(
tcx.sess,
span,
E0691,
"zero-sized field in transparent {} has alignment larger than 1",
adt.descr(),
)
.span_label(span, "has alignment larger than 1")
.emit();
);

if let Some(align) = align {
err.span_label(span, format!("has alignment of {align}, which is larger than 1"));
} else {
err.span_label(span, "may have alignment larger than 1");
};

if let Some(item_list) =
tcx.get_attr(adt.did(), sym::repr).and_then(|attr| attr.meta_item_list())
{
for item in item_list {
if item.name_or_empty() == sym::transparent {
err.span_suggestion_short(
item.span(),
"Try using `repr(C)` instead",
"C",
Applicability::MachineApplicable,
);
}
}
}

err.emit();
}
}
}
Expand Down Expand Up @@ -1475,7 +1507,7 @@ fn format_discriminant_overflow<'tcx>(
&& let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node
&& dis.val != *lit_value
{
return format!("`{dis}` (overflowed from `{lit_value}`)");
return format!("`{dis}` (overflowed from `{lit_value}`)");
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/repr/repr-transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct ZstAlign32<T>(PhantomData<T>);
#[repr(transparent)]
struct GenericAlign<T>(ZstAlign32<T>, u32); //~ ERROR alignment larger than 1

#[repr(transparent)]
struct GenericAlignZeroArray<T>([T; 0], u32); //~ ERROR alignment larger than 1

#[repr(transparent)] //~ ERROR unsupported representation for zero-variant enum
enum Void {} //~ ERROR transparent enum needs exactly one variant, but has 0

Expand Down Expand Up @@ -76,6 +79,11 @@ enum GenericAlignEnum<T> {
Foo { bar: ZstAlign32<T>, baz: u32 } //~ ERROR alignment larger than 1
}

#[repr(transparent)]
enum GenericAlignEnumZeroArray<T> {
Foo { bar: [T; 0], baz: u32 } //~ ERROR alignment larger than 1
}

#[repr(transparent)]
union UnitUnion {
u: (),
Expand Down
51 changes: 39 additions & 12 deletions src/test/ui/repr/repr-transparent.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,43 @@ LL | pub struct StructWithProjection(f32, <f32 as Mirror>::It);
error[E0691]: zero-sized field in transparent struct has alignment larger than 1
--> $DIR/repr-transparent.rs:36:32
|
LL | #[repr(transparent)]
| ----------- help: Try using `repr(C)` instead
LL | struct NontrivialAlignZst(u32, [u16; 0]);
| ^^^^^^^^ has alignment larger than 1
| ^^^^^^^^ has alignment of 2, which is larger than 1

error[E0691]: zero-sized field in transparent struct has alignment larger than 1
--> $DIR/repr-transparent.rs:42:24
|
LL | #[repr(transparent)]
| ----------- help: Try using `repr(C)` instead
LL | struct GenericAlign<T>(ZstAlign32<T>, u32);
| ^^^^^^^^^^^^^ has alignment larger than 1
| ^^^^^^^^^^^^^ has alignment of 32, which is larger than 1

error[E0691]: zero-sized field in transparent struct has alignment larger than 1
--> $DIR/repr-transparent.rs:45:33
|
LL | #[repr(transparent)]
| ----------- help: Try using `repr(C)` instead
LL | struct GenericAlignZeroArray<T>([T; 0], u32);
| ^^^^^^ may have alignment larger than 1
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved

error[E0084]: unsupported representation for zero-variant enum
--> $DIR/repr-transparent.rs:44:1
--> $DIR/repr-transparent.rs:47:1
|
LL | #[repr(transparent)]
| ^^^^^^^^^^^^^^^^^^^^
LL | enum Void {}
| ------------ zero-variant enum

error[E0731]: transparent enum needs exactly one variant, but has 0
--> $DIR/repr-transparent.rs:45:1
--> $DIR/repr-transparent.rs:48:1
|
LL | enum Void {}
| ^^^^^^^^^ needs exactly one variant, but has 0

error[E0690]: the variant of a transparent enum needs at most one non-zero-sized field, but has 2
--> $DIR/repr-transparent.rs:58:1
--> $DIR/repr-transparent.rs:61:1
|
LL | enum TooManyFieldsEnum {
| ^^^^^^^^^^^^^^^^^^^^^^ needs at most one non-zero-sized field, but has 2
Expand All @@ -55,7 +67,7 @@ LL | Foo(u32, String),
| this field is non-zero-sized

error[E0731]: transparent enum needs exactly one variant, but has 2
--> $DIR/repr-transparent.rs:64:1
--> $DIR/repr-transparent.rs:67:1
|
LL | enum MultipleVariants {
| ^^^^^^^^^^^^^^^^^^^^^ needs exactly one variant, but has 2
Expand All @@ -65,19 +77,34 @@ LL | Bar,
| --- too many variants in `MultipleVariants`

error[E0691]: zero-sized field in transparent enum has alignment larger than 1
--> $DIR/repr-transparent.rs:71:14
--> $DIR/repr-transparent.rs:74:14
|
LL | #[repr(transparent)]
| ----------- help: Try using `repr(C)` instead
LL | enum NontrivialAlignZstEnum {
LL | Foo(u32, [u16; 0]),
| ^^^^^^^^ has alignment larger than 1
| ^^^^^^^^ has alignment of 2, which is larger than 1

error[E0691]: zero-sized field in transparent enum has alignment larger than 1
--> $DIR/repr-transparent.rs:76:11
--> $DIR/repr-transparent.rs:79:11
|
LL | #[repr(transparent)]
| ----------- help: Try using `repr(C)` instead
LL | enum GenericAlignEnum<T> {
LL | Foo { bar: ZstAlign32<T>, baz: u32 }
| ^^^^^^^^^^^^^^^^^^ has alignment larger than 1
| ^^^^^^^^^^^^^^^^^^ has alignment of 32, which is larger than 1

error[E0691]: zero-sized field in transparent enum has alignment larger than 1
--> $DIR/repr-transparent.rs:84:11
|
LL | #[repr(transparent)]
| ----------- help: Try using `repr(C)` instead
LL | enum GenericAlignEnumZeroArray<T> {
LL | Foo { bar: [T; 0], baz: u32 }
| ^^^^^^^^^^^ may have alignment larger than 1

error[E0690]: transparent union needs at most one non-zero-sized field, but has 2
--> $DIR/repr-transparent.rs:85:1
--> $DIR/repr-transparent.rs:93:1
|
LL | union TooManyFields {
| ^^^^^^^^^^^^^^^^^^^ needs at most one non-zero-sized field, but has 2
Expand All @@ -86,7 +113,7 @@ LL | u: u32,
LL | s: i32
| ------ this field is non-zero-sized

error: aborting due to 11 previous errors
error: aborting due to 13 previous errors

Some errors have detailed explanations: E0084, E0690, E0691, E0731.
For more information about an error, try `rustc --explain E0084`.