diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2019-07-18 18:36:19 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2019-07-30 09:16:27 -0700 |
| commit | c82e1f2d0e0b7fec9265eac851b2ac8b500ec4cc (patch) | |
| tree | 592ca5d1ec1cd6cee76aae75db74d347583b7a59 /src/libsyntax | |
| parent | 04b88a9eba8abbac87eddcb2998beea09589c2c9 (diff) | |
| download | rust-c82e1f2d0e0b7fec9265eac851b2ac8b500ec4cc.tar.gz rust-c82e1f2d0e0b7fec9265eac851b2ac8b500ec4cc.zip | |
Point at type ascription before macro invocation on expansion parse error
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/ext/base.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax/ext/placeholders.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_rules.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/mut_visit.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 42 |
7 files changed, 49 insertions, 9 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 58a1c4aee9a..76b67366f06 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1303,6 +1303,7 @@ pub struct Mac_ { pub path: Path, pub delim: MacDelimiter, pub tts: TokenStream, + pub prior_type_ascription: Option<(Span, bool)>, } #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)] 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<ModuleData>, 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. diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 43b6bea69d9..f215a321cb3 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -530,7 +530,7 @@ pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) { } pub fn noop_visit_mac<T: MutVisitor>(Spanned { node, span }: &mut Mac, vis: &mut T) { - let Mac_ { path, delim: _, tts } = node; + let Mac_ { path, delim: _, tts, .. } = node; vis.visit_path(path); vis.visit_tts(tts); vis.visit_span(span); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6cb965bf817..d15419f615f 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1414,7 +1414,12 @@ impl<'a> Parser<'a> { if self.eat(&token::Not) { // Macro invocation in type position let (delim, tts) = self.expect_delimited_token_tree()?; - let node = Mac_ { path, tts, delim }; + let node = Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }; TyKind::Mac(respan(lo.to(self.prev_span), node)) } else { // Just a type path or bound list (trait object type) starting with a trait. @@ -2245,7 +2250,12 @@ impl<'a> Parser<'a> { // MACRO INVOCATION expression let (delim, tts) = self.expect_delimited_token_tree()?; hi = self.prev_span; - ex = ExprKind::Mac(respan(lo.to(hi), Mac_ { path, tts, delim })); + ex = ExprKind::Mac(respan(lo.to(hi), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + })); } else if self.check(&token::OpenDelim(token::Brace)) { if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) { return expr; @@ -3971,7 +3981,12 @@ impl<'a> Parser<'a> { // Parse macro invocation self.bump(); let (delim, tts) = self.expect_delimited_token_tree()?; - let mac = respan(lo.to(self.prev_span), Mac_ { path, tts, delim }); + let mac = respan(lo.to(self.prev_span), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }); pat = PatKind::Mac(mac); } token::DotDotDot | token::DotDotEq | token::DotDot => { @@ -4417,7 +4432,12 @@ impl<'a> Parser<'a> { MacStmtStyle::NoBraces }; - let mac = respan(lo.to(hi), Mac_ { path, tts, delim }); + let mac = respan(lo.to(hi), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }); let node = if delim == MacDelimiter::Brace || self.token == token::Semi || self.token == token::Eof { StmtKind::Mac(P((mac, style, attrs.into()))) @@ -7550,7 +7570,12 @@ impl<'a> Parser<'a> { } let hi = self.prev_span; - let mac = respan(mac_lo.to(hi), Mac_ { path, tts, delim }); + let mac = respan(mac_lo.to(hi), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }); let item = self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs); return Ok(Some(item)); @@ -7600,7 +7625,12 @@ impl<'a> Parser<'a> { self.expect(&token::Semi)?; } - Ok(Some(respan(lo.to(self.prev_span), Mac_ { path, tts, delim }))) + Ok(Some(respan(lo.to(self.prev_span), Mac_ { + path, + tts, + delim, + prior_type_ascription: self.last_type_ascription, + }))) } else { Ok(None) } |
