Skip to content

Commit f01b9f8

Browse files
committed
Auto merge of #62816 - estebank:type-ascription-macros, r=petrochenkov
Point at type ascription before macro invocation on expansion parse error Fix #47666. Follow up to #62791. r? @petrochenkov
2 parents 460072e + c82e1f2 commit f01b9f8

File tree

9 files changed

+57
-13
lines changed

9 files changed

+57
-13
lines changed

Diff for: src/libsyntax/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,7 @@ pub struct Mac_ {
13061306
pub path: Path,
13071307
pub delim: MacDelimiter,
13081308
pub tts: TokenStream,
1309+
pub prior_type_ascription: Option<(Span, bool)>,
13091310
}
13101311

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

Diff for: 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(),

Diff for: 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
}

Diff for: 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

Diff for: 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.

Diff for: src/libsyntax/mut_visit.rs

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

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

Diff for: src/libsyntax/parse/parser.rs

+36-6
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,12 @@ impl<'a> Parser<'a> {
14151415
if self.eat(&token::Not) {
14161416
// Macro invocation in type position
14171417
let (delim, tts) = self.expect_delimited_token_tree()?;
1418-
let node = Mac_ { path, tts, delim };
1418+
let node = Mac_ {
1419+
path,
1420+
tts,
1421+
delim,
1422+
prior_type_ascription: self.last_type_ascription,
1423+
};
14191424
TyKind::Mac(respan(lo.to(self.prev_span), node))
14201425
} else {
14211426
// Just a type path or bound list (trait object type) starting with a trait.
@@ -2246,7 +2251,12 @@ impl<'a> Parser<'a> {
22462251
// MACRO INVOCATION expression
22472252
let (delim, tts) = self.expect_delimited_token_tree()?;
22482253
hi = self.prev_span;
2249-
ex = ExprKind::Mac(respan(lo.to(hi), Mac_ { path, tts, delim }));
2254+
ex = ExprKind::Mac(respan(lo.to(hi), Mac_ {
2255+
path,
2256+
tts,
2257+
delim,
2258+
prior_type_ascription: self.last_type_ascription,
2259+
}));
22502260
} else if self.check(&token::OpenDelim(token::Brace)) {
22512261
if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) {
22522262
return expr;
@@ -3963,7 +3973,12 @@ impl<'a> Parser<'a> {
39633973
// Parse macro invocation
39643974
self.bump();
39653975
let (delim, tts) = self.expect_delimited_token_tree()?;
3966-
let mac = respan(lo.to(self.prev_span), Mac_ { path, tts, delim });
3976+
let mac = respan(lo.to(self.prev_span), Mac_ {
3977+
path,
3978+
tts,
3979+
delim,
3980+
prior_type_ascription: self.last_type_ascription,
3981+
});
39673982
pat = PatKind::Mac(mac);
39683983
}
39693984
token::DotDotDot | token::DotDotEq | token::DotDot => {
@@ -4403,7 +4418,12 @@ impl<'a> Parser<'a> {
44034418
MacStmtStyle::NoBraces
44044419
};
44054420

4406-
let mac = respan(lo.to(hi), Mac_ { path, tts, delim });
4421+
let mac = respan(lo.to(hi), Mac_ {
4422+
path,
4423+
tts,
4424+
delim,
4425+
prior_type_ascription: self.last_type_ascription,
4426+
});
44074427
let node = if delim == MacDelimiter::Brace ||
44084428
self.token == token::Semi || self.token == token::Eof {
44094429
StmtKind::Mac(P((mac, style, attrs.into())))
@@ -7518,7 +7538,12 @@ impl<'a> Parser<'a> {
75187538
}
75197539

75207540
let hi = self.prev_span;
7521-
let mac = respan(mac_lo.to(hi), Mac_ { path, tts, delim });
7541+
let mac = respan(mac_lo.to(hi), Mac_ {
7542+
path,
7543+
tts,
7544+
delim,
7545+
prior_type_ascription: self.last_type_ascription,
7546+
});
75227547
let item =
75237548
self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
75247549
return Ok(Some(item));
@@ -7568,7 +7593,12 @@ impl<'a> Parser<'a> {
75687593
self.expect(&token::Semi)?;
75697594
}
75707595

7571-
Ok(Some(respan(lo.to(self.prev_span), Mac_ { path, tts, delim })))
7596+
Ok(Some(respan(lo.to(self.prev_span), Mac_ {
7597+
path,
7598+
tts,
7599+
delim,
7600+
prior_type_ascription: self.last_type_ascription,
7601+
})))
75727602
} else {
75737603
Ok(None)
75747604
}

Diff for: src/libsyntax_ext/assert.rs

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

Diff for: 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)