From c82e1f2d0e0b7fec9265eac851b2ac8b500ec4cc Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 18 Jul 2019 18:36:19 -0700 Subject: Point at type ascription before macro invocation on expansion parse error --- src/libsyntax/ext/base.rs | 2 ++ src/libsyntax/ext/expand.rs | 9 +++++++-- src/libsyntax/ext/placeholders.rs | 1 + src/libsyntax/ext/tt/macro_rules.rs | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index bb7834a133f..239fe55672e 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -713,6 +713,7 @@ pub struct ExpansionData { pub depth: usize, pub module: Rc, pub directory_ownership: DirectoryOwnership, + pub prior_type_ascription: Option<(Span, bool)>, } /// One of these is made during expansion and incrementally updated as we go; @@ -743,6 +744,7 @@ impl<'a> ExtCtxt<'a> { depth: 0, module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }), directory_ownership: DirectoryOwnership::Owned { relative: None }, + prior_type_ascription: None, }, expansions: FxHashMap::default(), allow_derive_markers: [sym::rustc_attrs, sym::structural_match][..].into(), diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index cd602d08c5b..6355f88195f 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -517,8 +517,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> { result } SyntaxExtensionKind::LegacyBang(expander) => { + let prev = self.cx.current_expansion.prior_type_ascription; + self.cx.current_expansion.prior_type_ascription = + mac.node.prior_type_ascription; let tok_result = expander.expand(self.cx, span, mac.node.stream()); - if let Some(result) = fragment_kind.make_from(tok_result) { + let result = if let Some(result) = fragment_kind.make_from(tok_result) { result } else { let msg = format!("non-{kind} macro in {kind} position: {path}", @@ -526,7 +529,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> { self.cx.span_err(span, &msg); self.cx.trace_macros_diag(); fragment_kind.dummy(span) - } + }; + self.cx.current_expansion.prior_type_ascription = prev; + result } _ => unreachable!() } diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index 17d8bf3ab17..b2b17b0fb28 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -18,6 +18,7 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment { path: ast::Path { span: DUMMY_SP, segments: Vec::new() }, tts: TokenStream::empty().into(), delim: ast::MacDelimiter::Brace, + prior_type_ascription: None, }) } diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 2a2df76d30c..7d72a18ca3f 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -173,6 +173,7 @@ fn generic_extension<'cx>( let mut p = Parser::new(cx.parse_sess(), tts, Some(directory), true, false, None); p.root_module_name = cx.current_expansion.module.mod_path.last().map(|id| id.as_str().to_string()); + p.last_type_ascription = cx.current_expansion.prior_type_ascription; p.process_potential_macro_variable(); // Let the context choose how to interpret the result. -- cgit 1.4.1-3-g733a5