From b95e97cc181347f42291b7d04db46b9fa520b553 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Mon, 18 May 2026 02:02:27 +0300 Subject: [PATCH] Support named consts in range pattern types --- crates/hir-def/src/expr_store/lower.rs | 25 +++++++++++++++++++++++-- crates/hir-ty/src/layout/tests.rs | 10 ++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/hir-def/src/expr_store/lower.rs b/crates/hir-def/src/expr_store/lower.rs index 48ccc1c0aa7f..7fd635c89add 100644 --- a/crates/hir-def/src/expr_store/lower.rs +++ b/crates/hir-def/src/expr_store/lower.rs @@ -2968,12 +2968,33 @@ impl<'db> ExprCollector<'db> { } fn lower_ty_pat_range_side(&mut self, pat: ast::Pat) -> ExprId { + let ptr = AstPtr::new(&pat); match &pat { ast::Pat::LiteralPat(it) => { let Some((literal, _)) = pat_literal_to_hir(it) else { return self.missing_expr() }; - self.alloc_expr_from_pat(Expr::Literal(literal), AstPtr::new(&pat)) + self.alloc_expr_from_pat(Expr::Literal(literal), ptr) + } + ast::Pat::ConstBlockPat(it) => { + if let Some(block) = it.block_expr() { + let expr_id = self.with_label_rib(RibKind::Constant, |this| { + this.with_binding_owner(|this| this.collect_block(block)) + }); + self.alloc_expr_from_pat(Expr::Const(expr_id), ptr) + } else { + self.missing_expr() + } + } + ast::Pat::PathPat(it) => { + let path = it + .path() + .and_then(|path| self.lower_path(path, &mut Self::impl_trait_error_allocator)); + self.alloc_expr_from_pat(path.map(Expr::Path).unwrap_or(Expr::Missing), ptr) + } + ast::Pat::IdentPat(it) if it.is_simple_ident() => { + let name = it.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); + self.alloc_expr_from_pat(Expr::Path(name.into()), ptr) } - _ => self.missing_expr(), + _ => self.missing_expr(), // FIXME: Emit an error. } } diff --git a/crates/hir-ty/src/layout/tests.rs b/crates/hir-ty/src/layout/tests.rs index 482945f5f0aa..b42ac54f130a 100644 --- a/crates/hir-ty/src/layout/tests.rs +++ b/crates/hir-ty/src/layout/tests.rs @@ -182,6 +182,7 @@ fn check_fail(#[rust_analyzer::rust_fixture] ra_fixture: &str, e: LayoutError) { assert_eq!(r, Err(e)); } +#[rust_analyzer::macro_style(braces)] macro_rules! size_and_align { (minicore: $($x:tt),*;$($t:tt)*) => { { @@ -535,6 +536,15 @@ fn non_zero_and_non_null() { use core::{num::NonZeroU8, ptr::NonNull}; struct Goal(Option, Option>); } + check_size_and_align( + r#" +const END: usize = 10; +struct Goal(core::pattern_type!(usize is 0..=END)); + "#, + "//- minicore: pat\n", + 8, + 8, + ); } #[test]