Skip to content

Commit 3e9633b

Browse files
committed
resolve_bind_pat_to_const does not early return if expr
1 parent e402c8c commit 3e9633b

File tree

2 files changed

+28
-99
lines changed

2 files changed

+28
-99
lines changed

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

Lines changed: 16 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod block;
22

3-
use crate::{test_db::TestDB, ModuleDefId};
3+
use crate::{hir::MatchArm, test_db::TestDB, ModuleDefId};
44
use expect_test::{expect, Expect};
55
use la_arena::RawIdx;
66
use test_fixture::WithFixture;
@@ -459,124 +459,44 @@ async fn foo(a: (), b: i32) -> u32 {
459459
.assert_eq(&printed);
460460
}
461461

462-
fn test1() {
463-
let (db, body, owner) = lower(
462+
#[test]
463+
fn range_bounds_are_hir_exprs() {
464+
let (_, body, _) = lower(
464465
r#"
465466
pub const L: i32 = 6;
466467
mod x {
467468
pub const R: i32 = 100;
468469
}
469470
const fn f(x: i32) -> i32 {
470471
match x {
471-
L..=x::R => x * 100,
472472
-1..=5 => x * 10,
473+
L..=x::R => x * 100,
473474
_ => x,
474475
}
475476
}"#,
476477
);
477478

478-
let pat = body
479-
.pats
479+
let mtch_arms = body
480+
.exprs
480481
.iter()
481-
.find_map(|pat| {
482-
if let Pat::Range { .. } = pat.1 {
483-
return Some(pat.1);
482+
.find_map(|(_, expr)| {
483+
if let Expr::Match { arms, .. } = expr {
484+
return Some(arms);
484485
}
485486

486487
None
487488
})
488489
.unwrap();
489490

490-
match pat {
491+
let MatchArm { pat, .. } = mtch_arms[1];
492+
match body.pats[pat] {
491493
Pat::Range { start, end } => {
492-
dbg!(&body.exprs[start.unwrap()]);
493-
dbg!(&body.exprs[end.unwrap()]);
494-
}
495-
_ => {}
496-
}
497-
}
498-
499-
#[test]
500-
fn test2() {
501-
let (db, body, owner) = lower(
502-
r#"
503-
pub const L: i32 = 6;
504-
mod x {
505-
pub const R: i32 = 100;
506-
}
507-
const fn f(x: i32) -> i32 {
508-
match x {
509-
-1..=5 => x * 10,
510-
::std::i32::MIN..=x::R => x * 100,
511-
_ => x,
512-
}
513-
}"#,
514-
);
494+
let hir_start = &body.exprs[start.unwrap()];
495+
let hir_end = &body.exprs[end.unwrap()];
515496

516-
for (pat_id, pat) in body.pats.iter() {
517-
match pat {
518-
Pat::Range { start, end } => {
519-
let pretty = body.pretty_print_pat(&db, owner, pat_id, false, Edition::Edition2021);
520-
eprintln!("RANGE {}", pretty);
521-
522-
if let Some(start) = start {
523-
eprintln!("START");
524-
let expr = body.exprs[*start].clone();
525-
dbg!(expr);
526-
} else {
527-
eprintln!("START is None");
528-
}
529-
530-
if let Some(end) = end {
531-
eprintln!("END");
532-
let expr = body.exprs[*end].clone();
533-
dbg!(expr);
534-
} else {
535-
eprintln!("END is None");
536-
}
537-
}
538-
_ => {}
497+
assert!(matches!(hir_start, Expr::Path { .. }));
498+
assert!(matches!(hir_end, Expr::Path { .. }));
539499
}
540-
}
541-
}
542-
543-
#[test]
544-
fn test3() {
545-
let (db, body, owner) = lower(
546-
r#"
547-
const A: u32 = 0;
548-
549-
fn bar(v: u32) {
550-
match v {
551-
0..=A => {}
552500
_ => {}
553501
}
554-
}"#,
555-
);
556-
557-
for (pat_id, pat) in body.pats.iter() {
558-
match pat {
559-
Pat::Range { start, end } => {
560-
let pretty = body.pretty_print_pat(&db, owner, pat_id, false, Edition::Edition2021);
561-
eprintln!("RANGE {}", pretty);
562-
563-
if let Some(start) = start {
564-
eprintln!("START");
565-
let expr = body.exprs[*start].clone();
566-
dbg!(expr);
567-
} else {
568-
eprintln!("START is None");
569-
}
570-
571-
if let Some(end) = end {
572-
eprintln!("END");
573-
let expr = body.exprs[*end].clone();
574-
dbg!(expr);
575-
} else {
576-
eprintln!("END is None");
577-
}
578-
}
579-
_ => {}
580-
}
581-
}
582502
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,19 @@ impl SourceAnalyzer {
686686
) -> Option<ModuleDef> {
687687
let pat_id = self.pat_id(&pat.clone().into())?;
688688
let body = self.body()?;
689-
let path = match &body[pat_id.as_pat()?] {
690-
Pat::Path(path) => path,
691-
_ => return None,
689+
690+
let path = if pat_id.is_pat() {
691+
match &body[pat_id.as_pat()?] {
692+
Pat::Path(path) => path,
693+
_ => return None,
694+
}
695+
} else {
696+
match &body[pat_id.as_expr()?] {
697+
Expr::Path(path) => path,
698+
_ => return None,
699+
}
692700
};
701+
693702
let res = resolve_hir_path(db, &self.resolver, path, HygieneId::ROOT, TypesMap::EMPTY)?;
694703
match res {
695704
PathResolution::Def(def) => Some(def),

0 commit comments

Comments
 (0)