33
44use either:: Either ;
55use hir_expand:: {
6+ hygiene:: Hygiene ,
67 name:: { name, AsName , Name } ,
7- MacroDefId , MacroDefKind ,
8+ HirFileId , MacroDefId , MacroDefKind ,
89} ;
910use ra_arena:: Arena ;
1011use ra_syntax:: {
@@ -26,7 +27,7 @@ use crate::{
2627 LogicOp , MatchArm , Ordering , Pat , PatId , RecordFieldPat , RecordLitField , Statement ,
2728 } ,
2829 item_scope:: BuiltinShadowMode ,
29- path:: GenericArgs ,
30+ path:: { GenericArgs , Path } ,
3031 type_ref:: { Mutability , TypeRef } ,
3132 AdtId , ConstLoc , ContainerId , DefWithBodyId , EnumLoc , FunctionLoc , Intern , ModuleDefId ,
3233 StaticLoc , StructLoc , TraitLoc , TypeAliasLoc , UnionLoc ,
@@ -35,17 +36,37 @@ use crate::{
3536use super :: { ExprSource , PatSource } ;
3637use ast:: AstChildren ;
3738
39+ pub ( crate ) struct LowerCtx {
40+ hygiene : Hygiene ,
41+ }
42+
43+ impl LowerCtx {
44+ pub fn new ( db : & dyn DefDatabase , file_id : HirFileId ) -> Self {
45+ LowerCtx { hygiene : Hygiene :: new ( db. upcast ( ) , file_id) }
46+ }
47+ pub fn with_hygiene ( hygiene : & Hygiene ) -> Self {
48+ LowerCtx { hygiene : hygiene. clone ( ) }
49+ }
50+
51+ pub fn lower_path ( & self , ast : ast:: Path ) -> Option < Path > {
52+ Path :: from_src ( ast, & self . hygiene )
53+ }
54+ }
55+
3856pub ( super ) fn lower (
3957 db : & dyn DefDatabase ,
4058 def : DefWithBodyId ,
4159 expander : Expander ,
4260 params : Option < ast:: ParamList > ,
4361 body : Option < ast:: Expr > ,
4462) -> ( Body , BodySourceMap ) {
63+ let ctx = LowerCtx :: new ( db, expander. current_file_id . clone ( ) ) ;
64+
4565 ExprCollector {
4666 db,
4767 def,
4868 expander,
69+ ctx,
4970 source_map : BodySourceMap :: default ( ) ,
5071 body : Body {
5172 exprs : Arena :: default ( ) ,
@@ -62,7 +83,7 @@ struct ExprCollector<'a> {
6283 db : & ' a dyn DefDatabase ,
6384 def : DefWithBodyId ,
6485 expander : Expander ,
65-
86+ ctx : LowerCtx ,
6687 body : Body ,
6788 source_map : BodySourceMap ,
6889}
@@ -237,7 +258,8 @@ impl ExprCollector<'_> {
237258 Vec :: new ( )
238259 } ;
239260 let method_name = e. name_ref ( ) . map ( |nr| nr. as_name ( ) ) . unwrap_or_else ( Name :: missing) ;
240- let generic_args = e. type_arg_list ( ) . and_then ( GenericArgs :: from_ast) ;
261+ let generic_args =
262+ e. type_arg_list ( ) . and_then ( |it| GenericArgs :: from_ast ( & self . ctx , it) ) ;
241263 self . alloc_expr (
242264 Expr :: MethodCall { receiver, method_name, args, generic_args } ,
243265 syntax_ptr,
@@ -343,7 +365,7 @@ impl ExprCollector<'_> {
343365 }
344366 ast:: Expr :: CastExpr ( e) => {
345367 let expr = self . collect_expr_opt ( e. expr ( ) ) ;
346- let type_ref = TypeRef :: from_ast_opt ( e. type_ref ( ) ) ;
368+ let type_ref = TypeRef :: from_ast_opt ( & self . ctx , e. type_ref ( ) ) ;
347369 self . alloc_expr ( Expr :: Cast { expr, type_ref } , syntax_ptr)
348370 }
349371 ast:: Expr :: RefExpr ( e) => {
@@ -365,12 +387,16 @@ impl ExprCollector<'_> {
365387 if let Some ( pl) = e. param_list ( ) {
366388 for param in pl. params ( ) {
367389 let pat = self . collect_pat_opt ( param. pat ( ) ) ;
368- let type_ref = param. ascribed_type ( ) . map ( TypeRef :: from_ast) ;
390+ let type_ref =
391+ param. ascribed_type ( ) . map ( |it| TypeRef :: from_ast ( & self . ctx , it) ) ;
369392 args. push ( pat) ;
370393 arg_types. push ( type_ref) ;
371394 }
372395 }
373- let ret_type = e. ret_type ( ) . and_then ( |r| r. type_ref ( ) ) . map ( TypeRef :: from_ast) ;
396+ let ret_type = e
397+ . ret_type ( )
398+ . and_then ( |r| r. type_ref ( ) )
399+ . map ( |it| TypeRef :: from_ast ( & self . ctx , it) ) ;
374400 let body = self . collect_expr_opt ( e. body ( ) ) ;
375401 self . alloc_expr ( Expr :: Lambda { args, arg_types, ret_type, body } , syntax_ptr)
376402 }
@@ -476,7 +502,7 @@ impl ExprCollector<'_> {
476502 . map ( |s| match s {
477503 ast:: Stmt :: LetStmt ( stmt) => {
478504 let pat = self . collect_pat_opt ( stmt. pat ( ) ) ;
479- let type_ref = stmt. ascribed_type ( ) . map ( TypeRef :: from_ast) ;
505+ let type_ref = stmt. ascribed_type ( ) . map ( |it| TypeRef :: from_ast ( & self . ctx , it ) ) ;
480506 let initializer = stmt. initializer ( ) . map ( |e| self . collect_expr ( e) ) ;
481507 Statement :: Let { pat, type_ref, initializer }
482508 }
0 commit comments