@@ -11,7 +11,7 @@ use ra_ap_hir::{
1111} ;
1212use ra_ap_hir_def:: ModuleId ;
1313use ra_ap_hir_def:: type_ref:: Mutability ;
14- use ra_ap_hir_expand:: ExpandTo ;
14+ use ra_ap_hir_expand:: { ExpandResult , ExpandTo } ;
1515use ra_ap_ide_db:: RootDatabase ;
1616use ra_ap_ide_db:: line_index:: { LineCol , LineIndex } ;
1717use ra_ap_parser:: SyntaxKind ;
@@ -25,50 +25,53 @@ use ra_ap_syntax::{
2525#[ macro_export]
2626macro_rules! emit_detached {
2727 ( MacroCall , $self: ident, $node: ident, $label: ident) => {
28- $self. extract_macro_call_expanded( & $node, $label) ;
28+ $self. extract_macro_call_expanded( $node, $label) ;
2929 } ;
3030 ( Function , $self: ident, $node: ident, $label: ident) => {
31- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
31+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
3232 } ;
3333 ( Trait , $self: ident, $node: ident, $label: ident) => {
34- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
34+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
3535 } ;
3636 ( Struct , $self: ident, $node: ident, $label: ident) => {
37- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
37+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
3838 } ;
3939 ( Enum , $self: ident, $node: ident, $label: ident) => {
40- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
40+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
4141 } ;
4242 ( Union , $self: ident, $node: ident, $label: ident) => {
43- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
43+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
4444 } ;
4545 ( Module , $self: ident, $node: ident, $label: ident) => {
46- $self. extract_canonical_origin( & $node, $label. into( ) ) ;
46+ $self. extract_canonical_origin( $node, $label. into( ) ) ;
4747 } ;
4848 ( 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) ;
5053 } ;
5154 // TODO canonical origin of other items
5255 ( 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( ) ) ;
5457 } ;
5558 ( 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( ) ) ;
5760 } ;
5861 ( 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( ) ) ;
6063 } ;
6164 ( 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( ) ) ;
6366 } ;
6467 ( 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( ) ) ;
6669 } ;
6770 ( MethodCallExpr , $self: ident, $node: ident, $label: ident) => {
68- $self. extract_method_canonical_destination( & $node, $label) ;
71+ $self. extract_method_canonical_destination( $node, $label) ;
6972 } ;
7073 ( 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( ) ) ;
7275 } ;
7376 ( $( $_: tt) * ) => { } ;
7477}
@@ -252,7 +255,11 @@ impl<'a> Translator<'a> {
252255 }
253256 }
254257 }
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+ ) {
256263 let semantics = self . semantics . as_ref ( ) . unwrap ( ) ;
257264 if let Some ( value) = semantics
258265 . hir_file_for ( expanded)
@@ -266,19 +273,19 @@ impl<'a> Translator<'a> {
266273 if let Some ( err) = & value. err {
267274 let error = err. render_to_string ( semantics. db ) ;
268275
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 ( ) ) {
270277 let location = err. span ( ) . range
271278 + semantics
272279 . db
273280 . ast_id_map ( err. span ( ) . anchor . file_id . into ( ) )
274281 . get_erased ( err. span ( ) . anchor . ast_id )
275282 . text_range ( )
276283 . start ( ) ;
277- self . emit_parse_error ( mcall , & SyntaxError :: new ( error. message , location) ) ;
284+ self . emit_parse_error ( node , & SyntaxError :: new ( error. message , location) ) ;
278285 } ;
279286 }
280287 for err in value. value . iter ( ) {
281- self . emit_parse_error ( mcall , err) ;
288+ self . emit_parse_error ( node , err) ;
282289 }
283290 }
284291 }
@@ -290,20 +297,20 @@ impl<'a> Translator<'a> {
290297 ) -> Option < Label < generated:: AstNode > > {
291298 match expand_to {
292299 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) )
294301 . map ( Into :: into) ,
295302 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) )
297304 . map ( Into :: into) ,
298305
299306 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) )
301308 . map ( Into :: into) ,
302309 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) )
304311 . map ( Into :: into) ,
305312 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) )
307314 . map ( Into :: into) ,
308315 }
309316 }
@@ -626,17 +633,31 @@ impl<'a> Translator<'a> {
626633 if let Some ( t) = type_refs
627634 . next ( )
628635 . and_then ( ast:: Type :: cast)
629- . and_then ( |t| self . emit_type ( t) )
636+ . and_then ( |t| self . emit_type ( & t) )
630637 {
631638 generated:: PathSegment :: emit_type_repr ( label, t, & mut self . trap . writer )
632639 }
633640 if let Some ( t) = type_refs
634641 . next ( )
635642 . and_then ( ast:: PathType :: cast)
636- . and_then ( |t| self . emit_path_type ( t) )
643+ . and_then ( |t| self . emit_path_type ( & t) )
637644 {
638645 generated:: PathSegment :: emit_trait_type_repr ( label, t, & mut self . trap . writer )
639646 }
640647 }
641648 }
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+ }
642663}
0 commit comments