And additionally enforce ? and async/const aren't mixed
This commit is contained in:
parent
898ed2ffa6
commit
de88bc5c89
10 changed files with 120 additions and 44 deletions
|
@ -575,6 +575,9 @@ parse_missing_trait_in_trait_impl = missing trait in a trait impl
|
|||
parse_modifier_lifetime = `{$modifier}` may only modify trait bounds, not lifetime bounds
|
||||
.suggestion = remove the `{$modifier}`
|
||||
|
||||
parse_modifiers_and_polarity = `{$modifiers_concatenated}` trait not allowed with `{$polarity}` trait polarity modifier
|
||||
.label = there is not a well-defined meaning for a `{$modifiers_concatenated} {$polarity}` trait
|
||||
|
||||
parse_more_than_one_char = character literal may only contain one codepoint
|
||||
.followed_by = this `{$chr}` is followed by the combining {$len ->
|
||||
[one] mark
|
||||
|
|
|
@ -3060,3 +3060,14 @@ pub struct BinderAndPolarity {
|
|||
pub binder_span: Span,
|
||||
pub polarity: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(parse_modifiers_and_polarity)]
|
||||
pub struct PolarityAndModifiers {
|
||||
#[primary_span]
|
||||
pub polarity_span: Span,
|
||||
#[label]
|
||||
pub modifiers_span: Span,
|
||||
pub polarity: &'static str,
|
||||
pub modifiers_concatenated: String,
|
||||
}
|
||||
|
|
|
@ -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,6 +974,33 @@ 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 })
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue