Unnamed repository; edit this file 'description' to name the repository.
resolve_bind_pat_to_const does not early return if expr
Ali Bektas 2025-02-09
parent 574ea02 · commit 18f90a9
-rw-r--r--crates/hir-def/src/expr_store/tests.rs112
-rw-r--r--crates/hir/src/source_analyzer.rs15
2 files changed, 28 insertions, 99 deletions
diff --git a/crates/hir-def/src/expr_store/tests.rs b/crates/hir-def/src/expr_store/tests.rs
index 73eed74471..b84043af68 100644
--- a/crates/hir-def/src/expr_store/tests.rs
+++ b/crates/hir-def/src/expr_store/tests.rs
@@ -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()]);
- }
- _ => {}
- }
-}
-
-#[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,
- }
-}"#,
- );
+ let hir_start = &body.exprs[start.unwrap()];
+ let hir_end = &body.exprs[end.unwrap()];
- 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");
- }
- }
- _ => {}
+ assert!(matches!(hir_start, Expr::Path { .. }));
+ assert!(matches!(hir_end, Expr::Path { .. }));
}
- }
-}
-
-#[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");
- }
- }
- _ => {}
- }
- }
}
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index 06fcebad65..8a1e4fba6b 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -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()?] {
- Pat::Path(path) => path,
- _ => return None,
+
+ 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),