Skip to content

Commit c3159b8

Browse files
Gate const closures even when they appear in macros
1 parent bd43555 commit c3159b8

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
422422
ast::ExprKind::TryBlock(_) => {
423423
gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
424424
}
425-
ast::ExprKind::Closure(box ast::Closure { constness: ast::Const::Yes(_), .. }) => {
426-
gate_feature_post!(
427-
&self,
428-
const_closures,
429-
e.span,
430-
"const closures are experimental"
431-
);
432-
}
433425
_ => {}
434426
}
435427
visit::walk_expr(self, e)
@@ -592,6 +584,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
592584
gate_all!(associated_const_equality, "associated const equality is incomplete");
593585
gate_all!(yeet_expr, "`do yeet` expression is experimental");
594586
gate_all!(dyn_star, "`dyn*` trait objects are experimental");
587+
gate_all!(const_closures, "const closures are experimental");
595588

596589
// All uses of `gate_all!` below this point were added in #65742,
597590
// and subsequently disabled (with the non-early gating readded).

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2105,7 +2105,7 @@ impl<'a> Parser<'a> {
21052105
ClosureBinder::NotPresent
21062106
};
21072107

2108-
let constness = self.parse_closure_constness(Case::Sensitive);
2108+
let constness = self.parse_closure_constness();
21092109

21102110
let movability =
21112111
if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable };

compiler/rustc_parse/src/parser/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1196,9 +1196,13 @@ impl<'a> Parser<'a> {
11961196
self.parse_constness_(case, false)
11971197
}
11981198

1199-
/// Parses constness for closures
1200-
fn parse_closure_constness(&mut self, case: Case) -> Const {
1201-
self.parse_constness_(case, true)
1199+
/// Parses constness for closures (case sensitive, feature-gated)
1200+
fn parse_closure_constness(&mut self) -> Const {
1201+
let constness = self.parse_constness_(Case::Sensitive, true);
1202+
if let Const::Yes(span) = constness {
1203+
self.sess.gated_spans.gate(sym::const_closures, span);
1204+
}
1205+
constness
12021206
}
12031207

12041208
fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// gate-test-const_closures
2+
23
fn main() {
34
(const || {})();
45
//~^ ERROR: const closures are experimental
56
}
7+
8+
macro_rules! e {
9+
($e:expr) => {}
10+
}
11+
12+
e!((const || {}));
13+
//~^ ERROR const closures are experimental
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
error[E0658]: const closures are experimental
2-
--> $DIR/gate.rs:3:6
2+
--> $DIR/gate.rs:4:6
33
|
44
LL | (const || {})();
5-
| ^^^^^^^^^^^
5+
| ^^^^^
66
|
77
= note: see issue #106003 <https://github.com/rust-lang/rust/issues/106003> for more information
88
= help: add `#![feature(const_closures)]` to the crate attributes to enable
99

10-
error: aborting due to previous error
10+
error[E0658]: const closures are experimental
11+
--> $DIR/gate.rs:12:5
12+
|
13+
LL | e!((const || {}));
14+
| ^^^^^
15+
|
16+
= note: see issue #106003 <https://github.com/rust-lang/rust/issues/106003> for more information
17+
= help: add `#![feature(const_closures)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
1120

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

0 commit comments

Comments
 (0)