Skip to content

Commit 5b475a4

Browse files
committed
Suggest async {} for async || {}
Fixes #76011 This adds support for adding help diagnostics to the feature gating checks and then uses it for the async_closure gate to add the extra bit of help information as described in the issue.
1 parent 25b2f48 commit 5b475a4

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ use rustc_span::Span;
1414
use tracing::debug;
1515

1616
macro_rules! gate_feature_fn {
17+
($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $help: expr) => {{
18+
let (visitor, has_feature, span, name, explain, help) =
19+
(&*$visitor, $has_feature, $span, $name, $explain, $help);
20+
let has_feature: bool = has_feature(visitor.features);
21+
debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
22+
if !has_feature && !span.allows_unstable($name) {
23+
feature_err_issue(&visitor.sess.parse_sess, name, span, GateIssue::Language, explain)
24+
.help(help)
25+
.emit();
26+
}
27+
}};
1728
($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{
1829
let (visitor, has_feature, span, name, explain) =
1930
(&*$visitor, $has_feature, $span, $name, $explain);
@@ -27,6 +38,9 @@ macro_rules! gate_feature_fn {
2738
}
2839

2940
macro_rules! gate_feature_post {
41+
($visitor: expr, $feature: ident, $span: expr, $explain: expr, $help: expr) => {
42+
gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain, $help)
43+
};
3044
($visitor: expr, $feature: ident, $span: expr, $explain: expr) => {
3145
gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain)
3246
};
@@ -613,6 +627,13 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
613627

614628
let spans = sess.parse_sess.gated_spans.spans.borrow();
615629
macro_rules! gate_all {
630+
($gate:ident, $msg:literal, $help:literal) => {
631+
if let Some(spans) = spans.get(&sym::$gate) {
632+
for span in spans {
633+
gate_feature_post!(&visitor, $gate, *span, $msg, $help);
634+
}
635+
}
636+
};
616637
($gate:ident, $msg:literal) => {
617638
if let Some(spans) = spans.get(&sym::$gate) {
618639
for span in spans {
@@ -623,7 +644,11 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
623644
}
624645
gate_all!(if_let_guard, "`if let` guard is not implemented");
625646
gate_all!(let_chains, "`let` expressions in this position are experimental");
626-
gate_all!(async_closure, "async closures are unstable");
647+
gate_all!(
648+
async_closure,
649+
"async closures are unstable",
650+
"to use an async block, remove the `||`: `async {`"
651+
);
627652
gate_all!(generators, "yield syntax is experimental");
628653
gate_all!(or_patterns, "or-patterns syntax is experimental");
629654
gate_all!(raw_ref_op, "raw address of syntax is experimental");

compiler/rustc_parse/src/parser/expr.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1554,10 +1554,6 @@ impl<'a> Parser<'a> {
15541554
} else {
15551555
Async::No
15561556
};
1557-
if let Async::Yes { span, .. } = asyncness {
1558-
// Feature-gate `async ||` closures.
1559-
self.sess.gated_spans.gate(sym::async_closure, span);
1560-
}
15611557

15621558
let capture_clause = self.parse_capture_clause();
15631559
let decl = self.parse_fn_block_decl()?;
@@ -1574,6 +1570,11 @@ impl<'a> Parser<'a> {
15741570
}
15751571
};
15761572

1573+
if let Async::Yes { span, .. } = asyncness {
1574+
// Feature-gate `async ||` closures.
1575+
self.sess.gated_spans.gate(sym::async_closure, span);
1576+
}
1577+
15771578
Ok(self.mk_expr(
15781579
lo.to(body.span),
15791580
ExprKind::Closure(capture_clause, asyncness, movability, decl, body, lo.to(decl_hi)),

src/test/ui/async-await/feature-async-closure.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | let _ = async || {};
66
|
77
= note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
88
= help: add `#![feature(async_closure)]` to the crate attributes to enable
9+
= help: to use an async block, remove the `||`: `async {`
910

1011
error: aborting due to previous error
1112

src/test/ui/parser/block-no-opening-brace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fn f4() {
2626
}
2727

2828
fn f5() {
29-
async //~ ERROR async closures are unstable
29+
async
3030
let x = 0; //~ ERROR expected one of `move`, `|`, or `||`, found keyword `let`
3131
}

src/test/ui/parser/block-no-opening-brace.stderr

+1-11
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,5 @@ LL | async
3939
LL | let x = 0;
4040
| ^^^ unexpected token
4141

42-
error[E0658]: async closures are unstable
43-
--> $DIR/block-no-opening-brace.rs:29:5
44-
|
45-
LL | async
46-
| ^^^^^
47-
|
48-
= note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
49-
= help: add `#![feature(async_closure)]` to the crate attributes to enable
50-
51-
error: aborting due to 6 previous errors
42+
error: aborting due to 5 previous errors
5243

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

0 commit comments

Comments
 (0)