Skip to content

Commit 916a8c3

Browse files
authored
Rollup merge of rust-lang#63545 - Centril:gate-yield-preexp, r=oli-obk
Feature gate 'yield $expr?' pre-expansion Also improve the overall ergonomics of pre-expansion gating in general. r? @Zoxc
2 parents 8bb9663 + 4fe201c commit 916a8c3

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

src/libsyntax/feature_gate.rs

+10-30
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use crate::tokenstream::TokenTree;
3030

3131
use errors::{Applicability, DiagnosticBuilder, Handler};
3232
use rustc_data_structures::fx::FxHashMap;
33-
use rustc_data_structures::sync::Lock;
3433
use rustc_target::spec::abi::Abi;
3534
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
3635
use log::debug;
@@ -2088,11 +2087,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
20882087
"type ascription is experimental");
20892088
}
20902089
}
2091-
ast::ExprKind::Yield(..) => {
2092-
gate_feature_post!(&self, generators,
2093-
e.span,
2094-
"yield syntax is experimental");
2095-
}
20962090
ast::ExprKind::TryBlock(_) => {
20972091
gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
20982092
}
@@ -2427,10 +2421,6 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute],
24272421
features
24282422
}
24292423

2430-
fn for_each_in_lock<T>(vec: &Lock<Vec<T>>, f: impl Fn(&T)) {
2431-
vec.borrow().iter().for_each(f);
2432-
}
2433-
24342424
pub fn check_crate(krate: &ast::Crate,
24352425
sess: &ParseSess,
24362426
features: &Features,
@@ -2443,26 +2433,16 @@ pub fn check_crate(krate: &ast::Crate,
24432433
plugin_attributes,
24442434
};
24452435

2446-
for_each_in_lock(&sess.param_attr_spans, |span| gate_feature!(
2447-
&ctx,
2448-
param_attrs,
2449-
*span,
2450-
"attributes on function parameters are unstable"
2451-
));
2452-
2453-
for_each_in_lock(&sess.let_chains_spans, |span| gate_feature!(
2454-
&ctx,
2455-
let_chains,
2456-
*span,
2457-
"`let` expressions in this position are experimental"
2458-
));
2459-
2460-
for_each_in_lock(&sess.async_closure_spans, |span| gate_feature!(
2461-
&ctx,
2462-
async_closure,
2463-
*span,
2464-
"async closures are unstable"
2465-
));
2436+
macro_rules! gate_all {
2437+
($spans:ident, $gate:ident, $msg:literal) => {
2438+
for span in &*sess.$spans.borrow() { gate_feature!(&ctx, $gate, *span, $msg); }
2439+
}
2440+
}
2441+
2442+
gate_all!(param_attr_spans, param_attrs, "attributes on function parameters are unstable");
2443+
gate_all!(let_chains_spans, let_chains, "`let` expressions in this position are experimental");
2444+
gate_all!(async_closure_spans, async_closure, "async closures are unstable");
2445+
gate_all!(yield_spans, generators, "yield syntax is experimental");
24662446

24672447
let visitor = &mut PostExpansionVisitor {
24682448
context: &ctx,

src/libsyntax/parse/lexer/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess {
3434
param_attr_spans: Lock::new(Vec::new()),
3535
let_chains_spans: Lock::new(Vec::new()),
3636
async_closure_spans: Lock::new(Vec::new()),
37+
yield_spans: Lock::new(Vec::new()),
3738
injected_crate_name: Once::new(),
3839
}
3940
}

src/libsyntax/parse/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub struct ParseSess {
6262
pub let_chains_spans: Lock<Vec<Span>>,
6363
// Places where `async || ..` exprs were used and should be feature gated.
6464
pub async_closure_spans: Lock<Vec<Span>>,
65+
// Places where `yield e?` exprs were used and should be feature gated.
66+
pub yield_spans: Lock<Vec<Span>>,
6567
pub injected_crate_name: Once<Symbol>,
6668
}
6769

@@ -91,6 +93,7 @@ impl ParseSess {
9193
param_attr_spans: Lock::new(Vec::new()),
9294
let_chains_spans: Lock::new(Vec::new()),
9395
async_closure_spans: Lock::new(Vec::new()),
96+
yield_spans: Lock::new(Vec::new()),
9497
injected_crate_name: Once::new(),
9598
}
9699
}

src/libsyntax/parse/parser/expr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@ impl<'a> Parser<'a> {
997997
} else {
998998
ex = ExprKind::Yield(None);
999999
}
1000+
1001+
let span = lo.to(hi);
1002+
self.sess.yield_spans.borrow_mut().push(span);
10001003
} else if self.eat_keyword(kw::Let) {
10011004
return self.parse_let_expr(attrs);
10021005
} else if is_span_rust_2018 && self.eat_keyword(kw::Await) {

src/test/ui/feature-gates/feature-gate-generators.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ fn main() {
22
yield true; //~ ERROR yield syntax is experimental
33
//~^ ERROR yield statement outside of generator literal
44
}
5+
6+
#[cfg(FALSE)]
7+
fn foo() {
8+
yield; //~ ERROR yield syntax is experimental
9+
yield 0; //~ ERROR yield syntax is experimental
10+
}

src/test/ui/feature-gates/feature-gate-generators.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,30 @@ LL | yield true;
77
= note: for more information, see https://github.com/rust-lang/rust/issues/43122
88
= help: add `#![feature(generators)]` to the crate attributes to enable
99

10+
error[E0658]: yield syntax is experimental
11+
--> $DIR/feature-gate-generators.rs:8:5
12+
|
13+
LL | yield;
14+
| ^^^^^
15+
|
16+
= note: for more information, see https://github.com/rust-lang/rust/issues/43122
17+
= help: add `#![feature(generators)]` to the crate attributes to enable
18+
19+
error[E0658]: yield syntax is experimental
20+
--> $DIR/feature-gate-generators.rs:9:5
21+
|
22+
LL | yield 0;
23+
| ^^^^^^^
24+
|
25+
= note: for more information, see https://github.com/rust-lang/rust/issues/43122
26+
= help: add `#![feature(generators)]` to the crate attributes to enable
27+
1028
error[E0627]: yield statement outside of generator literal
1129
--> $DIR/feature-gate-generators.rs:2:5
1230
|
1331
LL | yield true;
1432
| ^^^^^^^^^^
1533

16-
error: aborting due to 2 previous errors
34+
error: aborting due to 4 previous errors
1735

1836
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)