Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22932 from shulaoda/07-27-fix_don_t_pick_a_discriminant_type_larger_than_typeck_s
fix: don't pick a discriminant type larger than typeck's
Chayim Refael Friedman 13 days ago
parent 0a032f2 · parent 96e8a5e · commit 8f6a809
-rw-r--r--crates/hir-ty/src/layout/adt.rs4
-rw-r--r--crates/hir-ty/src/layout/tests.rs7
2 files changed, 9 insertions, 2 deletions
diff --git a/crates/hir-ty/src/layout/adt.rs b/crates/hir-ty/src/layout/adt.rs
index 1f321af794..777d0803fc 100644
--- a/crates/hir-ty/src/layout/adt.rs
+++ b/crates/hir-ty/src/layout/adt.rs
@@ -142,8 +142,8 @@ fn repr_discr(
Integer::I8
};
- // If there are no negative values, we can use the unsigned fit.
- Ok(if min >= 0 {
+ // `min` and `max` are the ends of a wrapping range, so their sign is not a usable test.
+ Ok(if unsigned_fit <= signed_fit {
(cmp::max(unsigned_fit, at_least), false)
} else {
(cmp::max(signed_fit, at_least), true)
diff --git a/crates/hir-ty/src/layout/tests.rs b/crates/hir-ty/src/layout/tests.rs
index b9ee38c44f..b5db24e98b 100644
--- a/crates/hir-ty/src/layout/tests.rs
+++ b/crates/hir-ty/src/layout/tests.rs
@@ -610,6 +610,13 @@ fn enums_with_discriminants() {
A = 1, // This one is (perhaps surprisingly) zero sized.
}
}
+ size_and_align! {
+ #[allow(overflowing_literals, clippy::enum_clike_unportable_variant)]
+ enum Goal {
+ A = 0,
+ B = 0x8000_0000_0000_0001, // Wraps around to a negative discriminant.
+ }
+ }
}
#[test]