Skip to content

Commit e19cb40

Browse files
authored
Rollup merge of #65974 - Centril:matcher-friendly-gating, r=petrochenkov
A scheme for more macro-matcher friendly pre-expansion gating Pre-expansion gating will now avoid gating macro matchers that did not result in `Success(...)`. That is, the following is now OK despite `box 42` being a valid `expr` and that form being pre-expansion gated: ```rust macro_rules! m { ($e:expr) => { 0 }; // This fails on the input below due to `, foo`. (box $e:expr, foo) => { 1 }; // Successful matcher, we should get `2`. } fn main() { assert_eq!(1, m!(box 42, foo)); } ``` Closes #65846. r? @petrochenkov cc @Mark-Simulacrum
2 parents 883fe10 + bceaba8 commit e19cb40

File tree

10 files changed

+106
-73
lines changed

10 files changed

+106
-73
lines changed

src/libsyntax/feature_gate/check.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -870,18 +870,17 @@ pub fn check_crate(krate: &ast::Crate,
870870
maybe_stage_features(&parse_sess.span_diagnostic, krate, unstable);
871871
let mut visitor = PostExpansionVisitor { parse_sess, features };
872872

873+
let spans = parse_sess.gated_spans.spans.borrow();
873874
macro_rules! gate_all {
874-
($gate:ident, $msg:literal) => { gate_all!($gate, $gate, $msg); };
875-
($spans:ident, $gate:ident, $msg:literal) => {
876-
for span in &*parse_sess.gated_spans.$spans.borrow() {
875+
($gate:ident, $msg:literal) => {
876+
for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
877877
gate_feature!(&visitor, $gate, *span, $msg);
878878
}
879879
}
880880
}
881-
882881
gate_all!(let_chains, "`let` expressions in this position are experimental");
883882
gate_all!(async_closure, "async closures are unstable");
884-
gate_all!(yields, generators, "yield syntax is experimental");
883+
gate_all!(generators, "yield syntax is experimental");
885884
gate_all!(or_patterns, "or-patterns syntax is experimental");
886885
gate_all!(const_extern_fn, "`const extern fn` definitions are unstable");
887886

@@ -892,7 +891,7 @@ pub fn check_crate(krate: &ast::Crate,
892891
// FIXME(eddyb) do something more useful than always
893892
// disabling these uses of early feature-gatings.
894893
if false {
895-
for span in &*parse_sess.gated_spans.$gate.borrow() {
894+
for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
896895
gate_feature!(&visitor, $gate, *span, $msg);
897896
}
898897
}
@@ -909,7 +908,6 @@ pub fn check_crate(krate: &ast::Crate,
909908
gate_all!(try_blocks, "`try` blocks are unstable");
910909
gate_all!(label_break_value, "labels on blocks are unstable");
911910
gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
912-
913911
// To avoid noise about type ascription in common syntax errors,
914912
// only emit if it is the *only* error. (Also check it last.)
915913
if parse_sess.span_diagnostic.err_count() == 0 {

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ impl<'a> Parser<'a> {
11201120
self.expected_tokens.push(TokenType::Keyword(kw::Crate));
11211121
if self.is_crate_vis() {
11221122
self.bump(); // `crate`
1123-
self.sess.gated_spans.crate_visibility_modifier.borrow_mut().push(self.prev_span);
1123+
self.sess.gated_spans.gate(sym::crate_visibility_modifier, self.prev_span);
11241124
return Ok(respan(self.prev_span, VisibilityKind::Crate(CrateSugar::JustCrate)));
11251125
}
11261126

src/libsyntax/parse/parser/expr.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use crate::parse::token::{self, Token, TokenKind};
1616
use crate::print::pprust;
1717
use crate::ptr::P;
1818
use crate::source_map::{self, Span};
19-
use crate::symbol::{kw, sym};
2019
use crate::util::parser::{AssocOp, Fixity, prec_let_scrutinee_needs_par};
2120

2221
use errors::Applicability;
22+
use syntax_pos::symbol::{kw, sym};
2323
use syntax_pos::Symbol;
2424
use std::mem;
2525
use rustc_data_structures::thin_vec::ThinVec;
@@ -252,7 +252,7 @@ impl<'a> Parser<'a> {
252252
self.last_type_ascription = Some((self.prev_span, maybe_path));
253253

254254
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type)?;
255-
self.sess.gated_spans.type_ascription.borrow_mut().push(lhs.span);
255+
self.sess.gated_spans.gate(sym::type_ascription, lhs.span);
256256
continue
257257
} else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
258258
// If we didn’t have to handle `x..`/`x..=`, it would be pretty easy to
@@ -455,7 +455,7 @@ impl<'a> Parser<'a> {
455455
let e = self.parse_prefix_expr(None);
456456
let (span, e) = self.interpolated_or_expr_span(e)?;
457457
let span = lo.to(span);
458-
self.sess.gated_spans.box_syntax.borrow_mut().push(span);
458+
self.sess.gated_spans.gate(sym::box_syntax, span);
459459
(span, ExprKind::Box(e))
460460
}
461461
token::Ident(..) if self.token.is_ident_named(sym::not) => {
@@ -1045,7 +1045,7 @@ impl<'a> Parser<'a> {
10451045
}
10461046

10471047
let span = lo.to(hi);
1048-
self.sess.gated_spans.yields.borrow_mut().push(span);
1048+
self.sess.gated_spans.gate(sym::generators, span);
10491049
} else if self.eat_keyword(kw::Let) {
10501050
return self.parse_let_expr(attrs);
10511051
} else if is_span_rust_2018 && self.eat_keyword(kw::Await) {
@@ -1268,7 +1268,7 @@ impl<'a> Parser<'a> {
12681268
outer_attrs: ThinVec<Attribute>,
12691269
) -> PResult<'a, P<Expr>> {
12701270
if let Some(label) = opt_label {
1271-
self.sess.gated_spans.label_break_value.borrow_mut().push(label.ident.span);
1271+
self.sess.gated_spans.gate(sym::label_break_value, label.ident.span);
12721272
}
12731273

12741274
self.expect(&token::OpenDelim(token::Brace))?;
@@ -1297,7 +1297,7 @@ impl<'a> Parser<'a> {
12971297
};
12981298
if asyncness.is_async() {
12991299
// Feature-gate `async ||` closures.
1300-
self.sess.gated_spans.async_closure.borrow_mut().push(self.prev_span);
1300+
self.sess.gated_spans.gate(sym::async_closure, self.prev_span);
13011301
}
13021302

13031303
let capture_clause = self.parse_capture_clause();
@@ -1419,8 +1419,7 @@ impl<'a> Parser<'a> {
14191419

14201420
if let ExprKind::Let(..) = cond.kind {
14211421
// Remove the last feature gating of a `let` expression since it's stable.
1422-
let last = self.sess.gated_spans.let_chains.borrow_mut().pop();
1423-
debug_assert_eq!(cond.span, last.unwrap());
1422+
self.sess.gated_spans.ungate_last(sym::let_chains, cond.span);
14241423
}
14251424

14261425
Ok(cond)
@@ -1437,7 +1436,7 @@ impl<'a> Parser<'a> {
14371436
|this| this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
14381437
)?;
14391438
let span = lo.to(expr.span);
1440-
self.sess.gated_spans.let_chains.borrow_mut().push(span);
1439+
self.sess.gated_spans.gate(sym::let_chains, span);
14411440
Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs))
14421441
}
14431442

@@ -1658,7 +1657,7 @@ impl<'a> Parser<'a> {
16581657
Err(error)
16591658
} else {
16601659
let span = span_lo.to(body.span);
1661-
self.sess.gated_spans.try_blocks.borrow_mut().push(span);
1660+
self.sess.gated_spans.gate(sym::try_blocks, span);
16621661
Ok(self.mk_expr(span, ExprKind::TryBlock(body), attrs))
16631662
}
16641663
}

src/libsyntax/parse/parser/generics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use super::{Parser, PResult};
33
use crate::ast::{self, WhereClause, GenericParam, GenericParamKind, GenericBounds, Attribute};
44
use crate::parse::token;
55
use crate::source_map::DUMMY_SP;
6-
use crate::symbol::kw;
6+
7+
use syntax_pos::symbol::{kw, sym};
78

89
impl<'a> Parser<'a> {
910
/// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
@@ -62,7 +63,7 @@ impl<'a> Parser<'a> {
6263
self.expect(&token::Colon)?;
6364
let ty = self.parse_ty()?;
6465

65-
self.sess.gated_spans.const_generics.borrow_mut().push(lo.to(self.prev_span));
66+
self.sess.gated_spans.gate(sym::const_generics, lo.to(self.prev_span));
6667

6768
Ok(GenericParam {
6869
ident,

src/libsyntax/parse/parser/item.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ impl<'a> Parser<'a> {
146146
let unsafety = self.parse_unsafety();
147147

148148
if self.check_keyword(kw::Extern) {
149-
self.sess.gated_spans.const_extern_fn.borrow_mut().push(
150-
lo.to(self.token.span)
151-
);
149+
self.sess.gated_spans.gate(sym::const_extern_fn, lo.to(self.token.span));
152150
}
153151
let abi = self.parse_extern_abi()?;
154152
self.bump(); // `fn`
@@ -831,7 +829,7 @@ impl<'a> Parser<'a> {
831829
.emit();
832830
}
833831

834-
self.sess.gated_spans.trait_alias.borrow_mut().push(whole_span);
832+
self.sess.gated_spans.gate(sym::trait_alias, whole_span);
835833

836834
Ok((ident, ItemKind::TraitAlias(tps, bounds), None))
837835
} else {
@@ -1711,7 +1709,7 @@ impl<'a> Parser<'a> {
17111709
let span = lo.to(self.prev_span);
17121710

17131711
if !def.legacy {
1714-
self.sess.gated_spans.decl_macro.borrow_mut().push(span);
1712+
self.sess.gated_spans.gate(sym::decl_macro, span);
17151713
}
17161714

17171715
Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))

src/libsyntax/parse/parser/pat.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use crate::mut_visit::{noop_visit_pat, noop_visit_mac, MutVisitor};
88
use crate::parse::token::{self};
99
use crate::print::pprust;
1010
use crate::source_map::{respan, Span, Spanned};
11-
use crate::symbol::kw;
1211
use crate::ThinVec;
13-
12+
use syntax_pos::symbol::{kw, sym};
1413
use errors::{Applicability, DiagnosticBuilder};
1514

1615
type Expected = Option<&'static str>;
@@ -52,11 +51,8 @@ impl<'a> Parser<'a> {
5251
// and no other gated or-pattern has been parsed thus far,
5352
// then we should really gate the leading `|`.
5453
// This complicated procedure is done purely for diagnostics UX.
55-
if gated_leading_vert {
56-
let mut or_pattern_spans = self.sess.gated_spans.or_patterns.borrow_mut();
57-
if or_pattern_spans.is_empty() {
58-
or_pattern_spans.push(leading_vert_span);
59-
}
54+
if gated_leading_vert && self.sess.gated_spans.is_ungated(sym::or_patterns) {
55+
self.sess.gated_spans.gate(sym::or_patterns, leading_vert_span);
6056
}
6157

6258
Ok(pat)
@@ -117,7 +113,7 @@ impl<'a> Parser<'a> {
117113

118114
// Feature gate the or-pattern if instructed:
119115
if gate_or == GateOr::Yes {
120-
self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
116+
self.sess.gated_spans.gate(sym::or_patterns, or_pattern_span);
121117
}
122118

123119
Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
@@ -325,7 +321,7 @@ impl<'a> Parser<'a> {
325321
} else if self.eat_keyword(kw::Box) {
326322
// Parse `box pat`
327323
let pat = self.parse_pat_with_range_pat(false, None)?;
328-
self.sess.gated_spans.box_patterns.borrow_mut().push(lo.to(self.prev_span));
324+
self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_span));
329325
PatKind::Box(pat)
330326
} else if self.can_be_ident_pat() {
331327
// Parse `ident @ pat`
@@ -612,7 +608,7 @@ impl<'a> Parser<'a> {
612608
}
613609

614610
fn excluded_range_end(&self, span: Span) -> RangeEnd {
615-
self.sess.gated_spans.exclusive_range_pattern.borrow_mut().push(span);
611+
self.sess.gated_spans.gate(sym::exclusive_range_pattern, span);
616612
RangeEnd::Excluded
617613
}
618614

src/libsyntax/parse/parser/path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ast::{self, QSelf, Path, PathSegment, Ident, ParenthesizedArgs, Angle
55
use crate::ast::{AnonConst, GenericArg, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode};
66
use crate::parse::token::{self, Token};
77
use crate::source_map::{Span, BytePos};
8-
use crate::symbol::kw;
8+
use syntax_pos::symbol::{kw, sym};
99

1010
use std::mem;
1111
use log::debug;
@@ -426,7 +426,7 @@ impl<'a> Parser<'a> {
426426

427427
// Gate associated type bounds, e.g., `Iterator<Item: Ord>`.
428428
if let AssocTyConstraintKind::Bound { .. } = kind {
429-
self.sess.gated_spans.associated_type_bounds.borrow_mut().push(span);
429+
self.sess.gated_spans.gate(sym::associated_type_bounds, span);
430430
}
431431

432432
constraints.push(AssocTyConstraint {

src/libsyntax/sess.rs

+48-34
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,53 @@ use std::str;
1919
/// Collected spans during parsing for places where a certain feature was
2020
/// used and should be feature gated accordingly in `check_crate`.
2121
#[derive(Default)]
22-
crate struct GatedSpans {
23-
/// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
24-
crate let_chains: Lock<Vec<Span>>,
25-
/// Spans collected for gating `async_closure`, e.g. `async || ..`.
26-
crate async_closure: Lock<Vec<Span>>,
27-
/// Spans collected for gating `yield e?` expressions (`generators` gate).
28-
crate yields: Lock<Vec<Span>>,
29-
/// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
30-
crate or_patterns: Lock<Vec<Span>>,
31-
/// Spans collected for gating `const_extern_fn`, e.g. `const extern fn foo`.
32-
crate const_extern_fn: Lock<Vec<Span>>,
33-
/// Spans collected for gating `trait_alias`, e.g. `trait Foo = Ord + Eq;`.
34-
pub trait_alias: Lock<Vec<Span>>,
35-
/// Spans collected for gating `associated_type_bounds`, e.g. `Iterator<Item: Ord>`.
36-
pub associated_type_bounds: Lock<Vec<Span>>,
37-
/// Spans collected for gating `crate_visibility_modifier`, e.g. `crate fn`.
38-
pub crate_visibility_modifier: Lock<Vec<Span>>,
39-
/// Spans collected for gating `const_generics`, e.g. `const N: usize`.
40-
pub const_generics: Lock<Vec<Span>>,
41-
/// Spans collected for gating `decl_macro`, e.g. `macro m() {}`.
42-
pub decl_macro: Lock<Vec<Span>>,
43-
/// Spans collected for gating `box_patterns`, e.g. `box 0`.
44-
pub box_patterns: Lock<Vec<Span>>,
45-
/// Spans collected for gating `exclusive_range_pattern`, e.g. `0..2`.
46-
pub exclusive_range_pattern: Lock<Vec<Span>>,
47-
/// Spans collected for gating `try_blocks`, e.g. `try { a? + b? }`.
48-
pub try_blocks: Lock<Vec<Span>>,
49-
/// Spans collected for gating `label_break_value`, e.g. `'label: { ... }`.
50-
pub label_break_value: Lock<Vec<Span>>,
51-
/// Spans collected for gating `box_syntax`, e.g. `box $expr`.
52-
pub box_syntax: Lock<Vec<Span>>,
53-
/// Spans collected for gating `type_ascription`, e.g. `42: usize`.
54-
pub type_ascription: Lock<Vec<Span>>,
22+
pub struct GatedSpans {
23+
pub spans: Lock<FxHashMap<Symbol, Vec<Span>>>,
24+
}
25+
26+
impl GatedSpans {
27+
/// Feature gate the given `span` under the given `feature`
28+
/// which is same `Symbol` used in `active.rs`.
29+
pub fn gate(&self, feature: Symbol, span: Span) {
30+
self.spans
31+
.borrow_mut()
32+
.entry(feature)
33+
.or_default()
34+
.push(span);
35+
}
36+
37+
/// Ungate the last span under the given `feature`.
38+
/// Panics if the given `span` wasn't the last one.
39+
///
40+
/// Using this is discouraged unless you have a really good reason to.
41+
pub fn ungate_last(&self, feature: Symbol, span: Span) {
42+
let removed_span = self.spans
43+
.borrow_mut()
44+
.entry(feature)
45+
.or_default()
46+
.pop()
47+
.unwrap();
48+
debug_assert_eq!(span, removed_span);
49+
}
50+
51+
/// Is the provided `feature` gate ungated currently?
52+
///
53+
/// Using this is discouraged unless you have a really good reason to.
54+
pub fn is_ungated(&self, feature: Symbol) -> bool {
55+
self.spans
56+
.borrow()
57+
.get(&feature)
58+
.map_or(true, |spans| spans.is_empty())
59+
}
60+
61+
/// Prepend the given set of `spans` onto the set in `self`.
62+
pub fn merge(&self, mut spans: FxHashMap<Symbol, Vec<Span>>) {
63+
let mut inner = self.spans.borrow_mut();
64+
for (gate, mut gate_spans) in inner.drain() {
65+
spans.entry(gate).or_default().append(&mut gate_spans);
66+
}
67+
*inner = spans;
68+
}
5569
}
5670

5771
/// Info about a parsing session.
@@ -72,7 +86,7 @@ pub struct ParseSess {
7286
/// analysis.
7387
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
7488
pub injected_crate_name: Once<Symbol>,
75-
crate gated_spans: GatedSpans,
89+
pub gated_spans: GatedSpans,
7690
/// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors.
7791
pub reached_eof: Lock<bool>,
7892
}

src/libsyntax_expand/mbe/macro_rules.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use syntax_pos::Span;
2929
use rustc_data_structures::fx::FxHashMap;
3030
use std::borrow::Cow;
3131
use std::collections::hash_map::Entry;
32-
use std::slice;
32+
use std::{mem, slice};
3333

3434
use errors::Applicability;
3535
use rustc_data_structures::sync::Lrc;
@@ -182,16 +182,25 @@ fn generic_extension<'cx>(
182182

183183
// Which arm's failure should we report? (the one furthest along)
184184
let mut best_failure: Option<(Token, &str)> = None;
185-
186185
for (i, lhs) in lhses.iter().enumerate() {
187186
// try each arm's matchers
188187
let lhs_tt = match *lhs {
189188
mbe::TokenTree::Delimited(_, ref delim) => &delim.tts[..],
190189
_ => cx.span_bug(sp, "malformed macro lhs"),
191190
};
192191

192+
// Take a snapshot of the state of pre-expansion gating at this point.
193+
// This is used so that if a matcher is not `Success(..)`ful,
194+
// then the spans which became gated when parsing the unsucessful matcher
195+
// are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
196+
let mut gated_spans_snaphot = mem::take(&mut *cx.parse_sess.gated_spans.spans.borrow_mut());
197+
193198
match parse_tt(cx, lhs_tt, arg.clone()) {
194199
Success(named_matches) => {
200+
// The matcher was `Success(..)`ful.
201+
// Merge the gated spans from parsing the matcher with the pre-existing ones.
202+
cx.parse_sess.gated_spans.merge(gated_spans_snaphot);
203+
195204
let rhs = match rhses[i] {
196205
// ignore delimiters
197206
mbe::TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(),
@@ -248,6 +257,10 @@ fn generic_extension<'cx>(
248257
},
249258
Error(err_sp, ref msg) => cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..]),
250259
}
260+
261+
// The matcher was not `Success(..)`ful.
262+
// Restore to the state before snapshotting and maybe try again.
263+
mem::swap(&mut gated_spans_snaphot, &mut cx.parse_sess.gated_spans.spans.borrow_mut());
251264
}
252265

253266
let (token, label) = best_failure.expect("ran no matchers");

0 commit comments

Comments
 (0)