Skip to content

Commit c82e1f2

Browse files
committed
Point at type ascription before macro invocation on expansion parse error
1 parent 04b88a9 commit c82e1f2

File tree

9 files changed

+57
-13
lines changed

9 files changed

+57
-13
lines changed

src/libsyntax/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,7 @@ pub struct Mac_ {
13031303
pub path: Path,
13041304
pub delim: MacDelimiter,
13051305
pub tts: TokenStream,
1306+
pub prior_type_ascription: Option<(Span, bool)>,
13061307
}
13071308

13081309
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]

src/libsyntax/ext/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ pub struct ExpansionData {
713713
pub depth: usize,
714714
pub module: Rc<ModuleData>,
715715
pub directory_ownership: DirectoryOwnership,
716+
pub prior_type_ascription: Option<(Span, bool)>,
716717
}
717718

718719
/// One of these is made during expansion and incrementally updated as we go;
@@ -743,6 +744,7 @@ impl<'a> ExtCtxt<'a> {
743744
depth: 0,
744745
module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }),
745746
directory_ownership: DirectoryOwnership::Owned { relative: None },
747+
prior_type_ascription: None,
746748
},
747749
expansions: FxHashMap::default(),
748750
allow_derive_markers: [sym::rustc_attrs, sym::structural_match][..].into(),

src/libsyntax/ext/expand.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,21 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
517517
result
518518
}
519519
SyntaxExtensionKind::LegacyBang(expander) => {
520+
let prev = self.cx.current_expansion.prior_type_ascription;
521+
self.cx.current_expansion.prior_type_ascription =
522+
mac.node.prior_type_ascription;
520523
let tok_result = expander.expand(self.cx, span, mac.node.stream());
521-
if let Some(result) = fragment_kind.make_from(tok_result) {
524+
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
522525
result
523526
} else {
524527
let msg = format!("non-{kind} macro in {kind} position: {path}",
525528
kind = fragment_kind.name(), path = mac.node.path);
526529
self.cx.span_err(span, &msg);
527530
self.cx.trace_macros_diag();
528531
fragment_kind.dummy(span)
529-
}
532+
};
533+
self.cx.current_expansion.prior_type_ascription = prev;
534+
result
530535
}
531536
_ => unreachable!()
532537
}

src/libsyntax/ext/placeholders.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment {
1818
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
1919
tts: TokenStream::empty().into(),
2020
delim: ast::MacDelimiter::Brace,
21+
prior_type_ascription: None,
2122
})
2223
}
2324

src/libsyntax/ext/tt/macro_rules.rs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ fn generic_extension<'cx>(
173173
let mut p = Parser::new(cx.parse_sess(), tts, Some(directory), true, false, None);
174174
p.root_module_name =
175175
cx.current_expansion.module.mod_path.last().map(|id| id.as_str().to_string());
176+
p.last_type_ascription = cx.current_expansion.prior_type_ascription;
176177

177178
p.process_potential_macro_variable();
178179
// Let the context choose how to interpret the result.

src/libsyntax/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
530530
}
531531

532532
pub fn noop_visit_mac<T: MutVisitor>(Spanned { node, span }: &mut Mac, vis: &mut T) {
533-
let Mac_ { path, delim: _, tts } = node;
533+
let Mac_ { path, delim: _, tts, .. } = node;
534534
vis.visit_path(path);
535535
vis.visit_tts(tts);
536536
vis.visit_span(span);

src/libsyntax/parse/parser.rs

+36-6
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,12 @@ impl<'a> Parser<'a> {
14141414
if self.eat(&token::Not) {
14151415
// Macro invocation in type position
14161416
let (delim, tts) = self.expect_delimited_token_tree()?;
1417-
let node = Mac_ { path, tts, delim };
1417+
let node = Mac_ {
1418+
path,
1419+
tts,
1420+
delim,
1421+
prior_type_ascription: self.last_type_ascription,
1422+
};
14181423
TyKind::Mac(respan(lo.to(self.prev_span), node))
14191424
} else {
14201425
// Just a type path or bound list (trait object type) starting with a trait.
@@ -2245,7 +2250,12 @@ impl<'a> Parser<'a> {
22452250
// MACRO INVOCATION expression
22462251
let (delim, tts) = self.expect_delimited_token_tree()?;
22472252
hi = self.prev_span;
2248-
ex = ExprKind::Mac(respan(lo.to(hi), Mac_ { path, tts, delim }));
2253+
ex = ExprKind::Mac(respan(lo.to(hi), Mac_ {
2254+
path,
2255+
tts,
2256+
delim,
2257+
prior_type_ascription: self.last_type_ascription,
2258+
}));
22492259
} else if self.check(&token::OpenDelim(token::Brace)) {
22502260
if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) {
22512261
return expr;
@@ -3971,7 +3981,12 @@ impl<'a> Parser<'a> {
39713981
// Parse macro invocation
39723982
self.bump();
39733983
let (delim, tts) = self.expect_delimited_token_tree()?;
3974-
let mac = respan(lo.to(self.prev_span), Mac_ { path, tts, delim });
3984+
let mac = respan(lo.to(self.prev_span), Mac_ {
3985+
path,
3986+
tts,
3987+
delim,
3988+
prior_type_ascription: self.last_type_ascription,
3989+
});
39753990
pat = PatKind::Mac(mac);
39763991
}
39773992
token::DotDotDot | token::DotDotEq | token::DotDot => {
@@ -4417,7 +4432,12 @@ impl<'a> Parser<'a> {
44174432
MacStmtStyle::NoBraces
44184433
};
44194434

4420-
let mac = respan(lo.to(hi), Mac_ { path, tts, delim });
4435+
let mac = respan(lo.to(hi), Mac_ {
4436+
path,
4437+
tts,
4438+
delim,
4439+
prior_type_ascription: self.last_type_ascription,
4440+
});
44214441
let node = if delim == MacDelimiter::Brace ||
44224442
self.token == token::Semi || self.token == token::Eof {
44234443
StmtKind::Mac(P((mac, style, attrs.into())))
@@ -7550,7 +7570,12 @@ impl<'a> Parser<'a> {
75507570
}
75517571

75527572
let hi = self.prev_span;
7553-
let mac = respan(mac_lo.to(hi), Mac_ { path, tts, delim });
7573+
let mac = respan(mac_lo.to(hi), Mac_ {
7574+
path,
7575+
tts,
7576+
delim,
7577+
prior_type_ascription: self.last_type_ascription,
7578+
});
75547579
let item =
75557580
self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
75567581
return Ok(Some(item));
@@ -7600,7 +7625,12 @@ impl<'a> Parser<'a> {
76007625
self.expect(&token::Semi)?;
76017626
}
76027627

7603-
Ok(Some(respan(lo.to(self.prev_span), Mac_ { path, tts, delim })))
7628+
Ok(Some(respan(lo.to(self.prev_span), Mac_ {
7629+
path,
7630+
tts,
7631+
delim,
7632+
prior_type_ascription: self.last_type_ascription,
7633+
})))
76047634
} else {
76057635
Ok(None)
76067636
}

src/libsyntax_ext/assert.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub fn expand_assert<'cx>(
3838
))
3939
}).into(),
4040
delim: MacDelimiter::Parenthesis,
41+
prior_type_ascription: None,
4142
};
4243
let if_expr = cx.expr_if(
4344
sp,

src/test/ui/type/ascription/issue-47666.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ error: expected type, found reserved keyword `box`
22
--> $DIR/issue-47666.rs:2:25
33
|
44
LL | let _ = Option:Some(vec![0, 1]);
5-
| ^^^^^^^^^^
6-
| |
7-
| expected type
8-
| in this macro invocation
5+
| - ^^^^^^^^^^
6+
| | |
7+
| | expected type
8+
| | in this macro invocation
9+
| help: maybe write a path separator here: `::`
910
|
11+
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
12+
= note: for more information, see https://github.com/rust-lang/rust/issues/23416
1013
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
1114

1215
error: aborting due to previous error

0 commit comments

Comments
 (0)