Skip to content

Commit 98fea68

Browse files
authored
Rollup merge of #109029 - compiler-errors:parse-gating, r=jackh726
Gate usages of `dyn*` and const closures in macros We silently accepted `dyn*` and const closures in macros as long as they didn't expand to anything containing these experimental features, unlike other gated features such as `for<'a>` binders on closures, etc. Let's not do that, to make sure nobody begins relying on this.
2 parents b28775c + c3159b8 commit 98fea68

File tree

12 files changed

+61
-27
lines changed

12 files changed

+61
-27
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
337337
ast::TyKind::Never => {
338338
gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
339339
}
340-
ast::TyKind::TraitObject(_, ast::TraitObjectSyntax::DynStar, ..) => {
341-
gate_feature_post!(&self, dyn_star, ty.span, "dyn* trait objects are unstable");
342-
}
343340
_ => {}
344341
}
345342
visit::walk_ty(self, ty)
@@ -425,14 +422,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
425422
ast::ExprKind::TryBlock(_) => {
426423
gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
427424
}
428-
ast::ExprKind::Closure(box ast::Closure { constness: ast::Const::Yes(_), .. }) => {
429-
gate_feature_post!(
430-
&self,
431-
const_closures,
432-
e.span,
433-
"const closures are experimental"
434-
);
435-
}
436425
_ => {}
437426
}
438427
visit::walk_expr(self, e)
@@ -594,6 +583,8 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
594583
gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
595584
gate_all!(associated_const_equality, "associated const equality is incomplete");
596585
gate_all!(yeet_expr, "`do yeet` expression is experimental");
586+
gate_all!(dyn_star, "`dyn*` trait objects are experimental");
587+
gate_all!(const_closures, "const closures are experimental");
597588

598589
// All uses of `gate_all!` below this point were added in #65742,
599590
// 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 {

compiler/rustc_parse/src/parser/ty.rs

+2
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,12 @@ impl<'a> Parser<'a> {
624624
///
625625
/// Note that this does *not* parse bare trait objects.
626626
fn parse_dyn_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
627+
let lo = self.token.span;
627628
self.bump(); // `dyn`
628629

629630
// parse dyn* types
630631
let syntax = if self.eat(&TokenKind::BinOp(token::Star)) {
632+
self.sess.gated_spans.gate(sym::dyn_star, lo.to(self.prev_token.span));
631633
TraitObjectSyntax::DynStar
632634
} else {
633635
TraitObjectSyntax::Dyn

tests/ui/dyn-star/feature-gate-dyn_star.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// dyn* is not necessarily the final surface syntax (if we have one at all),
44
/// but for now we will support it to aid in writing tests independently.
55
pub fn dyn_star_parameter(_: &dyn* Send) {
6-
//~^ dyn* trait objects are unstable
6+
//~^ `dyn*` trait objects are experimental
77
}
88

99
fn main() {}

tests/ui/dyn-star/feature-gate-dyn_star.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0658]: dyn* trait objects are unstable
1+
error[E0658]: `dyn*` trait objects are experimental
22
--> $DIR/feature-gate-dyn_star.rs:5:31
33
|
44
LL | pub fn dyn_star_parameter(_: &dyn* Send) {
5-
| ^^^^^^^^^
5+
| ^^^^
66
|
77
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information
88
= help: add `#![feature(dyn_star)]` to the crate attributes to enable

tests/ui/dyn-star/gated-span.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
macro_rules! t {
2+
($t:ty) => {}
3+
}
4+
5+
t!(dyn* Send);
6+
//~^ ERROR `dyn*` trait objects are experimental
7+
8+
fn main() {}

tests/ui/dyn-star/gated-span.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: `dyn*` trait objects are experimental
2+
--> $DIR/gated-span.rs:5:4
3+
|
4+
LL | t!(dyn* Send);
5+
| ^^^^
6+
|
7+
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information
8+
= help: add `#![feature(dyn_star)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

tests/ui/dyn-star/no-explicit-dyn-star-cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ fn make_dyn_star() {
44
let i = 42usize;
55
let dyn_i: dyn* Debug = i as dyn* Debug;
66
//~^ ERROR casting `usize` as `dyn* Debug` is invalid
7-
//~| ERROR dyn* trait objects are unstable
8-
//~| ERROR dyn* trait objects are unstable
7+
//~| ERROR `dyn*` trait objects are experimental
8+
//~| ERROR `dyn*` trait objects are experimental
99
}
1010

1111
fn main() {

tests/ui/dyn-star/no-explicit-dyn-star-cast.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
error[E0658]: dyn* trait objects are unstable
1+
error[E0658]: `dyn*` trait objects are experimental
22
--> $DIR/no-explicit-dyn-star-cast.rs:5:16
33
|
44
LL | let dyn_i: dyn* Debug = i as dyn* Debug;
5-
| ^^^^^^^^^^
5+
| ^^^^
66
|
77
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information
88
= help: add `#![feature(dyn_star)]` to the crate attributes to enable
99

10-
error[E0658]: dyn* trait objects are unstable
10+
error[E0658]: `dyn*` trait objects are experimental
1111
--> $DIR/no-explicit-dyn-star-cast.rs:5:34
1212
|
1313
LL | let dyn_i: dyn* Debug = i as dyn* Debug;
14-
| ^^^^^^^^^^
14+
| ^^^^
1515
|
1616
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information
1717
= help: add `#![feature(dyn_star)]` to the crate attributes to enable
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)