aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2020-05-17 16:37:30 +0100
committerEdwin Cheng <[email protected]>2020-05-17 16:37:30 +0100
commit12a3bf3c31d4c9a6d9ee110db174604f688ca0f0 (patch)
tree951a9cffac26108963a42f31c44739aa3700d8c2 /crates
parentebaa05a4478096aaf3bc2a48d0d171a287422c7c (diff)
Create LowerCtx on the fly
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/body/lower.rs19
-rw-r--r--crates/ra_hir_ty/src/tests/regression.rs31
2 files changed, 41 insertions, 9 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 443b057ab..c69e0efea 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -60,13 +60,10 @@ pub(super) fn lower(
60 params: Option<ast::ParamList>, 60 params: Option<ast::ParamList>,
61 body: Option<ast::Expr>, 61 body: Option<ast::Expr>,
62) -> (Body, BodySourceMap) { 62) -> (Body, BodySourceMap) {
63 let ctx = LowerCtx::new(db, expander.current_file_id.clone());
64
65 ExprCollector { 63 ExprCollector {
66 db, 64 db,
67 def, 65 def,
68 expander, 66 expander,
69 ctx,
70 source_map: BodySourceMap::default(), 67 source_map: BodySourceMap::default(),
71 body: Body { 68 body: Body {
72 exprs: Arena::default(), 69 exprs: Arena::default(),
@@ -83,7 +80,6 @@ struct ExprCollector<'a> {
83 db: &'a dyn DefDatabase, 80 db: &'a dyn DefDatabase,
84 def: DefWithBodyId, 81 def: DefWithBodyId,
85 expander: Expander, 82 expander: Expander,
86 ctx: LowerCtx,
87 body: Body, 83 body: Body,
88 source_map: BodySourceMap, 84 source_map: BodySourceMap,
89} 85}
@@ -122,6 +118,10 @@ impl ExprCollector<'_> {
122 (self.body, self.source_map) 118 (self.body, self.source_map)
123 } 119 }
124 120
121 fn ctx(&self) -> LowerCtx {
122 LowerCtx::new(self.db, self.expander.current_file_id)
123 }
124
125 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { 125 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
126 let src = self.expander.to_source(ptr); 126 let src = self.expander.to_source(ptr);
127 let id = self.make_expr(expr, Ok(src.clone())); 127 let id = self.make_expr(expr, Ok(src.clone()));
@@ -268,7 +268,7 @@ impl ExprCollector<'_> {
268 }; 268 };
269 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); 269 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
270 let generic_args = 270 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));
272 self.alloc_expr( 272 self.alloc_expr(
273 Expr::MethodCall { receiver, method_name, args, generic_args }, 273 Expr::MethodCall { receiver, method_name, args, generic_args },
274 syntax_ptr, 274 syntax_ptr,
@@ -373,7 +373,7 @@ impl ExprCollector<'_> {
373 } 373 }
374 ast::Expr::CastExpr(e) => { 374 ast::Expr::CastExpr(e) => {
375 let expr = self.collect_expr_opt(e.expr()); 375 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());
377 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) 377 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
378 } 378 }
379 ast::Expr::RefExpr(e) => { 379 ast::Expr::RefExpr(e) => {
@@ -396,7 +396,7 @@ impl ExprCollector<'_> {
396 for param in pl.params() { 396 for param in pl.params() {
397 let pat = self.collect_pat_opt(param.pat()); 397 let pat = self.collect_pat_opt(param.pat());
398 let type_ref = 398 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));
400 args.push(pat); 400 args.push(pat);
401 arg_types.push(type_ref); 401 arg_types.push(type_ref);
402 } 402 }
@@ -404,7 +404,7 @@ impl ExprCollector<'_> {
404 let ret_type = e 404 let ret_type = e
405 .ret_type() 405 .ret_type()
406 .and_then(|r| r.type_ref()) 406 .and_then(|r| r.type_ref())
407 .map(|it| TypeRef::from_ast(&self.ctx, it)); 407 .map(|it| TypeRef::from_ast(&self.ctx(), it));
408 let body = self.collect_expr_opt(e.body()); 408 let body = self.collect_expr_opt(e.body());
409 self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) 409 self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
410 } 410 }
@@ -507,7 +507,8 @@ impl ExprCollector<'_> {
507 .map(|s| match s { 507 .map(|s| match s {
508 ast::Stmt::LetStmt(stmt) => { 508 ast::Stmt::LetStmt(stmt) => {
509 let pat = self.collect_pat_opt(stmt.pat()); 509 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));
511 let initializer = stmt.initializer().map(|e| self.collect_expr(e)); 512 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
512 Statement::Let { pat, type_ref, initializer } 513 Statement::Let { pat, type_ref, initializer }
513 } 514 }
diff --git a/crates/ra_hir_ty/src/tests/regression.rs b/crates/ra_hir_ty/src/tests/regression.rs
index 115ad8328..c2168222e 100644
--- a/crates/ra_hir_ty/src/tests/regression.rs
+++ b/crates/ra_hir_ty/src/tests/regression.rs
@@ -564,6 +564,37 @@ fn main() {
564} 564}
565 565
566#[test] 566#[test]
567fn issue_4465_dollar_crate_at_type() {
568 assert_snapshot!(
569 infer(r#"
570pub struct Foo {}
571pub fn anything<T>() -> T {
572 loop {}
573}
574macro_rules! foo {
575 () => {{
576 let r: $crate::Foo = anything();
577 r
578 }};
579}
580fn 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
597#[test]
567fn issue_4053_diesel_where_clauses() { 598fn issue_4053_diesel_where_clauses() {
568 assert_snapshot!( 599 assert_snapshot!(
569 infer(r#" 600 infer(r#"