-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reorder trait bound modifiers *after* for<...>
binder in trait bounds
#127054
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e844581
Reorder modifiers and polarity to be *after* binder in trait bounds
compiler-errors 32c8bfd
Improve error message
compiler-errors 898ed2f
Enforce that ? and for<...> are not combined
compiler-errors de88bc5
And additionally enforce ? and async/const aren't mixed
compiler-errors 79d1ac6
Remove rustdoc tests which no longer parse
compiler-errors 7da751a
Apply suggestions from code review
fmease File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -930,6 +930,7 @@ impl<'a> Parser<'a> { | |||||
/// TRAIT_BOUND_MODIFIERS = [["~"] "const"] ["async"] ["?" | "!"] | ||||||
/// ``` | ||||||
fn parse_trait_bound_modifiers(&mut self) -> PResult<'a, TraitBoundModifiers> { | ||||||
let modifier_lo = self.token.span; | ||||||
let constness = if self.eat(&token::Tilde) { | ||||||
let tilde = self.prev_token.span; | ||||||
self.expect_keyword(kw::Const)?; | ||||||
|
@@ -962,6 +963,7 @@ impl<'a> Parser<'a> { | |||||
} else { | ||||||
BoundAsyncness::Normal | ||||||
}; | ||||||
let modifier_hi = self.prev_token.span; | ||||||
|
||||||
let polarity = if self.eat(&token::Question) { | ||||||
BoundPolarity::Maybe(self.prev_token.span) | ||||||
|
@@ -972,13 +974,40 @@ impl<'a> Parser<'a> { | |||||
BoundPolarity::Positive | ||||||
}; | ||||||
|
||||||
// Enforce the mutual-exclusivity of `const`/`async` and `?`/`!`. | ||||||
match polarity { | ||||||
BoundPolarity::Positive => { | ||||||
// All trait bound modifiers allowed to combine with positive polarity | ||||||
} | ||||||
BoundPolarity::Maybe(polarity_span) | BoundPolarity::Negative(polarity_span) => { | ||||||
match (asyncness, constness) { | ||||||
(BoundAsyncness::Normal, BoundConstness::Never) => { | ||||||
// Ok, no modifiers. | ||||||
} | ||||||
(_, _) => { | ||||||
let constness = constness.as_str(); | ||||||
let asyncness = asyncness.as_str(); | ||||||
let glue = | ||||||
if !constness.is_empty() && !asyncness.is_empty() { " " } else { "" }; | ||||||
let modifiers_concatenated = format!("{constness}{glue}{asyncness}"); | ||||||
self.dcx().emit_err(errors::PolarityAndModifiers { | ||||||
polarity_span, | ||||||
polarity: polarity.as_str(), | ||||||
modifiers_span: modifier_lo.to(modifier_hi), | ||||||
modifiers_concatenated, | ||||||
}); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
Ok(TraitBoundModifiers { constness, asyncness, polarity }) | ||||||
} | ||||||
|
||||||
/// Parses a type bound according to: | ||||||
/// ```ebnf | ||||||
/// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN) | ||||||
/// TY_BOUND_NOPAREN = [TRAIT_BOUND_MODIFIERS] [for<LT_PARAM_DEFS>] SIMPLE_PATH | ||||||
/// TY_BOUND_NOPAREN = [for<LT_PARAM_DEFS>] [TRAIT_BOUND_MODIFIERS] SIMPLE_PATH | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// ``` | ||||||
/// | ||||||
/// For example, this grammar accepts `for<'a: 'b> ~const ?m::Trait<'a>`. | ||||||
|
@@ -988,16 +1017,37 @@ impl<'a> Parser<'a> { | |||||
has_parens: bool, | ||||||
leading_token: &Token, | ||||||
) -> PResult<'a, GenericBound> { | ||||||
let modifiers = self.parse_trait_bound_modifiers()?; | ||||||
let (mut lifetime_defs, binder_span) = self.parse_late_bound_lifetime_defs()?; | ||||||
|
||||||
let modifiers_lo = self.token.span; | ||||||
let modifiers = self.parse_trait_bound_modifiers()?; | ||||||
compiler-errors marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
let modifiers_span = modifiers_lo.to(self.prev_token.span); | ||||||
|
||||||
if let Some(binder_span) = binder_span { | ||||||
match modifiers.polarity { | ||||||
BoundPolarity::Negative(polarity_span) | BoundPolarity::Maybe(polarity_span) => { | ||||||
self.dcx().emit_err(errors::BinderAndPolarity { | ||||||
binder_span, | ||||||
polarity_span, | ||||||
polarity: modifiers.polarity.as_str(), | ||||||
}); | ||||||
} | ||||||
BoundPolarity::Positive => {} | ||||||
} | ||||||
} | ||||||
|
||||||
// Recover erroneous lifetime bound with modifiers or binder. | ||||||
// e.g. `T: for<'a> 'a` or `T: ~const 'a`. | ||||||
if self.token.is_lifetime() { | ||||||
let _: ErrorGuaranteed = self.error_lt_bound_with_modifiers(modifiers, binder_span); | ||||||
return self.parse_generic_lt_bound(lo, has_parens); | ||||||
} | ||||||
|
||||||
if let (more_lifetime_defs, Some(binder_span)) = self.parse_late_bound_lifetime_defs()? { | ||||||
lifetime_defs.extend(more_lifetime_defs); | ||||||
self.dcx().emit_err(errors::BinderBeforeModifiers { binder_span, modifiers_span }); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also just make this into a warning if there's too much fallout 🤔 Checking crater for failures using an error, though. |
||||||
} | ||||||
|
||||||
let mut path = if self.token.is_keyword(kw::Fn) | ||||||
&& self.look_ahead(1, |tok| tok.kind == TokenKind::OpenDelim(Delimiter::Parenthesis)) | ||||||
&& let Some(path) = self.recover_path_from_fn() | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,3 @@ where | |
i32: !Copy, | ||
{ | ||
} | ||
|
||
fn maybe_const_negative() | ||
where | ||
i32: ~const !Copy, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
//@ check-pass | ||
#![allow(dead_code)] | ||
fn f<T: ?for<'a> Sized>() {} | ||
//~^ ERROR `for<...>` binder should be placed before trait bound modifiers | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: `for<...>` binder should be placed before trait bound modifiers | ||
--> $DIR/issue-39089.rs:1:13 | ||
| | ||
LL | fn f<T: ?for<'a> Sized>() {} | ||
| - ^^^^ | ||
| | | ||
| place the `for<...>` binder before any modifiers | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
//@ compile-flags: -Z parse-only | ||
//@ edition: 2021 | ||
|
||
struct S< | ||
T: 'a + Tr, // OK | ||
T: Tr + 'a, // OK | ||
T: 'a, // OK | ||
T:, // OK | ||
T: ?for<'a> Trait, // OK | ||
T: for<'a> ?Trait, //~ ERROR `for<...>` binder not allowed with `?` trait polarity modifier | ||
T: Tr +, // OK | ||
T: ?'a, //~ ERROR `?` may only modify trait bounds, not lifetime bounds | ||
|
||
T: ~const Tr, // OK | ||
T: ~const ?Tr, // OK | ||
T: ~const ?Tr, //~ ERROR `~const` trait not allowed with `?` trait polarity modifier | ||
T: ~const Tr + 'a, // OK | ||
T: ~const 'a, //~ ERROR `~const` may only modify trait bounds, not lifetime bounds | ||
T: const 'a, //~ ERROR `const` may only modify trait bounds, not lifetime bounds | ||
|
||
T: async Tr, // OK | ||
T: async ?Tr, //~ ERROR `async` trait not allowed with `?` trait polarity modifier | ||
T: async Tr + 'a, // OK | ||
T: async 'a, //~ ERROR `async` may only modify trait bounds, not lifetime bounds | ||
|
||
T: const async Tr, // OK | ||
T: const async ?Tr, //~ ERROR `const async` trait not allowed with `?` trait polarity modifier | ||
T: const async Tr + 'a, // OK | ||
T: const async 'a, //~ ERROR `const` may only modify trait bounds, not lifetime bounds | ||
>; | ||
|
||
fn main() {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yuup, the discrepancy between the grammar snippets & the recursive descent parser (not semantically only organizationally) is giving me the ick
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the EBNF snippets in
rustc_parse
are like my worst nightmare lol, switching between different styles (EBNF is an open family of dialects after all practically speaking) and being beyond outdated. I've been meaning to make everything consistent in one big PR for a while now, but er, not sure if it's worth my energy ^^'