Lower ast::Ident to hir::Path when lowering RangePats

This commit is contained in:
Ali Bektas 2025-01-19 23:58:49 +01:00
parent 94b00c210c
commit 8956b1e1ba
9 changed files with 139 additions and 58 deletions

View file

@ -1795,24 +1795,27 @@ impl ExprCollector<'_> {
p.and_then(|it| {
let ptr = PatPtr::new(&it);
match &it {
ast::Pat::LiteralPat(it) => {
// Some(Box::new(LiteralOrConst::Literal(pat_literal_to_hir(it)?.0)))
Some(self.alloc_expr_from_pat(
Expr::Literal(pat_literal_to_hir(it)?.0),
ptr,
))
}
ast::Pat::IdentPat(_) => Some(self.missing_expr()),
ast::Pat::PathPat(p) => {
if let Some(path) = p.path() {
if let Some(parsed) = self.parse_path(path) {
return Some(
self.alloc_expr_from_pat(Expr::Path(parsed), ptr),
);
}
ast::Pat::LiteralPat(it) => Some(self.alloc_expr_from_pat(
Expr::Literal(pat_literal_to_hir(it)?.0),
ptr,
)),
ast::Pat::IdentPat(ident) => {
if ident.is_simple_ident() {
return ident
.name()
.and_then(|name| Some(name.as_name()))
.and_then(|hir_name| Some(Path::from(hir_name)))
.and_then(|path| {
Some(self.alloc_expr_from_pat(Expr::Path(path), ptr))
});
}
Some(self.missing_expr())
None
}
ast::Pat::PathPat(p) => p
.path()
.and_then(|path| self.parse_path(path))
.map(|parsed| self.alloc_expr_from_pat(Expr::Path(parsed), ptr)),
_ => None,
}
})

View file

@ -6,10 +6,7 @@ use itertools::Itertools;
use span::Edition;
use crate::{
hir::{
Array, BindingAnnotation, CaptureBy, ClosureKind, Literal, LiteralOrConst, Movability,
Statement,
},
hir::{Array, BindingAnnotation, CaptureBy, ClosureKind, Literal, Movability, Statement},
pretty::{print_generic_args, print_path, print_type_ref},
};
@ -757,13 +754,6 @@ impl Printer<'_> {
}
}
fn print_literal_or_const(&mut self, literal_or_const: &LiteralOrConst) {
match literal_or_const {
LiteralOrConst::Literal(l) => self.print_literal(l),
LiteralOrConst::Const(c) => self.print_pat(*c),
}
}
fn print_literal(&mut self, literal: &Literal) {
match literal {
Literal::String(it) => w!(self, "{:?}", it),

View file

@ -1,10 +1,12 @@
mod block;
use base_db::Upcast;
use expect_test::{expect, Expect};
use la_arena::RawIdx;
use test_fixture::WithFixture;
use tracing::Instrument;
use crate::{test_db::TestDB, ModuleDefId};
use crate::{db::InternDatabase, test_db::TestDB, ModuleDefId};
use super::*;
@ -446,7 +448,6 @@ fn foo() {
);
}
#[test]
fn skip_skips_body() {
let (db, body, owner) = lower(
r#"
@ -461,7 +462,45 @@ async fn foo(a: (), b: i32) -> u32 {
.assert_eq(&printed);
}
fn abc() {
fn test1() {
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 {
L..=x::R => x * 100,
-1..=5 => x * 10,
_ => x,
}
}"#,
);
let pat = body
.pats
.iter()
.find_map(|pat| {
if let Pat::Range { .. } = pat.1 {
return Some(pat.1);
}
None
})
.unwrap();
match 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;
@ -471,7 +510,7 @@ mod x {
const fn f(x: i32) -> i32 {
match x {
-1..=5 => x * 10,
L..=x::R => x * 100,
::std::i32::MIN..=x::R => x * 100,
_ => x,
}
}"#,
@ -503,3 +542,44 @@ const fn f(x: i32) -> i32 {
}
}
}
#[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

@ -55,12 +55,26 @@ impl ExprOrPatId {
}
}
pub fn is_expr(&self) -> bool {
match self {
Self::ExprId(_) => true,
_ => false,
}
}
pub fn as_pat(self) -> Option<PatId> {
match self {
Self::PatId(v) => Some(v),
_ => None,
}
}
pub fn is_pat(&self) -> bool {
match self {
Self::PatId(_) => true,
_ => false,
}
}
}
stdx::impl_from!(ExprId, PatId for ExprOrPatId);