Skip to content

Commit 67cb8e0

Browse files
committed
Auto merge of #146443 - fmease:beta-less-greedy-maybe-const-bounds, r=fmease
[beta] Backport "Less greedily parse `[const]` bounds" Backport of #146422. Following https://forge.rust-lang.org/release/backporting.html#beta-backporting-in-rust-langrust. Not part of some hypothetical future backport rollup because we're under time pressure as release procedures will commence very soon: https://forge.rust-lang.org/#current-release-versions, https://forge.rust-lang.org/release/process.html. cc `@cuviper`
2 parents f946d4d + 58afb55 commit 67cb8e0

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,12 +951,18 @@ impl<'a> Parser<'a> {
951951
|| self.check(exp!(Tilde))
952952
|| self.check_keyword(exp!(For))
953953
|| self.check(exp!(OpenParen))
954-
|| self.check(exp!(OpenBracket))
954+
|| self.can_begin_maybe_const_bound()
955955
|| self.check_keyword(exp!(Const))
956956
|| self.check_keyword(exp!(Async))
957957
|| self.check_keyword(exp!(Use))
958958
}
959959

960+
fn can_begin_maybe_const_bound(&mut self) -> bool {
961+
self.check(exp!(OpenBracket))
962+
&& self.look_ahead(1, |t| t.is_keyword(kw::Const))
963+
&& self.look_ahead(2, |t| *t == token::CloseBracket)
964+
}
965+
960966
/// Parses a bound according to the grammar:
961967
/// ```ebnf
962968
/// BOUND = TY_BOUND | LT_BOUND
@@ -1137,10 +1143,7 @@ impl<'a> Parser<'a> {
11371143
let span = tilde.to(self.prev_token.span);
11381144
self.psess.gated_spans.gate(sym::const_trait_impl, span);
11391145
BoundConstness::Maybe(span)
1140-
} else if self.check(exp!(OpenBracket))
1141-
&& self.look_ahead(1, |t| t.is_keyword(kw::Const))
1142-
&& self.look_ahead(2, |t| *t == token::CloseBracket)
1143-
{
1146+
} else if self.can_begin_maybe_const_bound() {
11441147
let start = self.token.span;
11451148
self.bump();
11461149
self.expect_keyword(exp!(Const)).unwrap();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Ensure that we don't consider `[` to begin trait bounds to contain breakages.
2+
// Only `[const]` in its entirety begins a trait bound.
3+
// See also test `macro-const-trait-bound-theoretical-regression.rs`.
4+
5+
//@ check-pass (KEEP THIS AS A PASSING TEST!)
6+
// Setting the edition to >2015 since we didn't regress `check! { dyn [const] Trait }` in Rust 2015.
7+
// See also test `traits/const-traits/macro-dyn-const-2015.rs`.
8+
//@ edition:2018
9+
10+
macro_rules! check {
11+
($ty:ty) => { compile_error!("ty"); }; // KEEP THIS RULE FIRST AND AS IS!
12+
13+
// DON'T MODIFY THE MATCHERS BELOW UNLESS THE CONST TRAIT MODIFIER SYNTAX CHANGES!
14+
15+
(dyn [$($any:tt)*] Trait) => { /* KEEP THIS EMPTY! */ };
16+
(impl [$($any:tt)*] Trait) => { /* KEEP THIS EMPTY! */ };
17+
}
18+
19+
check!(dyn [T] Trait);
20+
21+
// issue: <https://github.com/rust-lang/rust/issues/146417>
22+
check!(impl [T] Trait);
23+
check!(impl [T: Bound] Trait);
24+
25+
fn main() {}

0 commit comments

Comments
 (0)