Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ee6b5cf

Browse files
committed
Make Pat::Range's start and end Option<ExprId>
1 parent 3c83458 commit ee6b5cf

File tree

6 files changed

+48
-26
lines changed

6 files changed

+48
-26
lines changed

src/tools/rust-analyzer/crates/hir-def/src/expr_store.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ pub struct ExpressionStoreSourceMap {
112112
// AST expressions can create patterns in destructuring assignments. Therefore, `ExprSource` can also map
113113
// to `PatId`, and `PatId` can also map to `ExprSource` (the other way around is unaffected).
114114
expr_map: FxHashMap<ExprSource, ExprOrPatId>,
115-
expr_map_back: ArenaMap<ExprId, ExprSource>,
115+
expr_map_back: ArenaMap<ExprId, ExprOrPatSource>,
116116

117-
pat_map: FxHashMap<PatSource, PatId>,
117+
pat_map: FxHashMap<PatSource, ExprOrPatId>,
118118
pat_map_back: ArenaMap<PatId, ExprOrPatSource>,
119119

120120
label_map: FxHashMap<LabelSource, LabelId>,
@@ -606,12 +606,12 @@ impl Index<TypeRefId> for ExpressionStore {
606606
impl ExpressionStoreSourceMap {
607607
pub fn expr_or_pat_syntax(&self, id: ExprOrPatId) -> Result<ExprOrPatSource, SyntheticSyntax> {
608608
match id {
609-
ExprOrPatId::ExprId(id) => self.expr_syntax(id).map(|it| it.map(AstPtr::wrap_left)),
609+
ExprOrPatId::ExprId(id) => self.expr_syntax(id),
610610
ExprOrPatId::PatId(id) => self.pat_syntax(id),
611611
}
612612
}
613613

614-
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> {
614+
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprOrPatSource, SyntheticSyntax> {
615615
self.expr_map_back.get(expr).cloned().ok_or(SyntheticSyntax)
616616
}
617617

@@ -633,7 +633,7 @@ impl ExpressionStoreSourceMap {
633633
self.pat_map_back.get(pat).cloned().ok_or(SyntheticSyntax)
634634
}
635635

636-
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
636+
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<ExprOrPatId> {
637637
self.pat_map.get(&node.map(AstPtr::new)).cloned()
638638
}
639639

src/tools/rust-analyzer/crates/hir-def/src/expr_store/lower.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,23 +1784,32 @@ impl ExprCollector<'_> {
17841784
self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
17851785
this.collect_pat_opt(expanded_pat, binding_list)
17861786
});
1787-
self.source_map.pat_map.insert(src, pat);
1787+
self.source_map.pat_map.insert(src, pat.into());
17881788
return pat;
17891789
}
17901790
None => Pat::Missing,
17911791
},
17921792
// FIXME: implement in a way that also builds source map and calculates assoc resolutions in type inference.
17931793
ast::Pat::RangePat(p) => {
1794-
let mut range_part_lower = |p: Option<ast::Pat>| {
1795-
p.and_then(|it| match &it {
1796-
ast::Pat::LiteralPat(it) => {
1797-
Some(Box::new(LiteralOrConst::Literal(pat_literal_to_hir(it)?.0)))
1798-
}
1799-
pat @ (ast::Pat::IdentPat(_) | ast::Pat::PathPat(_)) => {
1800-
let subpat = self.collect_pat(pat.clone(), binding_list);
1801-
Some(Box::new(LiteralOrConst::Const(subpat)))
1794+
let mut range_part_lower = |p: Option<ast::Pat>| -> Option<ExprId> {
1795+
p.and_then(|it| {
1796+
let ptr = PatPtr::new(&it);
1797+
match &it {
1798+
ast::Pat::LiteralPat(it) => {
1799+
// Some(Box::new(LiteralOrConst::Literal(pat_literal_to_hir(it)?.0)))
1800+
Some(self.alloc_expr_from_pat(
1801+
Expr::Literal(pat_literal_to_hir(it)?.0),
1802+
ptr,
1803+
))
1804+
}
1805+
pat @ (ast::Pat::IdentPat(_) | ast::Pat::PathPat(_)) => {
1806+
// let subpat = self.collect_pat(pat.clone(), binding_list);
1807+
// Some(Box::new(LiteralOrConst::Const(subpat)))
1808+
// TODO
1809+
Some(self.missing_expr())
1810+
}
1811+
_ => None,
18021812
}
1803-
_ => None,
18041813
})
18051814
};
18061815
let start = range_part_lower(p.start());
@@ -1863,7 +1872,7 @@ impl ExprCollector<'_> {
18631872
}
18641873
});
18651874
if let Some(pat) = pat.left() {
1866-
self.source_map.pat_map.insert(src, pat);
1875+
self.source_map.pat_map.insert(src, pat.into());
18671876
}
18681877
pat
18691878
}
@@ -2490,7 +2499,7 @@ impl ExprCollector<'_> {
24902499
fn alloc_expr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
24912500
let src = self.expander.in_file(ptr);
24922501
let id = self.store.exprs.alloc(expr);
2493-
self.source_map.expr_map_back.insert(id, src);
2502+
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_left));
24942503
self.source_map.expr_map.insert(src, id.into());
24952504
id
24962505
}
@@ -2502,7 +2511,7 @@ impl ExprCollector<'_> {
25022511
fn alloc_expr_desugared_with_ptr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
25032512
let src = self.expander.in_file(ptr);
25042513
let id = self.store.exprs.alloc(expr);
2505-
self.source_map.expr_map_back.insert(id, src);
2514+
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_left));
25062515
// We intentionally don't fill this as it could overwrite a non-desugared entry
25072516
// self.source_map.expr_map.insert(src, id);
25082517
id
@@ -2526,11 +2535,20 @@ impl ExprCollector<'_> {
25262535
self.source_map.pat_map_back.insert(id, src.map(AstPtr::wrap_left));
25272536
id
25282537
}
2538+
2539+
fn alloc_expr_from_pat(&mut self, expr: Expr, ptr: PatPtr) -> ExprId {
2540+
let src = self.expander.in_file(ptr);
2541+
let id = self.body.exprs.alloc(expr);
2542+
self.source_map.pat_map.insert(src, id.into());
2543+
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_right));
2544+
id
2545+
}
2546+
25292547
fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
25302548
let src = self.expander.in_file(ptr);
25312549
let id = self.store.pats.alloc(pat);
25322550
self.source_map.pat_map_back.insert(id, src.map(AstPtr::wrap_right));
2533-
self.source_map.pat_map.insert(src, id);
2551+
self.source_map.pat_map.insert(src, id.into());
25342552
id
25352553
}
25362554
// FIXME: desugared pats don't have ptr, that's wrong and should be fixed somehow.

src/tools/rust-analyzer/crates/hir-def/src/expr_store/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,11 @@ impl Printer<'_> {
656656
}
657657
Pat::Range { start, end } => {
658658
if let Some(start) = start {
659-
self.print_literal_or_const(start);
659+
self.print_expr(*start);
660660
}
661661
w!(self, "..=");
662662
if let Some(end) = end {
663-
self.print_literal_or_const(end);
663+
self.print_expr(*end);
664664
}
665665
}
666666
Pat::Slice { prefix, slice, suffix } => {

src/tools/rust-analyzer/crates/hir-def/src/hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ pub enum Pat {
571571
ellipsis: bool,
572572
},
573573
Range {
574-
start: Option<Box<LiteralOrConst>>,
575-
end: Option<Box<LiteralOrConst>>,
574+
start: Option<ExprId>,
575+
end: Option<ExprId>,
576576
},
577577
Slice {
578578
prefix: Box<[PatId]>,

src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ impl ExprValidator {
440440
return;
441441
};
442442
let root = source_ptr.file_syntax(db.upcast());
443-
let ast::Expr::IfExpr(if_expr) = source_ptr.value.to_node(&root) else {
443+
let either::Left(ast::Expr::IfExpr(if_expr)) =
444+
source_ptr.value.to_node(&root)
445+
else {
444446
return;
445447
};
446448
let mut top_if_expr = if_expr;

src/tools/rust-analyzer/crates/hir-ty/src/mir/lower/pattern_matching.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,12 @@ impl MirLowerCtx<'_> {
234234
};
235235
if mode == MatchingMode::Check {
236236
if let Some(start) = start {
237-
add_check(start, BinOp::Le)?;
237+
// TODO
238+
// add_check(start, BinOp::Le)?;
238239
}
239240
if let Some(end) = end {
240-
add_check(end, BinOp::Ge)?;
241+
// TODO
242+
// add_check(end, BinOp::Ge)?;
241243
}
242244
}
243245
(current, current_else)

0 commit comments

Comments
 (0)