@@ -11,7 +11,7 @@ use ra_ap_hir::{
11
11
} ;
12
12
use ra_ap_hir_def:: ModuleId ;
13
13
use ra_ap_hir_def:: type_ref:: Mutability ;
14
- use ra_ap_hir_expand:: ExpandTo ;
14
+ use ra_ap_hir_expand:: { ExpandResult , ExpandTo } ;
15
15
use ra_ap_ide_db:: RootDatabase ;
16
16
use ra_ap_ide_db:: line_index:: { LineCol , LineIndex } ;
17
17
use ra_ap_parser:: SyntaxKind ;
@@ -25,50 +25,53 @@ use ra_ap_syntax::{
25
25
#[ macro_export]
26
26
macro_rules! emit_detached {
27
27
( MacroCall , $self: ident, $node: ident, $label: ident) => {
28
- $self. extract_macro_call_expanded( & $node, $label) ;
28
+ $self. extract_macro_call_expanded( $node, $label) ;
29
29
} ;
30
30
( Function , $self: ident, $node: ident, $label: ident) => {
31
- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
31
+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
32
32
} ;
33
33
( Trait , $self: ident, $node: ident, $label: ident) => {
34
- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
34
+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
35
35
} ;
36
36
( Struct , $self: ident, $node: ident, $label: ident) => {
37
- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
37
+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
38
38
} ;
39
39
( Enum , $self: ident, $node: ident, $label: ident) => {
40
- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
40
+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
41
41
} ;
42
42
( Union , $self: ident, $node: ident, $label: ident) => {
43
- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
43
+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
44
44
} ;
45
45
( Module , $self: ident, $node: ident, $label: ident) => {
46
- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
46
+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
47
47
} ;
48
48
( Variant , $self: ident, $node: ident, $label: ident) => {
49
- $self. extract_canonical_origin_of_enum_variant( & $node, $label) ;
49
+ $self. extract_canonical_origin_of_enum_variant( $node, $label) ;
50
+ } ;
51
+ ( Item , $self: ident, $node: ident, $label: ident) => {
52
+ $self. emit_item_expansion( $node, $label) ;
50
53
} ;
51
54
// TODO canonical origin of other items
52
55
( PathExpr , $self: ident, $node: ident, $label: ident) => {
53
- $self. extract_path_canonical_destination( & $node, $label. into( ) ) ;
56
+ $self. extract_path_canonical_destination( $node, $label. into( ) ) ;
54
57
} ;
55
58
( StructExpr , $self: ident, $node: ident, $label: ident) => {
56
- $self. extract_path_canonical_destination( & $node, $label. into( ) ) ;
59
+ $self. extract_path_canonical_destination( $node, $label. into( ) ) ;
57
60
} ;
58
61
( PathPat , $self: ident, $node: ident, $label: ident) => {
59
- $self. extract_path_canonical_destination( & $node, $label. into( ) ) ;
62
+ $self. extract_path_canonical_destination( $node, $label. into( ) ) ;
60
63
} ;
61
64
( StructPat , $self: ident, $node: ident, $label: ident) => {
62
- $self. extract_path_canonical_destination( & $node, $label. into( ) ) ;
65
+ $self. extract_path_canonical_destination( $node, $label. into( ) ) ;
63
66
} ;
64
67
( TupleStructPat , $self: ident, $node: ident, $label: ident) => {
65
- $self. extract_path_canonical_destination( & $node, $label. into( ) ) ;
68
+ $self. extract_path_canonical_destination( $node, $label. into( ) ) ;
66
69
} ;
67
70
( MethodCallExpr , $self: ident, $node: ident, $label: ident) => {
68
- $self. extract_method_canonical_destination( & $node, $label) ;
71
+ $self. extract_method_canonical_destination( $node, $label) ;
69
72
} ;
70
73
( PathSegment , $self: ident, $node: ident, $label: ident) => {
71
- $self. extract_types_from_path_segment( & $node, $label. into( ) ) ;
74
+ $self. extract_types_from_path_segment( $node, $label. into( ) ) ;
72
75
} ;
73
76
( $( $_: tt) * ) => { } ;
74
77
}
@@ -252,7 +255,11 @@ impl<'a> Translator<'a> {
252
255
}
253
256
}
254
257
}
255
- fn emit_macro_expansion_parse_errors ( & mut self , mcall : & ast:: MacroCall , expanded : & SyntaxNode ) {
258
+ fn emit_macro_expansion_parse_errors (
259
+ & mut self ,
260
+ node : & impl ast:: AstNode ,
261
+ expanded : & SyntaxNode ,
262
+ ) {
256
263
let semantics = self . semantics . as_ref ( ) . unwrap ( ) ;
257
264
if let Some ( value) = semantics
258
265
. hir_file_for ( expanded)
@@ -266,19 +273,19 @@ impl<'a> Translator<'a> {
266
273
if let Some ( err) = & value. err {
267
274
let error = err. render_to_string ( semantics. db ) ;
268
275
269
- if err. span ( ) . anchor . file_id == semantics. hir_file_for ( mcall . syntax ( ) ) {
276
+ if err. span ( ) . anchor . file_id == semantics. hir_file_for ( node . syntax ( ) ) {
270
277
let location = err. span ( ) . range
271
278
+ semantics
272
279
. db
273
280
. ast_id_map ( err. span ( ) . anchor . file_id . into ( ) )
274
281
. get_erased ( err. span ( ) . anchor . ast_id )
275
282
. text_range ( )
276
283
. start ( ) ;
277
- self . emit_parse_error ( mcall , & SyntaxError :: new ( error. message , location) ) ;
284
+ self . emit_parse_error ( node , & SyntaxError :: new ( error. message , location) ) ;
278
285
} ;
279
286
}
280
287
for err in value. value . iter ( ) {
281
- self . emit_parse_error ( mcall , err) ;
288
+ self . emit_parse_error ( node , err) ;
282
289
}
283
290
}
284
291
}
@@ -290,20 +297,20 @@ impl<'a> Translator<'a> {
290
297
) -> Option < Label < generated:: AstNode > > {
291
298
match expand_to {
292
299
ra_ap_hir_expand:: ExpandTo :: Statements => ast:: MacroStmts :: cast ( expanded)
293
- . and_then ( |x| self . emit_macro_stmts ( x) )
300
+ . and_then ( |x| self . emit_macro_stmts ( & x) )
294
301
. map ( Into :: into) ,
295
302
ra_ap_hir_expand:: ExpandTo :: Items => ast:: MacroItems :: cast ( expanded)
296
- . and_then ( |x| self . emit_macro_items ( x) )
303
+ . and_then ( |x| self . emit_macro_items ( & x) )
297
304
. map ( Into :: into) ,
298
305
299
306
ra_ap_hir_expand:: ExpandTo :: Pattern => ast:: Pat :: cast ( expanded)
300
- . and_then ( |x| self . emit_pat ( x) )
307
+ . and_then ( |x| self . emit_pat ( & x) )
301
308
. map ( Into :: into) ,
302
309
ra_ap_hir_expand:: ExpandTo :: Type => ast:: Type :: cast ( expanded)
303
- . and_then ( |x| self . emit_type ( x) )
310
+ . and_then ( |x| self . emit_type ( & x) )
304
311
. map ( Into :: into) ,
305
312
ra_ap_hir_expand:: ExpandTo :: Expr => ast:: Expr :: cast ( expanded)
306
- . and_then ( |x| self . emit_expr ( x) )
313
+ . and_then ( |x| self . emit_expr ( & x) )
307
314
. map ( Into :: into) ,
308
315
}
309
316
}
@@ -626,17 +633,31 @@ impl<'a> Translator<'a> {
626
633
if let Some ( t) = type_refs
627
634
. next ( )
628
635
. and_then ( ast:: Type :: cast)
629
- . and_then ( |t| self . emit_type ( t) )
636
+ . and_then ( |t| self . emit_type ( & t) )
630
637
{
631
638
generated:: PathSegment :: emit_type_repr ( label, t, & mut self . trap . writer )
632
639
}
633
640
if let Some ( t) = type_refs
634
641
. next ( )
635
642
. and_then ( ast:: PathType :: cast)
636
- . and_then ( |t| self . emit_path_type ( t) )
643
+ . and_then ( |t| self . emit_path_type ( & t) )
637
644
{
638
645
generated:: PathSegment :: emit_trait_type_repr ( label, t, & mut self . trap . writer )
639
646
}
640
647
}
641
648
}
649
+
650
+ pub ( crate ) fn emit_item_expansion ( & mut self , node : & ast:: Item , label : Label < generated:: Item > ) {
651
+ ( || {
652
+ let semantics = self . semantics ?;
653
+ let ExpandResult {
654
+ value : expanded, ..
655
+ } = semantics. expand_attr_macro ( node) ?;
656
+ // TODO emit err?
657
+ self . emit_macro_expansion_parse_errors ( node, & expanded) ;
658
+ let expanded = self . emit_expanded_as ( ExpandTo :: Items , expanded) ?;
659
+ generated:: Item :: emit_expanded ( label, expanded, & mut self . trap . writer ) ;
660
+ Some ( ( ) )
661
+ } ) ( ) ;
662
+ }
642
663
}
0 commit comments