Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22524 from ChayimFriedman2/pat-ty-or
fix: Parse OR pattern types
Lukas Wirth 14 days ago
parent a0a5c97 · parent 52f1cb3 · commit 6c0c0d7
-rw-r--r--crates/hir-ty/src/layout.rs8
-rw-r--r--crates/hir-ty/src/layout/tests.rs14
-rw-r--r--crates/parser/src/grammar/types.rs4
-rw-r--r--crates/test-utils/src/minicore.rs12
4 files changed, 28 insertions, 10 deletions
diff --git a/crates/hir-ty/src/layout.rs b/crates/hir-ty/src/layout.rs
index 2b0a93e61d..59f1d6e575 100644
--- a/crates/hir-ty/src/layout.rs
+++ b/crates/hir-ty/src/layout.rs
@@ -418,17 +418,15 @@ pub fn layout_of_ty_query(
.iter()
.map(|pat| match pat.kind() {
PatternKind::Range { start, end } => Ok::<_, LayoutError>((
- extract_const_value(start)
- .unwrap()
+ extract_const_value(start)?
.try_to_bits(db, trait_env.as_ref())
.ok_or(LayoutError::Unknown)?,
- extract_const_value(end)
- .unwrap()
+ extract_const_value(end)?
.try_to_bits(db, trait_env.as_ref())
.ok_or(LayoutError::Unknown)?,
)),
PatternKind::NotNull | PatternKind::Or(_) => {
- unreachable!("mixed or patterns are not allowed")
+ Err(LayoutError::Unknown)
}
})
.collect();
diff --git a/crates/hir-ty/src/layout/tests.rs b/crates/hir-ty/src/layout/tests.rs
index afbafb3fea..b9ee38c44f 100644
--- a/crates/hir-ty/src/layout/tests.rs
+++ b/crates/hir-ty/src/layout/tests.rs
@@ -535,13 +535,21 @@ fn non_zero_and_non_null() {
}
check_size_and_align(
r#"
-const END: usize = 10;
-struct Goal(core::pattern_type!(usize is 0..=END));
- "#,
+ const END: usize = 10;
+ struct Goal(core::pattern_type!(usize is 0..=END));
+ "#,
"//- minicore: pat\n",
8,
8,
);
+ check_size_and_align(
+ r#"
+pub struct Goal(core::pattern_type!(i32 is ..0 | 1..));
+ "#,
+ "//- minicore: pat\n",
+ 4,
+ 4,
+ );
}
#[test]
diff --git a/crates/parser/src/grammar/types.rs b/crates/parser/src/grammar/types.rs
index f92afde60d..db0185331c 100644
--- a/crates/parser/src/grammar/types.rs
+++ b/crates/parser/src/grammar/types.rs
@@ -1,4 +1,4 @@
-use crate::grammar::entry::prefix::pat;
+use crate::grammar::entry::prefix::pat_top;
use super::*;
@@ -430,7 +430,7 @@ fn pattern_type(p: &mut Parser<'_>) {
if !p.eat_contextual_kw(T![is]) {
p.error("expected `is`")
}
- pat(p);
+ pat_top(p);
p.expect(T![')']);
m.complete(p, PATTERN_TYPE);
} else {
diff --git a/crates/test-utils/src/minicore.rs b/crates/test-utils/src/minicore.rs
index c5ebc0d248..0588b1c77d 100644
--- a/crates/test-utils/src/minicore.rs
+++ b/crates/test-utils/src/minicore.rs
@@ -2353,6 +2353,18 @@ pub mod pat {
}
}
+ impl const RangePattern for i32 {
+ const MIN: i32 = 0x80_00_00_00;
+ const MAX: i32 = 0x7F_FF_FF_FF;
+ fn sub_one(self) -> Self {
+ if self == Self::MIN {
+ panic!("exclusive range end at minimum value of type")
+ } else {
+ self - 1
+ }
+ }
+ }
+
// region:coerce_unsized
impl<T: crate::marker::PointeeSized, U: crate::marker::PointeeSized>
crate::ops::CoerceUnsized<pattern_type!(*const U is !null)> for pattern_type!(*const T is !null)