resolve_bind_pat_to_const does not early return if expr

This commit is contained in:
Ali Bektas 2025-01-21 19:21:41 +01:00
parent 574ea0296c
commit 18f90a9d2a
2 changed files with 29 additions and 100 deletions

View File

@ -1,6 +1,6 @@
mod block;
use crate::{test_db::TestDB, ModuleDefId};
use crate::{hir::MatchArm, test_db::TestDB, ModuleDefId};
use expect_test::{expect, Expect};
use la_arena::RawIdx;
use test_fixture::WithFixture;
@ -459,8 +459,9 @@ async fn foo(a: (), b: i32) -> u32 {
.assert_eq(&printed);
}
fn test1() {
let (db, body, owner) = lower(
#[test]
fn range_bounds_are_hir_exprs() {
let (_, body, _) = lower(
r#"
pub const L: i32 = 6;
mod x {
@ -468,115 +469,34 @@ mod x {
}
const fn f(x: i32) -> i32 {
match x {
L..=x::R => x * 100,
-1..=5 => x * 10,
L..=x::R => x * 100,
_ => x,
}
}"#,
);
let pat = body
.pats
let mtch_arms = body
.exprs
.iter()
.find_map(|pat| {
if let Pat::Range { .. } = pat.1 {
return Some(pat.1);
.find_map(|(_, expr)| {
if let Expr::Match { arms, .. } = expr {
return Some(arms);
}
None
})
.unwrap();
match pat {
let MatchArm { pat, .. } = mtch_arms[1];
match body.pats[pat] {
Pat::Range { start, end } => {
dbg!(&body.exprs[start.unwrap()]);
dbg!(&body.exprs[end.unwrap()]);
let hir_start = &body.exprs[start.unwrap()];
let hir_end = &body.exprs[end.unwrap()];
assert!(matches!(hir_start, Expr::Path { .. }));
assert!(matches!(hir_end, Expr::Path { .. }));
}
_ => {}
}
}
#[test]
fn test2() {
let (db, body, owner) = lower(
r#"
pub const L: i32 = 6;
mod x {
pub const R: i32 = 100;
}
const fn f(x: i32) -> i32 {
match x {
-1..=5 => x * 10,
::std::i32::MIN..=x::R => x * 100,
_ => x,
}
}"#,
);
for (pat_id, pat) in body.pats.iter() {
match pat {
Pat::Range { start, end } => {
let pretty = body.pretty_print_pat(&db, owner, pat_id, false, Edition::Edition2021);
eprintln!("RANGE {}", pretty);
if let Some(start) = start {
eprintln!("START");
let expr = body.exprs[*start].clone();
dbg!(expr);
} else {
eprintln!("START is None");
}
if let Some(end) = end {
eprintln!("END");
let expr = body.exprs[*end].clone();
dbg!(expr);
} else {
eprintln!("END is None");
}
}
_ => {}
}
}
}
#[test]
fn test3() {
let (db, body, owner) = lower(
r#"
const A: u32 = 0;
fn bar(v: u32) {
match v {
0..=A => {}
_ => {}
}
}"#,
);
for (pat_id, pat) in body.pats.iter() {
match pat {
Pat::Range { start, end } => {
let pretty = body.pretty_print_pat(&db, owner, pat_id, false, Edition::Edition2021);
eprintln!("RANGE {}", pretty);
if let Some(start) = start {
eprintln!("START");
let expr = body.exprs[*start].clone();
dbg!(expr);
} else {
eprintln!("START is None");
}
if let Some(end) = end {
eprintln!("END");
let expr = body.exprs[*end].clone();
dbg!(expr);
} else {
eprintln!("END is None");
}
}
_ => {}
}
}
}

View File

@ -686,10 +686,19 @@ impl SourceAnalyzer {
) -> Option<ModuleDef> {
let pat_id = self.pat_id(&pat.clone().into())?;
let body = self.body()?;
let path = match &body[pat_id.as_pat()?] {
let path = if pat_id.is_pat() {
match &body[pat_id.as_pat()?] {
Pat::Path(path) => path,
_ => return None,
}
} else {
match &body[pat_id.as_expr()?] {
Expr::Path(path) => path,
_ => return None,
}
};
let res = resolve_hir_path(db, &self.resolver, path, HygieneId::ROOT, TypesMap::EMPTY)?;
match res {
PathResolution::Def(def) => Some(def),