Skip to content

Commit b655674

Browse files
authored
Rollup merge of #63755 - Centril:simplify-prexp-gating, r=petrochenkov
Use dedicated type for spans in pre-expansion gating. - Simplify the overall pre-expansion gating "experience".
2 parents 61ae1cc + 777a12c commit b655674

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

src/libsyntax/feature_gate.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -2423,16 +2423,19 @@ pub fn check_crate(krate: &ast::Crate,
24232423
};
24242424

24252425
macro_rules! gate_all {
2426+
($gate:ident, $msg:literal) => { gate_all!($gate, $gate, $msg); };
24262427
($spans:ident, $gate:ident, $msg:literal) => {
2427-
for span in &*sess.$spans.borrow() { gate_feature!(&ctx, $gate, *span, $msg); }
2428+
for span in &*sess.gated_spans.$spans.borrow() {
2429+
gate_feature!(&ctx, $gate, *span, $msg);
2430+
}
24282431
}
24292432
}
24302433

2431-
gate_all!(param_attr_spans, param_attrs, "attributes on function parameters are unstable");
2432-
gate_all!(let_chains_spans, let_chains, "`let` expressions in this position are experimental");
2433-
gate_all!(async_closure_spans, async_closure, "async closures are unstable");
2434-
gate_all!(yield_spans, generators, "yield syntax is experimental");
2435-
gate_all!(or_pattern_spans, or_patterns, "or-patterns syntax is experimental");
2434+
gate_all!(param_attrs, "attributes on function parameters are unstable");
2435+
gate_all!(let_chains, "`let` expressions in this position are experimental");
2436+
gate_all!(async_closure, "async closures are unstable");
2437+
gate_all!(yields, generators, "yield syntax is experimental");
2438+
gate_all!(or_patterns, "or-patterns syntax is experimental");
24362439

24372440
let visitor = &mut PostExpansionVisitor {
24382441
context: &ctx,

src/libsyntax/parse/attr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \
2121
impl<'a> Parser<'a> {
2222
crate fn parse_arg_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
2323
let attrs = self.parse_outer_attributes()?;
24-
attrs.iter().for_each(|a|
25-
self.sess.param_attr_spans.borrow_mut().push(a.span)
26-
);
24+
self.sess.gated_spans.param_attrs.borrow_mut()
25+
.extend(attrs.iter().map(|a| a.span));
2726
Ok(attrs)
2827
}
2928

src/libsyntax/parse/mod.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ crate mod unescape_error_reporting;
3939

4040
pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
4141

42+
/// Collected spans during parsing for places where a certain feature was
43+
/// used and should be feature gated accordingly in `check_crate`.
44+
#[derive(Default)]
45+
pub struct GatedSpans {
46+
/// Spans collected for gating `param_attrs`, e.g. `fn foo(#[attr] x: u8) {}`.
47+
pub param_attrs: Lock<Vec<Span>>,
48+
/// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
49+
pub let_chains: Lock<Vec<Span>>,
50+
/// Spans collected for gating `async_closure`, e.g. `async || ..`.
51+
pub async_closure: Lock<Vec<Span>>,
52+
/// Spans collected for gating `yield e?` expressions (`generators` gate).
53+
pub yields: Lock<Vec<Span>>,
54+
/// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
55+
pub or_patterns: Lock<Vec<Span>>,
56+
}
57+
4258
/// Info about a parsing session.
4359
pub struct ParseSess {
4460
pub span_diagnostic: Handler,
@@ -58,16 +74,8 @@ pub struct ParseSess {
5874
/// operation token that followed it, but that the parser cannot identify without further
5975
/// analysis.
6076
pub ambiguous_block_expr_parse: Lock<FxHashMap<Span, Span>>,
61-
pub param_attr_spans: Lock<Vec<Span>>,
62-
// Places where `let` exprs were used and should be feature gated according to `let_chains`.
63-
pub let_chains_spans: Lock<Vec<Span>>,
64-
// Places where `async || ..` exprs were used and should be feature gated.
65-
pub async_closure_spans: Lock<Vec<Span>>,
66-
// Places where `yield e?` exprs were used and should be feature gated.
67-
pub yield_spans: Lock<Vec<Span>>,
6877
pub injected_crate_name: Once<Symbol>,
69-
// Places where or-patterns e.g. `Some(Foo | Bar)` were used and should be feature gated.
70-
pub or_pattern_spans: Lock<Vec<Span>>,
78+
pub gated_spans: GatedSpans,
7179
}
7280

7381
impl ParseSess {
@@ -93,12 +101,8 @@ impl ParseSess {
93101
buffered_lints: Lock::new(vec![]),
94102
edition: ExpnId::root().expn_data().edition,
95103
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
96-
param_attr_spans: Lock::new(Vec::new()),
97-
let_chains_spans: Lock::new(Vec::new()),
98-
async_closure_spans: Lock::new(Vec::new()),
99-
yield_spans: Lock::new(Vec::new()),
100104
injected_crate_name: Once::new(),
101-
or_pattern_spans: Lock::new(Vec::new()),
105+
gated_spans: GatedSpans::default(),
102106
}
103107
}
104108

src/libsyntax/parse/parser/expr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ impl<'a> Parser<'a> {
999999
}
10001000

10011001
let span = lo.to(hi);
1002-
self.sess.yield_spans.borrow_mut().push(span);
1002+
self.sess.gated_spans.yields.borrow_mut().push(span);
10031003
} else if self.eat_keyword(kw::Let) {
10041004
return self.parse_let_expr(attrs);
10051005
} else if is_span_rust_2018 && self.eat_keyword(kw::Await) {
@@ -1111,7 +1111,7 @@ impl<'a> Parser<'a> {
11111111
};
11121112
if asyncness.is_async() {
11131113
// Feature gate `async ||` closures.
1114-
self.sess.async_closure_spans.borrow_mut().push(self.prev_span);
1114+
self.sess.gated_spans.async_closure.borrow_mut().push(self.prev_span);
11151115
}
11161116

11171117
let capture_clause = self.parse_capture_clause();
@@ -1234,7 +1234,7 @@ impl<'a> Parser<'a> {
12341234

12351235
if let ExprKind::Let(..) = cond.node {
12361236
// Remove the last feature gating of a `let` expression since it's stable.
1237-
let last = self.sess.let_chains_spans.borrow_mut().pop();
1237+
let last = self.sess.gated_spans.let_chains.borrow_mut().pop();
12381238
debug_assert_eq!(cond.span, last.unwrap());
12391239
}
12401240

@@ -1252,7 +1252,7 @@ impl<'a> Parser<'a> {
12521252
|this| this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
12531253
)?;
12541254
let span = lo.to(expr.span);
1255-
self.sess.let_chains_spans.borrow_mut().push(span);
1255+
self.sess.gated_spans.let_chains.borrow_mut().push(span);
12561256
Ok(self.mk_expr(span, ExprKind::Let(pats, expr), attrs))
12571257
}
12581258

src/libsyntax/parse/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a> Parser<'a> {
123123

124124
let or_pattern_span = lo.to(self.prev_span);
125125

126-
self.sess.or_pattern_spans.borrow_mut().push(or_pattern_span);
126+
self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
127127

128128
Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
129129
}

0 commit comments

Comments
 (0)