Don't complain on a single non-exhaustive 1-zst

This commit is contained in:
Michael Goulet 2023-09-18 06:59:11 +00:00
parent 19dd953540
commit fd36553aa7
3 changed files with 110 additions and 20 deletions

View file

@ -1201,25 +1201,35 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
); );
return; return;
} }
for (span, _trivial, non_exhaustive) in field_infos { let mut prev_non_exhaustive_1zst = false;
if let Some((descr, def_id, args, non_exhaustive)) = non_exhaustive { for (span, _trivial, non_exhaustive_1zst) in field_infos {
tcx.struct_span_lint_hir( if let Some((descr, def_id, args, non_exhaustive)) = non_exhaustive_1zst {
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, // If there are any non-trivial fields, then there can be no non-exhaustive 1-zsts.
tcx.hir().local_def_id_to_hir_id(adt.did().expect_local()), // Otherwise, it's only an issue if there's >1 non-exhaustive 1-zst.
span, if non_trivial_count > 0 || prev_non_exhaustive_1zst {
"zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types", tcx.struct_span_lint_hir(
|lint| { REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
let note = if non_exhaustive { tcx.hir().local_def_id_to_hir_id(adt.did().expect_local()),
"is marked with `#[non_exhaustive]`" span,
} else { "zero-sized fields in `repr(transparent)` cannot \
"contains private fields" contain external non-exhaustive types",
}; |lint| {
let field_ty = tcx.def_path_str_with_args(def_id, args); let note = if non_exhaustive {
lint "is marked with `#[non_exhaustive]`"
.note(format!("this {descr} contains `{field_ty}`, which {note}, \ } else {
and makes it not a breaking change to become non-zero-sized in the future.")) "contains private fields"
}, };
) let field_ty = tcx.def_path_str_with_args(def_id, args);
lint.note(format!(
"this {descr} contains `{field_ty}`, which {note}, \
and makes it not a breaking change to become \
non-zero-sized in the future."
))
},
)
} else {
prev_non_exhaustive_1zst = true;
}
} }
} }
} }

View file

@ -93,4 +93,44 @@ pub struct T16(Sized, ExternalIndirection<NonExhaustiveVariant>);
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types //~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
//~| WARN this was previously accepted by the compiler //~| WARN this was previously accepted by the compiler
#[repr(transparent)]
pub struct T17(NonExhaustive, Sized);
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
//~| WARN this was previously accepted by the compiler
#[repr(transparent)]
pub struct T18(NonExhaustive, NonExhaustive);
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
//~| WARN this was previously accepted by the compiler
#[repr(transparent)]
pub struct T19(NonExhaustive, Private);
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
//~| WARN this was previously accepted by the compiler
#[repr(transparent)]
pub struct T19Flipped(Private, NonExhaustive);
//~^ ERROR zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
//~| WARN this was previously accepted by the compiler
#[repr(transparent)]
pub struct T20(NonExhaustive);
// Okay, since it's the only field.
#[repr(transparent)]
pub struct T21(NonExhaustive, InternalNonExhaustive);
// Okay, since there's only 1 foreign non-exhaustive type.
#[repr(transparent)]
pub struct T21Flipped(InternalNonExhaustive, NonExhaustive);
// Okay, since there's only 1 foreign non-exhaustive type.
#[repr(transparent)]
pub struct T22(NonExhaustive, InternalPrivate);
// Okay, since there's only 1 foreign non-exhaustive type.
#[repr(transparent)]
pub struct T22Flipped(InternalPrivate, NonExhaustive);
// Okay, since there's only 1 foreign non-exhaustive type.
fn main() {} fn main() {}

View file

@ -123,5 +123,45 @@ LL | pub struct T16(Sized, ExternalIndirection<NonExhaustiveVariant>);
= note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586> = note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
= note: this enum contains `NonExhaustiveVariant`, which is marked with `#[non_exhaustive]`, and makes it not a breaking change to become non-zero-sized in the future. = note: this enum contains `NonExhaustiveVariant`, which is marked with `#[non_exhaustive]`, and makes it not a breaking change to become non-zero-sized in the future.
error: aborting due to 12 previous errors error: zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
--> $DIR/repr-transparent-non-exhaustive.rs:97:16
|
LL | pub struct T17(NonExhaustive, Sized);
| ^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
= note: this struct contains `NonExhaustive`, which is marked with `#[non_exhaustive]`, and makes it not a breaking change to become non-zero-sized in the future.
error: zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
--> $DIR/repr-transparent-non-exhaustive.rs:102:31
|
LL | pub struct T18(NonExhaustive, NonExhaustive);
| ^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
= note: this struct contains `NonExhaustive`, which is marked with `#[non_exhaustive]`, and makes it not a breaking change to become non-zero-sized in the future.
error: zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
--> $DIR/repr-transparent-non-exhaustive.rs:107:31
|
LL | pub struct T19(NonExhaustive, Private);
| ^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
= note: this struct contains `Private`, which contains private fields, and makes it not a breaking change to become non-zero-sized in the future.
error: zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types
--> $DIR/repr-transparent-non-exhaustive.rs:112:32
|
LL | pub struct T19Flipped(Private, NonExhaustive);
| ^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
= note: this struct contains `NonExhaustive`, which is marked with `#[non_exhaustive]`, and makes it not a breaking change to become non-zero-sized in the future.
error: aborting due to 16 previous errors