From d45131239792cecf4a80b672ced8c385028a7266 Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Tue, 10 Apr 2018 00:12:41 +0300 Subject: [PATCH 1/4] Implement function!() macro (See RFC issue 1743) The introduced function!() macro in this change, returns a value of `&'static str`, that specifies the current function by name. It is an empty string if not used inside a function. Under nesting of other functions, the outer functions will be included too with '::' as a 'function path' separator. Under methods and traits, the trait or type name will be included. For types having type parameters, the type parameters are not expanded, so the same string is used for all instantiations. For an impl of anonymous types such as (bool, u32), the name of the implemented trait is provided. For example, test.rs: fn main() { fn inner() { println!("{}", function!()); fn inner_a() { println!("{}", function!()); } inner_a(); } fn inner2() { println!("{}", function!()); fn inner_b() { println!("{}", function!()); } inner_b(); } println!("{}", module_path!()); println!("{}", function!()); inner(); inner2(); ] Emits: test main main::inner main::inner::inner_a main::inner2 main::inner2::inner_b Signed-off-by: Dan Aloni --- .../src/language-features/plugin.md | 2 +- src/libsyntax/diagnostics/plugin.rs | 3 + src/libsyntax/ext/base.rs | 10 +- src/libsyntax/ext/expand.rs | 131 ++++++++++++++---- src/libsyntax/ext/quote.rs | 12 ++ src/libsyntax/ext/source_util.rs | 48 +++++-- src/libsyntax/ext/tt/macro_rules.rs | 1 + src/libsyntax_ext/asm.rs | 1 + src/libsyntax_ext/assert.rs | 1 + src/libsyntax_ext/cfg.rs | 1 + src/libsyntax_ext/compile_error.rs | 1 + src/libsyntax_ext/concat.rs | 1 + src/libsyntax_ext/concat_idents.rs | 1 + src/libsyntax_ext/env.rs | 2 + src/libsyntax_ext/format.rs | 1 + src/libsyntax_ext/global_asm.rs | 1 + src/libsyntax_ext/lib.rs | 2 +- src/libsyntax_ext/log_syntax.rs | 1 + src/libsyntax_ext/trace_macros.rs | 1 + 19 files changed, 177 insertions(+), 44 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/plugin.md b/src/doc/unstable-book/src/language-features/plugin.md index 1cece930eeaa5..53fec6382ddc4 100644 --- a/src/doc/unstable-book/src/language-features/plugin.md +++ b/src/doc/unstable-book/src/language-features/plugin.md @@ -62,7 +62,7 @@ use syntax::ext::build::AstBuilder; // A trait for expr_usize. use syntax::ext::quote::rt::Span; use rustc_plugin::Registry; -fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) +fn expand_rn(cx: &mut ExtCtxt, path: &Path, sp: Span, args: &[TokenTree]) -> Box { static NUMERALS: &'static [(&'static str, usize)] = &[ diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index aecf32ab6afb7..863970c70b97e 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -40,6 +40,7 @@ pub struct ErrorInfo { pub type ErrorMap = BTreeMap; pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, span: Span, token_tree: &[TokenTree]) -> Box { @@ -73,6 +74,7 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt, } pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, span: Span, token_tree: &[TokenTree]) -> Box { @@ -143,6 +145,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt, } pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, span: Span, token_tree: &[TokenTree]) -> Box { diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index a09bea25a249c..9ec5b2497c8dd 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -231,18 +231,18 @@ impl AttrProcMacro for F /// Represents a thing that maps token trees to Macro Results pub trait TTMacroExpander { - fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream) + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, &'cx Option<::ast::Path>, span: Span, input: TokenStream) -> Box; } pub type MacroExpanderFn = - for<'cx> fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) + for<'cx> fn(&'cx mut ExtCtxt, &'cx Option<::ast::Path>, Span, &[tokenstream::TokenTree]) -> Box; impl TTMacroExpander for F - where F: for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) -> Box + where F: for<'cx> Fn(&'cx mut ExtCtxt, &'cx Option<::ast::Path>, Span, &[tokenstream::TokenTree]) -> Box { - fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream) + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, path: &'cx Option<::ast::Path>, span: Span, input: TokenStream) -> Box { struct AvoidInterpolatedIdents; @@ -264,7 +264,7 @@ impl TTMacroExpander for F let input: Vec<_> = input.trees().map(|tt| AvoidInterpolatedIdents.fold_tt(tt)).collect(); - (*self)(ecx, span, &input) + (*self)(ecx, path, span, &input) } } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 678c20402d6f4..ae0e8f3381c9e 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -137,6 +137,13 @@ expansions! { "foreign item", .make_foreign_items, lift .fold_foreign_item, lift .visit_foreign_item; } +fn dummy_path() -> Path { + Path { + span: DUMMY_SP, + segments: Vec::new(), + } +} + impl ExpansionKind { fn dummy(self, span: Span) -> Option { self.make_from(DummyResult::any(span)) @@ -189,6 +196,7 @@ pub enum InvocationKind { mac: ast::Mac, ident: Option, span: Span, + context_path: Path, }, Attr { attr: Option, @@ -271,7 +279,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { let orig_expansion_data = self.cx.current_expansion.clone(); self.cx.current_expansion.depth = 0; - let (expansion, mut invocations) = self.collect_invocations(expansion, &[]); + let (expansion, mut invocations) = self.collect_invocations(expansion, &[], None); self.resolve_imports(); invocations.reverse(); @@ -309,10 +317,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> { // FIXME(jseyfried): Refactor out the following logic let (expansion, new_invocations) = if let Some(ext) = ext { if let Some(ext) = ext { - let dummy = invoc.expansion_kind.dummy(invoc.span()).unwrap(); - let expansion = self.expand_invoc(invoc, &*ext).unwrap_or(dummy); - self.collect_invocations(expansion, &[]) - } else if let InvocationKind::Attr { attr: None, traits, item } = invoc.kind { + let dummy = (invoc.expansion_kind.dummy(invoc.span()).unwrap(), Some(dummy_path())); + let (expansion, context_path) = self.expand_invoc(invoc, &*ext).unwrap_or(dummy); + self.collect_invocations(expansion, &[], context_path) + } else if let InvocationKind::Attr { attr: None, traits, item, .. } = invoc.kind { if !item.derive_allowed() { let attr = attr::find_by_name(item.attrs(), "derive") .expect("`derive` attribute should exist"); @@ -357,12 +365,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } let expansion = invoc.expansion_kind .expect_from_annotatables(::std::iter::once(item_with_markers)); - self.collect_invocations(expansion, derives) + self.collect_invocations(expansion, derives, None) } else { unreachable!() } } else { - self.collect_invocations(invoc.expansion_kind.dummy(invoc.span()).unwrap(), &[]) + self.collect_invocations(invoc.expansion_kind.dummy(invoc.span()).unwrap(), &[], None) }; if expansions.len() < depth { @@ -395,7 +403,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } } - fn collect_invocations(&mut self, expansion: Expansion, derives: &[Mark]) + fn collect_invocations(&mut self, expansion: Expansion, derives: &[Mark], context_path : Option) -> (Expansion, Vec) { let result = { let mut collector = InvocationCollector { @@ -405,6 +413,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { features: self.cx.ecfg.features, }, cx: self.cx, + context_path : context_path.unwrap_or(dummy_path()), invocations: Vec::new(), monotonic: self.monotonic, }; @@ -453,11 +462,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } } - fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option { + fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option<(Expansion, Option)> { let result = match invoc.kind { InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext)?, - InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext)?, - InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext)?, + InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext).map(|x|(x, None))?, + InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext).map(|x|(x, None))?, }; if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit { @@ -543,12 +552,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> { fn expand_bang_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) - -> Option { + -> Option<(Expansion, Option)> { let (mark, kind) = (invoc.expansion_data.mark, invoc.expansion_kind); - let (mac, ident, span) = match invoc.kind { - InvocationKind::Bang { mac, ident, span } => (mac, ident, span), + let (mac, ident, span, context_path) = match invoc.kind { + InvocationKind::Bang { mac, ident, span, context_path } + => (mac, ident, span, context_path), _ => unreachable!(), }; + let some_context_path = Some(context_path); let path = &mac.node.path; let ident = ident.unwrap_or_else(|| keywords::Invalid.ident()); @@ -601,7 +612,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { false, false, None) { dummy_span } else { - kind.make_from(expand.expand(self.cx, span, mac.node.stream())) + kind.make_from(expand.expand(self.cx, &some_context_path, span, mac.node.stream())) } } @@ -618,7 +629,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { unstable_feature) { dummy_span } else { - kind.make_from(expander.expand(self.cx, span, mac.node.stream())) + kind.make_from(expander.expand(self.cx, &some_context_path, span, mac.node.stream())) } } @@ -684,7 +695,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } }; - if opt_expanded.is_some() { + let r = if opt_expanded.is_some() { opt_expanded } else { let msg = format!("non-{kind} macro in {kind} position: {name}", @@ -692,7 +703,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { self.cx.span_err(path.span, &msg); self.cx.trace_macros_diag(); kind.dummy(span) - } + }; + r.map(|x|(x, some_context_path)) } /// Expand a derive invocation. Returns the result of expansion. @@ -854,6 +866,7 @@ impl<'a> Parser<'a> { struct InvocationCollector<'a, 'b: 'a> { cx: &'a mut ExtCtxt<'b>, cfg: StripUnconfigured<'a>, + context_path: Path, invocations: Vec, monotonic: bool, } @@ -873,8 +886,25 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { placeholder(expansion_kind, NodeId::placeholder_from_mark(mark)) } + fn push_ident(&mut self, ident: &Ident) -> bool { + if *ident != keywords::Invalid.ident() { + self.context_path.segments.push(ast::PathSegment { + ident : ident.clone(), + parameters : None, // TODO + }); + true + } else { + false + } + } + + fn make_bang(&self, ident: Option, mac: ast::Mac, span: Span) -> InvocationKind { + InvocationKind::Bang { mac: mac, ident, span: span, context_path: self.context_path.clone() } + } + fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: ExpansionKind) -> Expansion { - self.collect(kind, InvocationKind::Bang { mac: mac, ident: None, span: span }) + let bang = self.make_bang(None, mac, span); + self.collect(kind, bang) } fn collect_attr(&mut self, @@ -1054,16 +1084,41 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { return self.collect_attr(attr, traits, item, ExpansionKind::Items).make_items(); } - match item.node { + let mut pushed_ident = false; + match &item.node { + &ast::ItemKind::Impl(_, _, _, _, _, ref ty, _) => { + pushed_ident = self.push_ident(&item.ident); + if !pushed_ident { + if let ast::TyKind::Path(_, ref path) = &ty.node { + match path.segments.last() { + Some(last) => { + self.context_path.segments.push(last.clone()); + pushed_ident = true; + } + None => {} + } + } + } + } + ast::ItemKind::Mod(_) => { + // module_path!() macro already covers crate/module hierarchy + } + ast::ItemKind::Static(_, _, _) => { + // if function!() used to initialize a static, the name of the static + // should not wind up in the ident list. + } + _ => { + pushed_ident = self.push_ident(&item.ident); + } + } + + let folded = match item.node { ast::ItemKind::Mac(..) => { self.check_attributes(&item.attrs); item.and_then(|item| match item.node { ItemKind::Mac(mac) => { - self.collect(ExpansionKind::Items, InvocationKind::Bang { - mac, - ident: Some(item.ident), - span: item.span, - }).make_items() + let bang = self.make_bang(Some(item.ident), mac, item.span); + self.collect(ExpansionKind::Items, bang).make_items() } _ => unreachable!(), }) @@ -1126,7 +1181,13 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { noop_fold_item(item, self) } _ => noop_fold_item(item, self), + }; + + if pushed_ident { + self.context_path.segments.pop(); } + + folded } fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector { @@ -1139,14 +1200,22 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { .make_trait_items() } - match item.node { + let pushed_ident = self.push_ident(&item.ident); + + let r = match item.node { ast::TraitItemKind::Macro(mac) => { let ast::TraitItem { attrs, span, .. } = item; self.check_attributes(&attrs); self.collect_bang(mac, span, ExpansionKind::TraitItems).make_trait_items() } _ => fold::noop_fold_trait_item(item, self), + }; + + if pushed_ident { + self.context_path.segments.pop(); } + + r } fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector { @@ -1159,14 +1228,22 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { .make_impl_items(); } - match item.node { + let pushed_ident = self.push_ident(&item.ident); + + let r = match item.node { ast::ImplItemKind::Macro(mac) => { let ast::ImplItem { attrs, span, .. } = item; self.check_attributes(&attrs); self.collect_bang(mac, span, ExpansionKind::ImplItems).make_impl_items() } _ => fold::noop_fold_impl_item(item, self), + }; + + if pushed_ident { + self.context_path.segments.pop(); } + + r } fn fold_ty(&mut self, ty: P) -> P { diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 3303955d398a6..d9aad66d6b315 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -450,6 +450,7 @@ pub fn parse_path_panic(parser: &mut Parser, mode: PathStyle) -> ast::Path { } pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -459,6 +460,7 @@ pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -467,6 +469,7 @@ pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_item<'cx>(cx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -475,6 +478,7 @@ pub fn expand_quote_item<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -483,6 +487,7 @@ pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_arm(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -491,6 +496,7 @@ pub fn expand_quote_arm(cx: &mut ExtCtxt, } pub fn expand_quote_ty(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -499,6 +505,7 @@ pub fn expand_quote_ty(cx: &mut ExtCtxt, } pub fn expand_quote_stmt(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -507,6 +514,7 @@ pub fn expand_quote_stmt(cx: &mut ExtCtxt, } pub fn expand_quote_attr(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -517,6 +525,7 @@ pub fn expand_quote_attr(cx: &mut ExtCtxt, } pub fn expand_quote_arg(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -525,6 +534,7 @@ pub fn expand_quote_arg(cx: &mut ExtCtxt, } pub fn expand_quote_block(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -533,6 +543,7 @@ pub fn expand_quote_block(cx: &mut ExtCtxt, } pub fn expand_quote_meta_item(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -541,6 +552,7 @@ pub fn expand_quote_meta_item(cx: &mut ExtCtxt, } pub fn expand_quote_path(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index d6dce63ea5e4b..ee37c9900e245 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -31,7 +31,9 @@ use rustc_data_structures::sync::Lrc; // a given file into the current one. /// line!(): expands to the current line number -pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_line(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "line!"); @@ -42,7 +44,9 @@ pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) } /* column!(): expands to the current column number */ -pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_column(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "column!"); @@ -53,10 +57,12 @@ pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) } /* __rust_unstable_column!(): expands to the current column number */ -pub fn expand_column_gated(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_column_gated(cx: &mut ExtCtxt, + path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { if sp.allows_unstable() { - expand_column(cx, sp, tts) + expand_column(cx, path, sp, tts) } else { cx.span_fatal(sp, "the __rust_unstable_column macro is unstable"); } @@ -65,7 +71,8 @@ pub fn expand_column_gated(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::Token /// file!(): expands to the current filename */ /// The filemap (`loc.file`) contains a bunch more information we could spit /// out if we wanted. -pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_file(cx: &mut ExtCtxt, _path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "file!"); @@ -74,13 +81,28 @@ pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string()))) } -pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_function<'cx>(cx: &'cx mut ExtCtxt, + path: &'cx Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) + -> Box { + base::check_zero_tts(cx, sp, tts, "function!"); + + let topmost = cx.expansion_cause().unwrap_or(sp); + let path = path.as_ref().unwrap(); + base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&path.to_string()))) +} + +pub fn expand_stringify(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let s = pprust::tts_to_string(tts); base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s))) } -pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_mod(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "module_path!"); let mod_path = &cx.current_expansion.module.mod_path; @@ -92,7 +114,9 @@ pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) /// include! : parse the given file as an expr /// This is generally a bad idea because it's going to behave /// unhygienically. -pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include!") { Some(f) => f, @@ -130,7 +154,9 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::T } // include_str! : read the given file, insert it as a literal string expr -pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include_str(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") { Some(f) => f, @@ -165,7 +191,9 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenT } } -pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include_bytes(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, + sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") { Some(f) => f, diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index ffe68289d5224..4fb0da99ac311 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -69,6 +69,7 @@ struct MacroRulesMacroExpander { impl TTMacroExpander for MacroRulesMacroExpander { fn expand<'cx>(&self, cx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, sp: Span, input: TokenStream) -> Box { diff --git a/src/libsyntax_ext/asm.rs b/src/libsyntax_ext/asm.rs index d1de4dccd0043..33f0a01e27339 100644 --- a/src/libsyntax_ext/asm.rs +++ b/src/libsyntax_ext/asm.rs @@ -48,6 +48,7 @@ impl State { const OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"]; pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/assert.rs b/src/libsyntax_ext/assert.rs index 7352c494a426c..9dbd07e4dca14 100644 --- a/src/libsyntax_ext/assert.rs +++ b/src/libsyntax_ext/assert.rs @@ -20,6 +20,7 @@ use syntax_pos::{Span, DUMMY_SP}; pub fn expand_assert<'cx>( cx: &'cx mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree], ) -> Box { diff --git a/src/libsyntax_ext/cfg.rs b/src/libsyntax_ext/cfg.rs index 6acc578d07e78..754ad7d33fa30 100644 --- a/src/libsyntax_ext/cfg.rs +++ b/src/libsyntax_ext/cfg.rs @@ -21,6 +21,7 @@ use syntax::parse::token; use syntax_pos::Span; pub fn expand_cfg<'cx>(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/compile_error.rs b/src/libsyntax_ext/compile_error.rs index 7bc7afba63cb4..6d542806cad0f 100644 --- a/src/libsyntax_ext/compile_error.rs +++ b/src/libsyntax_ext/compile_error.rs @@ -16,6 +16,7 @@ use syntax_pos::Span; use syntax::tokenstream; pub fn expand_compile_error<'cx>(cx: &'cx mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index 6c085528a6632..c78f9c21c8001 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -18,6 +18,7 @@ use syntax::tokenstream; use std::string::String; pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, + _path: &Option<::ast::Path>, sp: syntax_pos::Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/concat_idents.rs b/src/libsyntax_ext/concat_idents.rs index 544b1410d3d91..c7e1d50c403e0 100644 --- a/src/libsyntax_ext/concat_idents.rs +++ b/src/libsyntax_ext/concat_idents.rs @@ -19,6 +19,7 @@ use syntax_pos::symbol::Symbol; use syntax::tokenstream::TokenTree; pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { diff --git a/src/libsyntax_ext/env.rs b/src/libsyntax_ext/env.rs index 4e1af108ab4fa..4cd39d67ef158 100644 --- a/src/libsyntax_ext/env.rs +++ b/src/libsyntax_ext/env.rs @@ -24,6 +24,7 @@ use syntax::tokenstream; use std::env; pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { @@ -56,6 +57,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index c78decb1eb9d0..65e728c7921ed 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -675,6 +675,7 @@ impl<'a, 'b> Context<'a, 'b> { } pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt, + _path: &'cx Option<::ast::Path>, mut sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/global_asm.rs b/src/libsyntax_ext/global_asm.rs index f01a0aacb0a73..2d132d5314645 100644 --- a/src/libsyntax_ext/global_asm.rs +++ b/src/libsyntax_ext/global_asm.rs @@ -33,6 +33,7 @@ use syntax::util::small_vector::SmallVector; pub const MACRO: &'static str = "global_asm"; pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { if !cx.ecfg.enable_global_asm() { diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 249a64b353f59..4e8ae4dcd65ef 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -97,13 +97,13 @@ pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, line: expand_line, __rust_unstable_column: expand_column_gated, column: expand_column, + function: expand_function, file: expand_file, stringify: expand_stringify, include: expand_include, include_str: expand_include_str, include_bytes: expand_include_bytes, module_path: expand_mod, - asm: asm::expand_asm, global_asm: global_asm::expand_global_asm, cfg: cfg::expand_cfg, diff --git a/src/libsyntax_ext/log_syntax.rs b/src/libsyntax_ext/log_syntax.rs index 71f1951d5d455..606de5b9f0ee3 100644 --- a/src/libsyntax_ext/log_syntax.rs +++ b/src/libsyntax_ext/log_syntax.rs @@ -15,6 +15,7 @@ use syntax::tokenstream; use syntax_pos; pub fn expand_syntax_ext<'cx>(cx: &'cx mut base::ExtCtxt, + _path: &Option<::ast::Path>, sp: syntax_pos::Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/trace_macros.rs b/src/libsyntax_ext/trace_macros.rs index 48be8e0c53c2e..c7cd806adfb8d 100644 --- a/src/libsyntax_ext/trace_macros.rs +++ b/src/libsyntax_ext/trace_macros.rs @@ -16,6 +16,7 @@ use syntax_pos::Span; use syntax::tokenstream::TokenTree; pub fn expand_trace_macros(cx: &mut ExtCtxt, + _path: &Option<::ast::Path>, sp: Span, tt: &[TokenTree]) -> Box { From 59e500ec96ab8cd5eb08d66d1f9abec8cd1dfd27 Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Tue, 10 Apr 2018 00:53:33 +0300 Subject: [PATCH 2/4] Formatting fixes --- src/libsyntax/ext/base.rs | 7 ++++--- src/libsyntax/ext/expand.rs | 32 +++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 9ec5b2497c8dd..deefec807a8e0 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -240,10 +240,11 @@ pub type MacroExpanderFn = -> Box; impl TTMacroExpander for F - where F: for<'cx> Fn(&'cx mut ExtCtxt, &'cx Option<::ast::Path>, Span, &[tokenstream::TokenTree]) -> Box + where F: for<'cx> Fn(&'cx mut ExtCtxt, &'cx Option<::ast::Path>, Span, + &[tokenstream::TokenTree]) -> Box { - fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, path: &'cx Option<::ast::Path>, span: Span, input: TokenStream) - -> Box { + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, path: &'cx Option<::ast::Path>, span: Span, + input: TokenStream) -> Box { struct AvoidInterpolatedIdents; impl Folder for AvoidInterpolatedIdents { diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index ae0e8f3381c9e..de15d5b5f6c05 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -317,8 +317,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> { // FIXME(jseyfried): Refactor out the following logic let (expansion, new_invocations) = if let Some(ext) = ext { if let Some(ext) = ext { - let dummy = (invoc.expansion_kind.dummy(invoc.span()).unwrap(), Some(dummy_path())); - let (expansion, context_path) = self.expand_invoc(invoc, &*ext).unwrap_or(dummy); + let dummy = (invoc.expansion_kind.dummy( + invoc.span()).unwrap(), Some(dummy_path())); + let (expansion, context_path) = + self.expand_invoc(invoc, &*ext).unwrap_or(dummy); self.collect_invocations(expansion, &[], context_path) } else if let InvocationKind::Attr { attr: None, traits, item, .. } = invoc.kind { if !item.derive_allowed() { @@ -370,7 +372,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { unreachable!() } } else { - self.collect_invocations(invoc.expansion_kind.dummy(invoc.span()).unwrap(), &[], None) + self.collect_invocations( + invoc.expansion_kind.dummy(invoc.span()).unwrap(), &[], None) }; if expansions.len() < depth { @@ -403,7 +406,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } } - fn collect_invocations(&mut self, expansion: Expansion, derives: &[Mark], context_path : Option) + fn collect_invocations(&mut self, + expansion: Expansion, derives: &[Mark], context_path : Option) -> (Expansion, Vec) { let result = { let mut collector = InvocationCollector { @@ -462,11 +466,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } } - fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option<(Expansion, Option)> { + fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) + -> Option<(Expansion, Option)> { let result = match invoc.kind { InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext)?, - InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext).map(|x|(x, None))?, - InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext).map(|x|(x, None))?, + InvocationKind::Attr { .. } => + self.expand_attr_invoc(invoc, ext).map(|x|(x, None))?, + InvocationKind::Derive { .. } => + self.expand_derive_invoc(invoc, ext).map(|x|(x, None))?, }; if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit { @@ -612,7 +619,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { false, false, None) { dummy_span } else { - kind.make_from(expand.expand(self.cx, &some_context_path, span, mac.node.stream())) + kind.make_from(expand.expand(self.cx, + &some_context_path, span, mac.node.stream())) } } @@ -629,7 +637,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { unstable_feature) { dummy_span } else { - kind.make_from(expander.expand(self.cx, &some_context_path, span, mac.node.stream())) + kind.make_from(expander.expand(self.cx, + &some_context_path, span, mac.node.stream())) } } @@ -890,7 +899,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { if *ident != keywords::Invalid.ident() { self.context_path.segments.push(ast::PathSegment { ident : ident.clone(), - parameters : None, // TODO + parameters : None, // FIXME }); true } else { @@ -899,7 +908,8 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { } fn make_bang(&self, ident: Option, mac: ast::Mac, span: Span) -> InvocationKind { - InvocationKind::Bang { mac: mac, ident, span: span, context_path: self.context_path.clone() } + InvocationKind::Bang { + mac: mac, ident, span: span, context_path: self.context_path.clone() } } fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: ExpansionKind) -> Expansion { From 51b3ff05cbaf039b1caab36993842afbf33c4a24 Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Tue, 10 Apr 2018 01:01:35 +0300 Subject: [PATCH 3/4] Formatting fixes [2] --- src/libsyntax/ext/base.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index deefec807a8e0..8db77ab1e6e8f 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -231,7 +231,8 @@ impl AttrProcMacro for F /// Represents a thing that maps token trees to Macro Results pub trait TTMacroExpander { - fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, &'cx Option<::ast::Path>, span: Span, input: TokenStream) + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, + &'cx Option<::ast::Path>, span: Span, input: TokenStream) -> Box; } From 590f73f7a1f45b3f3b0da8c3a51291f868d27202 Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Tue, 10 Apr 2018 09:57:06 +0300 Subject: [PATCH 4/4] Avoid the plugin API change by managing the context path inside ExtCtxt. --- .../src/language-features/plugin.md | 2 +- src/libsyntax/diagnostics/plugin.rs | 3 -- src/libsyntax/ext/base.rs | 16 +++---- src/libsyntax/ext/expand.rs | 29 ++++++------ src/libsyntax/ext/quote.rs | 12 ----- src/libsyntax/ext/source_util.rs | 45 +++++++------------ src/libsyntax/ext/tt/macro_rules.rs | 1 - src/libsyntax_ext/asm.rs | 1 - src/libsyntax_ext/assert.rs | 1 - src/libsyntax_ext/cfg.rs | 1 - src/libsyntax_ext/compile_error.rs | 1 - src/libsyntax_ext/concat.rs | 1 - src/libsyntax_ext/concat_idents.rs | 1 - src/libsyntax_ext/env.rs | 2 - src/libsyntax_ext/format.rs | 1 - src/libsyntax_ext/global_asm.rs | 1 - src/libsyntax_ext/lib.rs | 1 + src/libsyntax_ext/log_syntax.rs | 1 - src/libsyntax_ext/trace_macros.rs | 1 - 19 files changed, 41 insertions(+), 80 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/plugin.md b/src/doc/unstable-book/src/language-features/plugin.md index 53fec6382ddc4..1cece930eeaa5 100644 --- a/src/doc/unstable-book/src/language-features/plugin.md +++ b/src/doc/unstable-book/src/language-features/plugin.md @@ -62,7 +62,7 @@ use syntax::ext::build::AstBuilder; // A trait for expr_usize. use syntax::ext::quote::rt::Span; use rustc_plugin::Registry; -fn expand_rn(cx: &mut ExtCtxt, path: &Path, sp: Span, args: &[TokenTree]) +fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box { static NUMERALS: &'static [(&'static str, usize)] = &[ diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index 863970c70b97e..aecf32ab6afb7 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -40,7 +40,6 @@ pub struct ErrorInfo { pub type ErrorMap = BTreeMap; pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, span: Span, token_tree: &[TokenTree]) -> Box { @@ -74,7 +73,6 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt, } pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, span: Span, token_tree: &[TokenTree]) -> Box { @@ -145,7 +143,6 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt, } pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, span: Span, token_tree: &[TokenTree]) -> Box { diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 8db77ab1e6e8f..01bee83c85dcc 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -231,21 +231,19 @@ impl AttrProcMacro for F /// Represents a thing that maps token trees to Macro Results pub trait TTMacroExpander { - fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, - &'cx Option<::ast::Path>, span: Span, input: TokenStream) + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream) -> Box; } pub type MacroExpanderFn = - for<'cx> fn(&'cx mut ExtCtxt, &'cx Option<::ast::Path>, Span, &[tokenstream::TokenTree]) + for<'cx> fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) -> Box; impl TTMacroExpander for F - where F: for<'cx> Fn(&'cx mut ExtCtxt, &'cx Option<::ast::Path>, Span, - &[tokenstream::TokenTree]) -> Box + where F: for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) -> Box { - fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, path: &'cx Option<::ast::Path>, span: Span, - input: TokenStream) -> Box { + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream) + -> Box { struct AvoidInterpolatedIdents; impl Folder for AvoidInterpolatedIdents { @@ -266,7 +264,7 @@ impl TTMacroExpander for F let input: Vec<_> = input.trees().map(|tt| AvoidInterpolatedIdents.fold_tt(tt)).collect(); - (*self)(ecx, path, span, &input) + (*self)(ecx, span, &input) } } @@ -721,6 +719,7 @@ pub struct ExtCtxt<'a> { pub parse_sess: &'a parse::ParseSess, pub ecfg: expand::ExpansionConfig<'a>, pub root_path: PathBuf, + pub context_path: Option>, pub resolver: &'a mut Resolver, pub resolve_err_count: usize, pub current_expansion: ExpansionData, @@ -738,6 +737,7 @@ impl<'a> ExtCtxt<'a> { root_path: PathBuf::new(), resolver, resolve_err_count: 0, + context_path: None, current_expansion: ExpansionData { mark: Mark::root(), depth: 0, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index de15d5b5f6c05..d97516f27611c 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -196,7 +196,7 @@ pub enum InvocationKind { mac: ast::Mac, ident: Option, span: Span, - context_path: Path, + context_path: Rc, }, Attr { attr: Option, @@ -318,7 +318,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { let (expansion, new_invocations) = if let Some(ext) = ext { if let Some(ext) = ext { let dummy = (invoc.expansion_kind.dummy( - invoc.span()).unwrap(), Some(dummy_path())); + invoc.span()).unwrap(), None); let (expansion, context_path) = self.expand_invoc(invoc, &*ext).unwrap_or(dummy); self.collect_invocations(expansion, &[], context_path) @@ -407,7 +407,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } fn collect_invocations(&mut self, - expansion: Expansion, derives: &[Mark], context_path : Option) + expansion: Expansion, derives: &[Mark], context_path : Option>) -> (Expansion, Vec) { let result = { let mut collector = InvocationCollector { @@ -417,7 +417,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> { features: self.cx.ecfg.features, }, cx: self.cx, - context_path : context_path.unwrap_or(dummy_path()), + context_path : match context_path { + None => dummy_path(), + Some(path) => (*path).clone(), + }, invocations: Vec::new(), monotonic: self.monotonic, }; @@ -467,7 +470,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) - -> Option<(Expansion, Option)> { + -> Option<(Expansion, Option>)> { let result = match invoc.kind { InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext)?, InvocationKind::Attr { .. } => @@ -559,15 +562,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> { fn expand_bang_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) - -> Option<(Expansion, Option)> { + -> Option<(Expansion, Option>)> { let (mark, kind) = (invoc.expansion_data.mark, invoc.expansion_kind); let (mac, ident, span, context_path) = match invoc.kind { InvocationKind::Bang { mac, ident, span, context_path } => (mac, ident, span, context_path), _ => unreachable!(), }; - let some_context_path = Some(context_path); let path = &mac.node.path; + self.cx.context_path = None; let ident = ident.unwrap_or_else(|| keywords::Invalid.ident()); let validate_and_set_expn_info = |this: &mut Self, // arg instead of capture @@ -619,8 +622,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { false, false, None) { dummy_span } else { - kind.make_from(expand.expand(self.cx, - &some_context_path, span, mac.node.stream())) + self.cx.context_path = Some(context_path.clone()); + kind.make_from(expand.expand(self.cx, span, mac.node.stream())) } } @@ -637,8 +640,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { unstable_feature) { dummy_span } else { - kind.make_from(expander.expand(self.cx, - &some_context_path, span, mac.node.stream())) + self.cx.context_path = Some(context_path.clone()); + kind.make_from(expander.expand(self.cx, span, mac.node.stream())) } } @@ -713,7 +716,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { self.cx.trace_macros_diag(); kind.dummy(span) }; - r.map(|x|(x, some_context_path)) + r.map(|x|(x, Some(context_path))) } /// Expand a derive invocation. Returns the result of expansion. @@ -909,7 +912,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { fn make_bang(&self, ident: Option, mac: ast::Mac, span: Span) -> InvocationKind { InvocationKind::Bang { - mac: mac, ident, span: span, context_path: self.context_path.clone() } + mac: mac, ident, span: span, context_path: Rc::new(self.context_path.clone()) } } fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: ExpansionKind) -> Expansion { diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index d9aad66d6b315..3303955d398a6 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -450,7 +450,6 @@ pub fn parse_path_panic(parser: &mut Parser, mode: PathStyle) -> ast::Path { } pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -460,7 +459,6 @@ pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -469,7 +467,6 @@ pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_item<'cx>(cx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -478,7 +475,6 @@ pub fn expand_quote_item<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -487,7 +483,6 @@ pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_quote_arm(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -496,7 +491,6 @@ pub fn expand_quote_arm(cx: &mut ExtCtxt, } pub fn expand_quote_ty(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -505,7 +499,6 @@ pub fn expand_quote_ty(cx: &mut ExtCtxt, } pub fn expand_quote_stmt(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -514,7 +507,6 @@ pub fn expand_quote_stmt(cx: &mut ExtCtxt, } pub fn expand_quote_attr(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -525,7 +517,6 @@ pub fn expand_quote_attr(cx: &mut ExtCtxt, } pub fn expand_quote_arg(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -534,7 +525,6 @@ pub fn expand_quote_arg(cx: &mut ExtCtxt, } pub fn expand_quote_block(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -543,7 +533,6 @@ pub fn expand_quote_block(cx: &mut ExtCtxt, } pub fn expand_quote_meta_item(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { @@ -552,7 +541,6 @@ pub fn expand_quote_meta_item(cx: &mut ExtCtxt, } pub fn expand_quote_path(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index ee37c9900e245..65295e57ac2b1 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -31,9 +31,7 @@ use rustc_data_structures::sync::Lrc; // a given file into the current one. /// line!(): expands to the current line number -pub fn expand_line(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "line!"); @@ -44,9 +42,7 @@ pub fn expand_line(cx: &mut ExtCtxt, } /* column!(): expands to the current column number */ -pub fn expand_column(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "column!"); @@ -57,12 +53,10 @@ pub fn expand_column(cx: &mut ExtCtxt, } /* __rust_unstable_column!(): expands to the current column number */ -pub fn expand_column_gated(cx: &mut ExtCtxt, - path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_column_gated(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { if sp.allows_unstable() { - expand_column(cx, path, sp, tts) + expand_column(cx, sp, tts) } else { cx.span_fatal(sp, "the __rust_unstable_column macro is unstable"); } @@ -71,8 +65,7 @@ pub fn expand_column_gated(cx: &mut ExtCtxt, /// file!(): expands to the current filename */ /// The filemap (`loc.file`) contains a bunch more information we could spit /// out if we wanted. -pub fn expand_file(cx: &mut ExtCtxt, _path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "file!"); @@ -82,27 +75,25 @@ pub fn expand_file(cx: &mut ExtCtxt, _path: &Option<::ast::Path>, } pub fn expand_function<'cx>(cx: &'cx mut ExtCtxt, - path: &'cx Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "function!"); let topmost = cx.expansion_cause().unwrap_or(sp); - let path = path.as_ref().unwrap(); - base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&path.to_string()))) + let symbol = match cx.context_path.as_ref() { + None => Symbol::intern(""), + Some(path) => Symbol::intern((*path).to_string().as_str()) + }; + base::MacEager::expr(cx.expr_str(topmost, symbol)) } -pub fn expand_stringify(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let s = pprust::tts_to_string(tts); base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s))) } -pub fn expand_mod(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { base::check_zero_tts(cx, sp, tts, "module_path!"); let mod_path = &cx.current_expansion.module.mod_path; @@ -114,9 +105,7 @@ pub fn expand_mod(cx: &mut ExtCtxt, /// include! : parse the given file as an expr /// This is generally a bad idea because it's going to behave /// unhygienically. -pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include!") { Some(f) => f, @@ -154,9 +143,7 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, } // include_str! : read the given file, insert it as a literal string expr -pub fn expand_include_str(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") { Some(f) => f, @@ -191,9 +178,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt, } } -pub fn expand_include_bytes(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, - sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") { Some(f) => f, diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 4fb0da99ac311..ffe68289d5224 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -69,7 +69,6 @@ struct MacroRulesMacroExpander { impl TTMacroExpander for MacroRulesMacroExpander { fn expand<'cx>(&self, cx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, sp: Span, input: TokenStream) -> Box { diff --git a/src/libsyntax_ext/asm.rs b/src/libsyntax_ext/asm.rs index 33f0a01e27339..d1de4dccd0043 100644 --- a/src/libsyntax_ext/asm.rs +++ b/src/libsyntax_ext/asm.rs @@ -48,7 +48,6 @@ impl State { const OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"]; pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/assert.rs b/src/libsyntax_ext/assert.rs index 9dbd07e4dca14..7352c494a426c 100644 --- a/src/libsyntax_ext/assert.rs +++ b/src/libsyntax_ext/assert.rs @@ -20,7 +20,6 @@ use syntax_pos::{Span, DUMMY_SP}; pub fn expand_assert<'cx>( cx: &'cx mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree], ) -> Box { diff --git a/src/libsyntax_ext/cfg.rs b/src/libsyntax_ext/cfg.rs index 754ad7d33fa30..6acc578d07e78 100644 --- a/src/libsyntax_ext/cfg.rs +++ b/src/libsyntax_ext/cfg.rs @@ -21,7 +21,6 @@ use syntax::parse::token; use syntax_pos::Span; pub fn expand_cfg<'cx>(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/compile_error.rs b/src/libsyntax_ext/compile_error.rs index 6d542806cad0f..7bc7afba63cb4 100644 --- a/src/libsyntax_ext/compile_error.rs +++ b/src/libsyntax_ext/compile_error.rs @@ -16,7 +16,6 @@ use syntax_pos::Span; use syntax::tokenstream; pub fn expand_compile_error<'cx>(cx: &'cx mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index c78f9c21c8001..6c085528a6632 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -18,7 +18,6 @@ use syntax::tokenstream; use std::string::String; pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, - _path: &Option<::ast::Path>, sp: syntax_pos::Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/concat_idents.rs b/src/libsyntax_ext/concat_idents.rs index c7e1d50c403e0..544b1410d3d91 100644 --- a/src/libsyntax_ext/concat_idents.rs +++ b/src/libsyntax_ext/concat_idents.rs @@ -19,7 +19,6 @@ use syntax_pos::symbol::Symbol; use syntax::tokenstream::TokenTree; pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[TokenTree]) -> Box { diff --git a/src/libsyntax_ext/env.rs b/src/libsyntax_ext/env.rs index 4cd39d67ef158..4e1af108ab4fa 100644 --- a/src/libsyntax_ext/env.rs +++ b/src/libsyntax_ext/env.rs @@ -24,7 +24,6 @@ use syntax::tokenstream; use std::env; pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { @@ -57,7 +56,6 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, } pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 65e728c7921ed..c78decb1eb9d0 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -675,7 +675,6 @@ impl<'a, 'b> Context<'a, 'b> { } pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt, - _path: &'cx Option<::ast::Path>, mut sp: Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/global_asm.rs b/src/libsyntax_ext/global_asm.rs index 2d132d5314645..f01a0aacb0a73 100644 --- a/src/libsyntax_ext/global_asm.rs +++ b/src/libsyntax_ext/global_asm.rs @@ -33,7 +33,6 @@ use syntax::util::small_vector::SmallVector; pub const MACRO: &'static str = "global_asm"; pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tts: &[tokenstream::TokenTree]) -> Box { if !cx.ecfg.enable_global_asm() { diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 4e8ae4dcd65ef..5a1e3467a5bcf 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -104,6 +104,7 @@ pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, include_str: expand_include_str, include_bytes: expand_include_bytes, module_path: expand_mod, + asm: asm::expand_asm, global_asm: global_asm::expand_global_asm, cfg: cfg::expand_cfg, diff --git a/src/libsyntax_ext/log_syntax.rs b/src/libsyntax_ext/log_syntax.rs index 606de5b9f0ee3..71f1951d5d455 100644 --- a/src/libsyntax_ext/log_syntax.rs +++ b/src/libsyntax_ext/log_syntax.rs @@ -15,7 +15,6 @@ use syntax::tokenstream; use syntax_pos; pub fn expand_syntax_ext<'cx>(cx: &'cx mut base::ExtCtxt, - _path: &Option<::ast::Path>, sp: syntax_pos::Span, tts: &[tokenstream::TokenTree]) -> Box { diff --git a/src/libsyntax_ext/trace_macros.rs b/src/libsyntax_ext/trace_macros.rs index c7cd806adfb8d..48be8e0c53c2e 100644 --- a/src/libsyntax_ext/trace_macros.rs +++ b/src/libsyntax_ext/trace_macros.rs @@ -16,7 +16,6 @@ use syntax_pos::Span; use syntax::tokenstream::TokenTree; pub fn expand_trace_macros(cx: &mut ExtCtxt, - _path: &Option<::ast::Path>, sp: Span, tt: &[TokenTree]) -> Box {