22
33use crate :: helpers:: mod_path_to_ast;
44use either:: Either ;
5- use hir:: { AsAssocItem , HirDisplay , ImportPathConfig , ModuleDef , SemanticsScope } ;
5+ use hir:: {
6+ AsAssocItem , HirDisplay , HirFileId , ImportPathConfig , ModuleDef , SemanticsScope ,
7+ prettify_macro_expansion,
8+ } ;
69use itertools:: Itertools ;
710use rustc_hash:: FxHashMap ;
811use span:: Edition ;
@@ -136,6 +139,25 @@ impl<'a> PathTransform<'a> {
136139 }
137140 }
138141
142+ fn prettify_target_node ( & self , node : SyntaxNode ) -> SyntaxNode {
143+ match self . target_scope . file_id ( ) {
144+ HirFileId :: FileId ( _) => node,
145+ HirFileId :: MacroFile ( file_id) => {
146+ let db = self . target_scope . db ;
147+ prettify_macro_expansion (
148+ db,
149+ node,
150+ & db. expansion_span_map ( file_id) ,
151+ self . target_scope . module ( ) . krate ( ) . into ( ) ,
152+ )
153+ }
154+ }
155+ }
156+
157+ fn prettify_target_ast < N : AstNode > ( & self , node : N ) -> N {
158+ N :: cast ( self . prettify_target_node ( node. syntax ( ) . clone ( ) ) ) . unwrap ( )
159+ }
160+
139161 fn build_ctx ( & self ) -> Ctx < ' a > {
140162 let db = self . source_scope . db ;
141163 let target_module = self . target_scope . module ( ) ;
@@ -163,7 +185,7 @@ impl<'a> PathTransform<'a> {
163185 . for_each ( |( k, v) | match ( k. split ( db) , v) {
164186 ( Either :: Right ( k) , Some ( TypeOrConst :: Either ( v) ) ) => {
165187 if let Some ( ty) = v. ty ( ) {
166- type_substs. insert ( k, ty ) ;
188+ type_substs. insert ( k, self . prettify_target_ast ( ty ) ) ;
167189 }
168190 }
169191 ( Either :: Right ( k) , None ) => {
@@ -178,7 +200,7 @@ impl<'a> PathTransform<'a> {
178200 }
179201 ( Either :: Left ( k) , Some ( TypeOrConst :: Either ( v) ) ) => {
180202 if let Some ( ty) = v. ty ( ) {
181- const_substs. insert ( k, ty. syntax ( ) . clone ( ) ) ;
203+ const_substs. insert ( k, self . prettify_target_node ( ty. syntax ( ) . clone ( ) ) ) ;
182204 }
183205 }
184206 ( Either :: Left ( k) , Some ( TypeOrConst :: Const ( v) ) ) => {
@@ -189,7 +211,7 @@ impl<'a> PathTransform<'a> {
189211 // and sometimes require slight modifications; see
190212 // https://doc.rust-lang.org/reference/statements.html#expression-statements
191213 // (default values in curly brackets can cause the same problem)
192- const_substs. insert ( k, expr. syntax ( ) . clone ( ) ) ;
214+ const_substs. insert ( k, self . prettify_target_node ( expr. syntax ( ) . clone ( ) ) ) ;
193215 }
194216 }
195217 ( Either :: Left ( k) , None ) => {
@@ -204,6 +226,7 @@ impl<'a> PathTransform<'a> {
204226 }
205227 _ => ( ) , // ignore mismatching params
206228 } ) ;
229+ // No need to prettify lifetimes, there's nothing to prettify.
207230 let lifetime_substs: FxHashMap < _ , _ > = self
208231 . generic_def
209232 . into_iter ( )
0 commit comments