@@ -3,15 +3,15 @@ use std::mem;
33use smallvec:: smallvec;
44use syntax:: ast:: { self , Ident } ;
55use syntax:: attr;
6- use syntax:: source_map:: { ExpnData , ExpnKind , respan} ;
7- use syntax:: ext:: base:: { ExtCtxt , MacroKind } ;
6+ use syntax:: ext:: base:: ExtCtxt ;
87use syntax:: ext:: expand:: { AstFragment , ExpansionConfig } ;
98use syntax:: ext:: proc_macro:: is_proc_macro_attr;
109use syntax:: parse:: ParseSess ;
1110use syntax:: ptr:: P ;
1211use syntax:: symbol:: { kw, sym} ;
1312use syntax:: visit:: { self , Visitor } ;
1413use syntax_pos:: { Span , DUMMY_SP } ;
14+ use syntax_pos:: hygiene:: AstPass ;
1515
1616struct ProcMacroDerive {
1717 trait_name : ast:: Name ,
@@ -308,8 +308,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
308308
309309// Creates a new module which looks like:
310310//
311- // #[doc(hidden)]
312- // mod $gensym {
311+ // const _: () = {
313312// extern crate proc_macro;
314313//
315314// use proc_macro::bridge::client::ProcMacro;
@@ -327,32 +326,29 @@ fn mk_decls(
327326 custom_attrs : & [ ProcMacroDef ] ,
328327 custom_macros : & [ ProcMacroDef ] ,
329328) -> P < ast:: Item > {
330- let span = DUMMY_SP . fresh_expansion ( ExpnData :: allow_unstable (
331- ExpnKind :: Macro ( MacroKind :: Attr , sym:: proc_macro) , DUMMY_SP , cx. parse_sess . edition ,
332- [ sym:: rustc_attrs, sym:: proc_macro_internals] [ ..] . into ( ) ,
333- ) ) ;
334-
335- let hidden = cx. meta_list_item_word ( span, sym:: hidden) ;
336- let doc = cx. meta_list ( span, sym:: doc, vec ! [ hidden] ) ;
337- let doc_hidden = cx. attribute ( doc) ;
338-
339- let proc_macro = Ident :: with_dummy_span ( sym:: proc_macro) ;
329+ let span = cx. resolver . span_for_ast_pass (
330+ DUMMY_SP ,
331+ AstPass :: ProcMacroHarness ,
332+ & [ sym:: rustc_attrs, sym:: proc_macro_internals] ,
333+ None ,
334+ ) ;
335+
336+ let proc_macro = Ident :: new ( sym:: proc_macro, span) ;
340337 let krate = cx. item ( span,
341338 proc_macro,
342339 Vec :: new ( ) ,
343340 ast:: ItemKind :: ExternCrate ( None ) ) ;
344341
345- let bridge = Ident :: from_str ( "bridge" ) ;
346- let client = Ident :: from_str ( "client" ) ;
347- let proc_macro_ty = Ident :: from_str ( "ProcMacro" ) ;
348- let custom_derive = Ident :: from_str ( "custom_derive" ) ;
349- let attr = Ident :: from_str ( "attr" ) ;
350- let bang = Ident :: from_str ( "bang" ) ;
351- let crate_kw = Ident :: with_dummy_span ( kw:: Crate ) ;
342+ let bridge = Ident :: from_str_and_span ( "bridge" , span) ;
343+ let client = Ident :: from_str_and_span ( "client" , span) ;
344+ let proc_macro_ty = Ident :: from_str_and_span ( "ProcMacro" , span) ;
345+ let custom_derive = Ident :: from_str_and_span ( "custom_derive" , span) ;
346+ let attr = Ident :: from_str_and_span ( "attr" , span) ;
347+ let bang = Ident :: from_str_and_span ( "bang" , span) ;
352348
353349 let decls = {
354350 let local_path = |sp : Span , name| {
355- cx. expr_path ( cx. path ( sp. with_ctxt ( span. ctxt ( ) ) , vec ! [ crate_kw , name] ) )
351+ cx. expr_path ( cx. path ( sp. with_ctxt ( span. ctxt ( ) ) , vec ! [ name] ) )
356352 } ;
357353 let proc_macro_ty_method_path = |method| cx. expr_path ( cx. path ( span, vec ! [
358354 proc_macro, bridge, client, proc_macro_ty, method,
@@ -381,7 +377,7 @@ fn mk_decls(
381377
382378 let decls_static = cx. item_static (
383379 span,
384- Ident :: from_str ( "_DECLS" ) ,
380+ Ident :: from_str_and_span ( "_DECLS" , span ) ,
385381 cx. ty_rptr ( span,
386382 cx. ty ( span, ast:: TyKind :: Slice (
387383 cx. ty_path ( cx. path ( span,
@@ -392,22 +388,44 @@ fn mk_decls(
392388 ) . map ( |mut i| {
393389 let attr = cx. meta_word ( span, sym:: rustc_proc_macro_decls) ;
394390 i. attrs . push ( cx. attribute ( attr) ) ;
395- i. vis = respan ( span, ast:: VisibilityKind :: Public ) ;
396391 i
397392 } ) ;
398393
399- let module = cx. item_mod (
400- span,
394+ let block = P ( ast:: Expr {
395+ id : ast:: DUMMY_NODE_ID ,
396+ attrs : syntax:: ThinVec :: new ( ) ,
397+ node : ast:: ExprKind :: Block ( P ( ast:: Block {
398+ id : ast:: DUMMY_NODE_ID ,
399+ rules : ast:: BlockCheckMode :: Default ,
400+ stmts : vec ! [
401+ ast:: Stmt {
402+ id: ast:: DUMMY_NODE_ID ,
403+ node: ast:: StmtKind :: Item ( krate) ,
404+ span,
405+ } ,
406+ ast:: Stmt {
407+ id: ast:: DUMMY_NODE_ID ,
408+ node: ast:: StmtKind :: Item ( decls_static) ,
409+ span,
410+ }
411+ ] ,
412+ span,
413+ } ) , None ) ,
401414 span,
402- ast:: Ident :: from_str ( "decls" ) . gensym ( ) ,
403- vec ! [ doc_hidden] ,
404- vec ! [ krate, decls_static] ,
405- ) . map ( |mut i| {
406- i. vis = respan ( span, ast:: VisibilityKind :: Public ) ;
407- i
408415 } ) ;
409416
417+ let anon_constant = cx. item_const (
418+ span,
419+ ast:: Ident :: new ( kw:: Underscore , span) ,
420+ P ( ast:: Ty {
421+ id : ast:: DUMMY_NODE_ID ,
422+ node : ast:: TyKind :: Tup ( Vec :: new ( ) ) ,
423+ span,
424+ } ) ,
425+ block,
426+ ) ;
427+
410428 // Integrate the new module into existing module structures.
411- let module = AstFragment :: Items ( smallvec ! [ module ] ) ;
412- cx. monotonic_expander ( ) . fully_expand_fragment ( module ) . make_items ( ) . pop ( ) . unwrap ( )
429+ let items = AstFragment :: Items ( smallvec ! [ anon_constant ] ) ;
430+ cx. monotonic_expander ( ) . fully_expand_fragment ( items ) . make_items ( ) . pop ( ) . unwrap ( )
413431}
0 commit comments