about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-06-24 07:29:59 +0000
committerbors <bors@rust-lang.org>2021-06-24 07:29:59 +0000
commit456a03227e3c81a51631f87ec80cac301e5fa6d7 (patch)
treeb31d83d758e546f49317b091a366df589c498cd6 /compiler
parent964a81eb37db6ee33b8fc107582618bf2befe02d (diff)
parent4e755a96a74b7d79a8796f78a194efca5d6d649b (diff)
downloadrust-456a03227e3c81a51631f87ec80cac301e5fa6d7.tar.gz
rust-456a03227e3c81a51631f87ec80cac301e5fa6d7.zip
Auto merge of #86279 - JohnTitor:transparent-zero-size-fields, r=nikomatsakis
Permit zero non-zero-field on transparent types

Fixes #77841

This makes the transparent fields meet the below:
> * A `repr(transparent)` type `T` must meet the following rules:
>   * It may have any number of 1-ZST fields
>   * In addition, it may have at most one other field of type U

r? `@nikomatsakis`
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_error_codes/src/error_codes/E0690.md10
-rw-r--r--compiler/rustc_typeck/src/check/check.rs2
-rw-r--r--compiler/rustc_typeck/src/check/mod.rs6
3 files changed, 9 insertions, 9 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0690.md b/compiler/rustc_error_codes/src/error_codes/E0690.md
index 1673456580a..ba706ad2b02 100644
--- a/compiler/rustc_error_codes/src/error_codes/E0690.md
+++ b/compiler/rustc_error_codes/src/error_codes/E0690.md
@@ -1,19 +1,19 @@
-A struct with the representation hint `repr(transparent)` had zero or more than
-one fields that were not guaranteed to be zero-sized.
+A struct with the representation hint `repr(transparent)` had two or more fields
+that were not guaranteed to be zero-sized.
 
 Erroneous code example:
 
 ```compile_fail,E0690
 #[repr(transparent)]
-struct LengthWithUnit<U> { // error: transparent struct needs exactly one
+struct LengthWithUnit<U> { // error: transparent struct needs at most one
     value: f32,            //        non-zero-sized field, but has 2
     unit: U,
 }
 ```
 
 Because transparent structs are represented exactly like one of their fields at
-run time, said field must be uniquely determined. If there is no field, or if
-there are multiple fields, it is not clear how the struct should be represented.
+run time, said field must be uniquely determined. If there are multiple fields,
+it is not clear how the struct should be represented.
 Note that fields of zero-sized types (e.g., `PhantomData`) can also exist
 alongside the field that contains the actual data, they do not count for this
 error. When generic types are involved (as in the above example), an error is
diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs
index 70d85796d00..77644ea1c3c 100644
--- a/compiler/rustc_typeck/src/check/check.rs
+++ b/compiler/rustc_typeck/src/check/check.rs
@@ -1382,7 +1382,7 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: &'tcx ty
     let non_zst_fields =
         field_infos.clone().filter_map(|(span, zst, _align1)| if !zst { Some(span) } else { None });
     let non_zst_count = non_zst_fields.clone().count();
-    if non_zst_count != 1 {
+    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 {
diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs
index 8703949f488..b5dc7b750b1 100644
--- a/compiler/rustc_typeck/src/check/mod.rs
+++ b/compiler/rustc_typeck/src/check/mod.rs
@@ -1020,7 +1020,7 @@ fn suggestion_signature(assoc: &ty::AssocItem, tcx: TyCtxt<'_>) -> String {
     }
 }
 
-/// Emit an error when encountering more or less than one variant in a transparent enum.
+/// Emit an error when encountering two or more variants in a transparent enum.
 fn bad_variant_count<'tcx>(tcx: TyCtxt<'tcx>, adt: &'tcx ty::AdtDef, sp: Span, did: DefId) {
     let variant_spans: Vec<_> = adt
         .variants
@@ -1039,7 +1039,7 @@ fn bad_variant_count<'tcx>(tcx: TyCtxt<'tcx>, adt: &'tcx ty::AdtDef, sp: Span, d
     err.emit();
 }
 
-/// Emit an error when encountering more or less than one non-zero-sized field in a transparent
+/// Emit an error when encountering two or more non-zero-sized fields in a transparent
 /// enum.
 fn bad_non_zero_sized_fields<'tcx>(
     tcx: TyCtxt<'tcx>,
@@ -1048,7 +1048,7 @@ fn bad_non_zero_sized_fields<'tcx>(
     field_spans: impl Iterator<Item = Span>,
     sp: Span,
 ) {
-    let msg = format!("needs exactly one non-zero-sized field, but has {}", field_count);
+    let msg = format!("needs at most one non-zero-sized field, but has {}", field_count);
     let mut err = struct_span_err!(
         tcx.sess,
         sp,