Skip to content

Commit 9bdedbb

Browse files
Merge #4497
4497: Create LowerCtx on the fly r=matklad a=edwin0cheng Previously we create `LowerCtx` at the beginning of lowering, however, the hygiene content is in fact changing between macro expression expanding. This PR change it to create the `LowerCtx` on the fly to fix above bug. However, #4465 is not fixed by this PR, the goto-def is still not work yet. It only fixed the infer part. Co-authored-by: Edwin Cheng <[email protected]>
2 parents ad03e4d + 12a3bf3 commit 9bdedbb

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

crates/ra_hir_def/src/body/lower.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,10 @@ pub(super) fn lower(
6060
params: Option<ast::ParamList>,
6161
body: Option<ast::Expr>,
6262
) -> (Body, BodySourceMap) {
63-
let ctx = LowerCtx::new(db, expander.current_file_id.clone());
64-
6563
ExprCollector {
6664
db,
6765
def,
6866
expander,
69-
ctx,
7067
source_map: BodySourceMap::default(),
7168
body: Body {
7269
exprs: Arena::default(),
@@ -83,7 +80,6 @@ struct ExprCollector<'a> {
8380
db: &'a dyn DefDatabase,
8481
def: DefWithBodyId,
8582
expander: Expander,
86-
ctx: LowerCtx,
8783
body: Body,
8884
source_map: BodySourceMap,
8985
}
@@ -122,6 +118,10 @@ impl ExprCollector<'_> {
122118
(self.body, self.source_map)
123119
}
124120

121+
fn ctx(&self) -> LowerCtx {
122+
LowerCtx::new(self.db, self.expander.current_file_id)
123+
}
124+
125125
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
126126
let src = self.expander.to_source(ptr);
127127
let id = self.make_expr(expr, Ok(src.clone()));
@@ -268,7 +268,7 @@ impl ExprCollector<'_> {
268268
};
269269
let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
270270
let generic_args =
271-
e.type_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx, it));
271+
e.type_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it));
272272
self.alloc_expr(
273273
Expr::MethodCall { receiver, method_name, args, generic_args },
274274
syntax_ptr,
@@ -373,7 +373,7 @@ impl ExprCollector<'_> {
373373
}
374374
ast::Expr::CastExpr(e) => {
375375
let expr = self.collect_expr_opt(e.expr());
376-
let type_ref = TypeRef::from_ast_opt(&self.ctx, e.type_ref());
376+
let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.type_ref());
377377
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
378378
}
379379
ast::Expr::RefExpr(e) => {
@@ -396,15 +396,15 @@ impl ExprCollector<'_> {
396396
for param in pl.params() {
397397
let pat = self.collect_pat_opt(param.pat());
398398
let type_ref =
399-
param.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx, it));
399+
param.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx(), it));
400400
args.push(pat);
401401
arg_types.push(type_ref);
402402
}
403403
}
404404
let ret_type = e
405405
.ret_type()
406406
.and_then(|r| r.type_ref())
407-
.map(|it| TypeRef::from_ast(&self.ctx, it));
407+
.map(|it| TypeRef::from_ast(&self.ctx(), it));
408408
let body = self.collect_expr_opt(e.body());
409409
self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
410410
}
@@ -507,7 +507,8 @@ impl ExprCollector<'_> {
507507
.map(|s| match s {
508508
ast::Stmt::LetStmt(stmt) => {
509509
let pat = self.collect_pat_opt(stmt.pat());
510-
let type_ref = stmt.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx, it));
510+
let type_ref =
511+
stmt.ascribed_type().map(|it| TypeRef::from_ast(&self.ctx(), it));
511512
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
512513
Statement::Let { pat, type_ref, initializer }
513514
}

crates/ra_hir_ty/src/tests/regression.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,37 @@ fn main() {
563563
);
564564
}
565565

566+
#[test]
567+
fn issue_4465_dollar_crate_at_type() {
568+
assert_snapshot!(
569+
infer(r#"
570+
pub struct Foo {}
571+
pub fn anything<T>() -> T {
572+
loop {}
573+
}
574+
macro_rules! foo {
575+
() => {{
576+
let r: $crate::Foo = anything();
577+
r
578+
}};
579+
}
580+
fn main() {
581+
let _a = foo!();
582+
}
583+
"#), @r###"
584+
45..60 '{ loop {} }': T
585+
51..58 'loop {}': !
586+
56..58 '{}': ()
587+
!0..31 '{letr:...g();r}': Foo
588+
!4..5 'r': Foo
589+
!18..26 'anything': fn anything<Foo>() -> Foo
590+
!18..28 'anything()': Foo
591+
!29..30 'r': Foo
592+
164..188 '{ ...!(); }': ()
593+
174..176 '_a': Foo
594+
"###);
595+
}
596+
566597
#[test]
567598
fn issue_4053_diesel_where_clauses() {
568599
assert_snapshot!(

0 commit comments

Comments
 (0)