Skip to content

Commit b95e97c

Browse files
Support named consts in range pattern types
1 parent ce81cf6 commit b95e97c

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

crates/hir-def/src/expr_store/lower.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,12 +2968,33 @@ impl<'db> ExprCollector<'db> {
29682968
}
29692969

29702970
fn lower_ty_pat_range_side(&mut self, pat: ast::Pat) -> ExprId {
2971+
let ptr = AstPtr::new(&pat);
29712972
match &pat {
29722973
ast::Pat::LiteralPat(it) => {
29732974
let Some((literal, _)) = pat_literal_to_hir(it) else { return self.missing_expr() };
2974-
self.alloc_expr_from_pat(Expr::Literal(literal), AstPtr::new(&pat))
2975+
self.alloc_expr_from_pat(Expr::Literal(literal), ptr)
2976+
}
2977+
ast::Pat::ConstBlockPat(it) => {
2978+
if let Some(block) = it.block_expr() {
2979+
let expr_id = self.with_label_rib(RibKind::Constant, |this| {
2980+
this.with_binding_owner(|this| this.collect_block(block))
2981+
});
2982+
self.alloc_expr_from_pat(Expr::Const(expr_id), ptr)
2983+
} else {
2984+
self.missing_expr()
2985+
}
2986+
}
2987+
ast::Pat::PathPat(it) => {
2988+
let path = it
2989+
.path()
2990+
.and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator));
2991+
self.alloc_expr_from_pat(path.map(Expr::Path).unwrap_or(Expr::Missing), ptr)
2992+
}
2993+
ast::Pat::IdentPat(it) if it.is_simple_ident() => {
2994+
let name = it.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
2995+
self.alloc_expr_from_pat(Expr::Path(name.into()), ptr)
29752996
}
2976-
_ => self.missing_expr(),
2997+
_ => self.missing_expr(), // FIXME: Emit an error.
29772998
}
29782999
}
29793000

crates/hir-ty/src/layout/tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ fn check_fail(#[rust_analyzer::rust_fixture] ra_fixture: &str, e: LayoutError) {
182182
assert_eq!(r, Err(e));
183183
}
184184

185+
#[rust_analyzer::macro_style(braces)]
185186
macro_rules! size_and_align {
186187
(minicore: $($x:tt),*;$($t:tt)*) => {
187188
{
@@ -535,6 +536,15 @@ fn non_zero_and_non_null() {
535536
use core::{num::NonZeroU8, ptr::NonNull};
536537
struct Goal(Option<NonZeroU8>, Option<NonNull<i32>>);
537538
}
539+
check_size_and_align(
540+
r#"
541+
const END: usize = 10;
542+
struct Goal(core::pattern_type!(usize is 0..=END));
543+
"#,
544+
"//- minicore: pat\n",
545+
8,
546+
8,
547+
);
538548
}
539549

540550
#[test]

0 commit comments

Comments
 (0)