Skip to content

Commit 050c63a

Browse files
committed
Lower ast::Ident to hir::Path when lowering RangePats
1 parent 8a18dce commit 050c63a

File tree

9 files changed

+139
-58
lines changed

9 files changed

+139
-58
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,24 +1795,27 @@ impl ExprCollector<'_> {
17951795
p.and_then(|it| {
17961796
let ptr = PatPtr::new(&it);
17971797
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-
ast::Pat::IdentPat(_) => Some(self.missing_expr()),
1806-
ast::Pat::PathPat(p) => {
1807-
if let Some(path) = p.path() {
1808-
if let Some(parsed) = self.parse_path(path) {
1809-
return Some(
1810-
self.alloc_expr_from_pat(Expr::Path(parsed), ptr),
1811-
);
1812-
}
1798+
ast::Pat::LiteralPat(it) => Some(self.alloc_expr_from_pat(
1799+
Expr::Literal(pat_literal_to_hir(it)?.0),
1800+
ptr,
1801+
)),
1802+
ast::Pat::IdentPat(ident) => {
1803+
if ident.is_simple_ident() {
1804+
return ident
1805+
.name()
1806+
.and_then(|name| Some(name.as_name()))
1807+
.and_then(|hir_name| Some(Path::from(hir_name)))
1808+
.and_then(|path| {
1809+
Some(self.alloc_expr_from_pat(Expr::Path(path), ptr))
1810+
});
18131811
}
1814-
Some(self.missing_expr())
1812+
1813+
None
18151814
}
1815+
ast::Pat::PathPat(p) => p
1816+
.path()
1817+
.and_then(|path| self.parse_path(path))
1818+
.map(|parsed| self.alloc_expr_from_pat(Expr::Path(parsed), ptr)),
18161819
_ => None,
18171820
}
18181821
})

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use itertools::Itertools;
66
use span::Edition;
77

88
use crate::{
9-
hir::{
10-
Array, BindingAnnotation, CaptureBy, ClosureKind, Literal, LiteralOrConst, Movability,
11-
Statement,
12-
},
9+
hir::{Array, BindingAnnotation, CaptureBy, ClosureKind, Literal, Movability, Statement},
1310
pretty::{print_generic_args, print_path, print_type_ref},
1411
};
1512

@@ -757,13 +754,6 @@ impl Printer<'_> {
757754
}
758755
}
759756

760-
fn print_literal_or_const(&mut self, literal_or_const: &LiteralOrConst) {
761-
match literal_or_const {
762-
LiteralOrConst::Literal(l) => self.print_literal(l),
763-
LiteralOrConst::Const(c) => self.print_pat(*c),
764-
}
765-
}
766-
767757
fn print_literal(&mut self, literal: &Literal) {
768758
match literal {
769759
Literal::String(it) => w!(self, "{:?}", it),

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

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod block;
22

3+
use base_db::Upcast;
34
use expect_test::{expect, Expect};
45
use la_arena::RawIdx;
56
use test_fixture::WithFixture;
7+
use tracing::Instrument;
68

7-
use crate::{test_db::TestDB, ModuleDefId};
9+
use crate::{db::InternDatabase, test_db::TestDB, ModuleDefId};
810

911
use super::*;
1012

@@ -446,7 +448,6 @@ fn foo() {
446448
);
447449
}
448450

449-
#[test]
450451
fn skip_skips_body() {
451452
let (db, body, owner) = lower(
452453
r#"
@@ -461,7 +462,7 @@ async fn foo(a: (), b: i32) -> u32 {
461462
.assert_eq(&printed);
462463
}
463464

464-
fn abc() {
465+
fn test1() {
465466
let (db, body, owner) = lower(
466467
r#"
467468
pub const L: i32 = 6;
@@ -470,8 +471,46 @@ mod x {
470471
}
471472
const fn f(x: i32) -> i32 {
472473
match x {
473-
-1..=5 => x * 10,
474474
L..=x::R => x * 100,
475+
-1..=5 => x * 10,
476+
_ => x,
477+
}
478+
}"#,
479+
);
480+
481+
let pat = body
482+
.pats
483+
.iter()
484+
.find_map(|pat| {
485+
if let Pat::Range { .. } = pat.1 {
486+
return Some(pat.1);
487+
}
488+
489+
None
490+
})
491+
.unwrap();
492+
493+
match pat {
494+
Pat::Range { start, end } => {
495+
dbg!(&body.exprs[start.unwrap()]);
496+
dbg!(&body.exprs[end.unwrap()]);
497+
}
498+
_ => {}
499+
}
500+
}
501+
502+
#[test]
503+
fn test2() {
504+
let (db, body, owner) = lower(
505+
r#"
506+
pub const L: i32 = 6;
507+
mod x {
508+
pub const R: i32 = 100;
509+
}
510+
const fn f(x: i32) -> i32 {
511+
match x {
512+
-1..=5 => x * 10,
513+
::std::i32::MIN..=x::R => x * 100,
475514
_ => x,
476515
}
477516
}"#,
@@ -503,3 +542,44 @@ const fn f(x: i32) -> i32 {
503542
}
504543
}
505544
}
545+
546+
#[test]
547+
fn test3() {
548+
let (db, body, owner) = lower(
549+
r#"
550+
const A: u32 = 0;
551+
552+
fn bar(v: u32) {
553+
match v {
554+
0..=A => {}
555+
_ => {}
556+
}
557+
}"#,
558+
);
559+
560+
for (pat_id, pat) in body.pats.iter() {
561+
match pat {
562+
Pat::Range { start, end } => {
563+
let pretty = body.pretty_print_pat(&db, owner, pat_id, false, Edition::Edition2021);
564+
eprintln!("RANGE {}", pretty);
565+
566+
if let Some(start) = start {
567+
eprintln!("START");
568+
let expr = body.exprs[*start].clone();
569+
dbg!(expr);
570+
} else {
571+
eprintln!("START is None");
572+
}
573+
574+
if let Some(end) = end {
575+
eprintln!("END");
576+
let expr = body.exprs[*end].clone();
577+
dbg!(expr);
578+
} else {
579+
eprintln!("END is None");
580+
}
581+
}
582+
_ => {}
583+
}
584+
}
585+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,26 @@ impl ExprOrPatId {
5555
}
5656
}
5757

58+
pub fn is_expr(&self) -> bool {
59+
match self {
60+
Self::ExprId(_) => true,
61+
_ => false,
62+
}
63+
}
64+
5865
pub fn as_pat(self) -> Option<PatId> {
5966
match self {
6067
Self::PatId(v) => Some(v),
6168
_ => None,
6269
}
6370
}
71+
72+
pub fn is_pat(&self) -> bool {
73+
match self {
74+
Self::PatId(_) => true,
75+
_ => false,
76+
}
77+
}
6478
}
6579
stdx::impl_from!(ExprId, PatId for ExprOrPatId);
6680

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use hir_def::{
88
data::adt::{StructKind, VariantData},
99
expr_store::{Body, HygieneId},
1010
hir::{
11-
ArithOp, Array, BinaryOp, BindingAnnotation, BindingId, ExprId, LabelId, Literal,
12-
LiteralOrConst, MatchArm, Pat, PatId, RecordFieldPat, RecordLitField,
11+
ArithOp, Array, BinaryOp, BindingAnnotation, BindingId, ExprId, LabelId, Literal, MatchArm,
12+
Pat, PatId, RecordFieldPat, RecordLitField,
1313
},
1414
lang_item::{LangItem, LangItemTarget},
1515
path::Path,
@@ -1358,28 +1358,18 @@ impl<'ctx> MirLowerCtx<'ctx> {
13581358
Ok(())
13591359
}
13601360

1361-
fn lower_literal_or_const_to_operand(
1362-
&mut self,
1363-
ty: Ty,
1364-
loc: &LiteralOrConst,
1365-
) -> Result<Operand> {
1366-
match loc {
1367-
LiteralOrConst::Literal(l) => self.lower_literal_to_operand(ty, l),
1368-
LiteralOrConst::Const(c) => {
1369-
let c = match &self.body.pats[*c] {
1370-
Pat::Path(p) => p,
1371-
_ => not_supported!(
1372-
"only `char` and numeric types are allowed in range patterns"
1373-
),
1374-
};
1361+
fn lower_literal_or_const_to_operand(&mut self, ty: Ty, loc: &ExprId) -> Result<Operand> {
1362+
match dbg!(&self.body.exprs[*loc]) {
1363+
Expr::Literal(l) => self.lower_literal_to_operand(ty, l),
1364+
Expr::Path(c) => {
13751365
let edition = self.edition();
13761366
let unresolved_name =
13771367
|| MirLowerError::unresolved_path(self.db, c, edition, &self.body.types);
13781368
let pr = self
13791369
.resolver
13801370
.resolve_path_in_value_ns(self.db.upcast(), c, HygieneId::ROOT)
13811371
.ok_or_else(unresolved_name)?;
1382-
match pr {
1372+
dbg!(match dbg!(pr) {
13831373
ResolveValueResult::ValueNs(v, _) => {
13841374
if let ValueNs::ConstId(c) = v {
13851375
self.lower_const_to_operand(Substitution::empty(Interner), c.into(), ty)
@@ -1390,7 +1380,10 @@ impl<'ctx> MirLowerCtx<'ctx> {
13901380
ResolveValueResult::Partial(_, _, _) => {
13911381
not_supported!("associated constants in range pattern")
13921382
}
1393-
}
1383+
})
1384+
}
1385+
_ => {
1386+
not_supported!("only `char` and numeric types are allowed in range patterns");
13941387
}
13951388
}
13961389
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! MIR lowering for patterns
22
3-
use hir_def::{hir::LiteralOrConst, AssocItemId};
3+
use hir_def::{hir::ExprId, AssocItemId};
44

55
use crate::{
66
mir::{
@@ -207,7 +207,7 @@ impl MirLowerCtx<'_> {
207207
)?
208208
}
209209
Pat::Range { start, end } => {
210-
let mut add_check = |l: &LiteralOrConst, binop| -> Result<()> {
210+
let mut add_check = |l: &ExprId, binop| -> Result<()> {
211211
let lv =
212212
self.lower_literal_or_const_to_operand(self.infer[pattern].clone(), l)?;
213213
let else_target = *current_else.get_or_insert_with(|| self.new_basic_block());
@@ -234,12 +234,10 @@ impl MirLowerCtx<'_> {
234234
};
235235
if mode == MatchingMode::Check {
236236
if let Some(start) = start {
237-
// TODO
238-
// add_check(start, BinOp::Le)?;
237+
add_check(start, BinOp::Le)?;
239238
}
240239
if let Some(end) = end {
241-
// TODO
242-
// add_check(end, BinOp::Ge)?;
240+
add_check(end, BinOp::Ge)?;
243241
}
244242
}
245243
(current, current_else)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ impl<'db> SemanticsImpl<'db> {
16401640
}
16411641

16421642
pub fn to_def<T: ToDef>(&self, src: &T) -> Option<T::Def> {
1643+
dbg!(std::any::type_name::<T>());
16431644
let src = self.find_file(src.syntax()).with_value(src);
16441645
T::to_def(self, src)
16451646
}

src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl SourceToDefCtx<'_, '_> {
347347
&mut self,
348348
src: InFile<&ast::IdentPat>,
349349
) -> Option<(DefWithBodyId, BindingId)> {
350-
let container = self.find_pat_or_label_container(src.syntax_ref())?;
350+
let container = dbg!(self.find_pat_or_label_container(src.syntax_ref()))?;
351351
let (body, source_map) = self.db.body_with_source_map(container);
352352
let src = src.cloned().map(ast::Pat::from);
353353
let pat_id = source_map.node_pat(src.as_ref())?;

src/tools/rust-analyzer/crates/ide-db/src/defs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use hir::{
2020
};
2121
use span::Edition;
2222
use stdx::{format_to, impl_from};
23+
use syntax::ToSmolStr;
2324
use syntax::{
2425
ast::{self, AstNode},
2526
match_ast, SyntaxKind, SyntaxNode, SyntaxToken,
@@ -365,6 +366,7 @@ impl IdentClass {
365366
sema: &Semantics<'_, RootDatabase>,
366367
node: &SyntaxNode,
367368
) -> Option<IdentClass> {
369+
dbg!(&node.to_smolstr());
368370
match_ast! {
369371
match node {
370372
ast::Name(name) => NameClass::classify(sema, &name).map(IdentClass::NameClass),
@@ -521,7 +523,7 @@ impl NameClass {
521523
let definition = match_ast! {
522524
match parent {
523525
ast::Item(it) => classify_item(sema, it)?,
524-
ast::IdentPat(it) => return classify_ident_pat(sema, it),
526+
ast::IdentPat(it) => return dbg!(classify_ident_pat(sema, it)),
525527
ast::Rename(it) => classify_rename(sema, it)?,
526528
ast::SelfParam(it) => Definition::Local(sema.to_def(&it)?),
527529
ast::RecordField(it) => Definition::Field(sema.to_def(&it)?),
@@ -574,7 +576,7 @@ impl NameClass {
574576
return Some(NameClass::ConstReference(Definition::from(def)));
575577
}
576578

577-
let local = sema.to_def(&ident_pat)?;
579+
let local = dbg!(sema.to_def(&ident_pat))?;
578580
let pat_parent = ident_pat.syntax().parent();
579581
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast) {
580582
if record_pat_field.name_ref().is_none() {

0 commit comments

Comments
 (0)