parse_generic_bounds: account for negative lifetime bounds
This commit is contained in:
parent
4625ba4872
commit
b5f00beaa5
@ -375,9 +375,9 @@ impl<'a> Parser<'a> {
|
||||
let last_span = *negative_bounds.last().unwrap();
|
||||
let mut err = self.struct_span_err(
|
||||
negative_bounds,
|
||||
"negative trait bounds are not supported",
|
||||
"negative bounds are not supported",
|
||||
);
|
||||
err.span_label(last_span, "negative trait bounds are not supported");
|
||||
err.span_label(last_span, "negative bounds are not supported");
|
||||
if let Some(bound_list) = colon_span {
|
||||
let bound_list = bound_list.to(self.prev_span);
|
||||
let mut new_bound_list = String::new();
|
||||
@ -392,7 +392,7 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
err.span_suggestion_hidden(
|
||||
bound_list,
|
||||
&format!("remove the trait bound{}", pluralize!(negative_bounds_len)),
|
||||
&format!("remove the bound{}", pluralize!(negative_bounds_len)),
|
||||
new_bound_list,
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
@ -418,25 +418,23 @@ impl<'a> Parser<'a> {
|
||||
/// ```
|
||||
/// BOUND = TY_BOUND | LT_BOUND
|
||||
/// ```
|
||||
fn parse_generic_bound(
|
||||
&mut self,
|
||||
) -> PResult<'a, Result<GenericBound, Span>> {
|
||||
fn parse_generic_bound(&mut self) -> PResult<'a, Result<GenericBound, Span>> {
|
||||
let anchor_lo = self.prev_span;
|
||||
let lo = self.token.span;
|
||||
let has_parens = self.eat(&token::OpenDelim(token::Paren));
|
||||
let inner_lo = self.token.span;
|
||||
let is_negative = self.eat(&token::Not);
|
||||
let question = self.eat(&token::Question).then_some(self.prev_span);
|
||||
if self.token.is_lifetime() {
|
||||
Ok(Ok(self.parse_generic_lt_bound(lo, inner_lo, has_parens, question)?))
|
||||
let bound = if self.token.is_lifetime() {
|
||||
self.parse_generic_lt_bound(lo, inner_lo, has_parens, question)?
|
||||
} else {
|
||||
let (poly_span, bound) = self.parse_generic_ty_bound(lo, has_parens, question)?;
|
||||
if is_negative {
|
||||
Ok(Err(anchor_lo.to(poly_span)))
|
||||
} else {
|
||||
Ok(Ok(bound))
|
||||
}
|
||||
}
|
||||
self.parse_generic_ty_bound(lo, has_parens, question)?
|
||||
};
|
||||
Ok(if is_negative {
|
||||
Err(anchor_lo.to(self.prev_span))
|
||||
} else {
|
||||
Ok(bound)
|
||||
})
|
||||
}
|
||||
|
||||
/// Parses a lifetime ("outlives") bound, e.g. `'a`, according to:
|
||||
@ -497,16 +495,15 @@ impl<'a> Parser<'a> {
|
||||
lo: Span,
|
||||
has_parens: bool,
|
||||
question: Option<Span>,
|
||||
) -> PResult<'a, (Span, GenericBound)> {
|
||||
) -> PResult<'a, GenericBound> {
|
||||
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
|
||||
let path = self.parse_path(PathStyle::Type)?;
|
||||
if has_parens {
|
||||
self.expect(&token::CloseDelim(token::Paren))?;
|
||||
}
|
||||
let poly_span = lo.to(self.prev_span);
|
||||
let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span);
|
||||
let poly_trait = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_span));
|
||||
let modifier = question.map_or(TraitBoundModifier::None, |_| TraitBoundModifier::Maybe);
|
||||
Ok((poly_span, GenericBound::Trait(poly_trait, modifier)))
|
||||
Ok(GenericBound::Trait(poly_trait, modifier))
|
||||
}
|
||||
|
||||
pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {
|
||||
|
@ -2,6 +2,6 @@ struct Conj<A> {a : A}
|
||||
trait Valid {}
|
||||
|
||||
impl<A: !Valid> Conj<A>{}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
|
||||
fn main() {}
|
||||
|
@ -1,10 +1,10 @@
|
||||
error: negative trait bounds are not supported
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-58857.rs:4:7
|
||||
|
|
||||
LL | impl<A: !Valid> Conj<A>{}
|
||||
| ^^^^^^^^ negative trait bounds are not supported
|
||||
| ^^^^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the trait bound
|
||||
= help: remove the bound
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
// run-rustfix
|
||||
|
||||
trait Tr {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr2: SuperA {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr3: SuperB {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr4: SuperB + SuperD {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr5 {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
|
||||
trait SuperA {}
|
||||
trait SuperB {}
|
||||
|
@ -1,17 +1,17 @@
|
||||
// run-rustfix
|
||||
|
||||
trait Tr: !SuperA {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr2: SuperA + !SuperB {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr3: !SuperA + SuperB {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr4: !SuperA + SuperB
|
||||
+ !SuperC + SuperD {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
trait Tr5: !SuperA
|
||||
+ !SuperB {}
|
||||
//~^ ERROR negative trait bounds are not supported
|
||||
//~^ ERROR negative bounds are not supported
|
||||
|
||||
trait SuperA {}
|
||||
trait SuperB {}
|
||||
|
@ -1,46 +1,46 @@
|
||||
error: negative trait bounds are not supported
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-33418.rs:3:9
|
||||
|
|
||||
LL | trait Tr: !SuperA {}
|
||||
| ^^^^^^^^^ negative trait bounds are not supported
|
||||
| ^^^^^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the trait bound
|
||||
= help: remove the bound
|
||||
|
||||
error: negative trait bounds are not supported
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-33418.rs:5:19
|
||||
|
|
||||
LL | trait Tr2: SuperA + !SuperB {}
|
||||
| ^^^^^^^^^ negative trait bounds are not supported
|
||||
| ^^^^^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the trait bound
|
||||
= help: remove the bound
|
||||
|
||||
error: negative trait bounds are not supported
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-33418.rs:7:10
|
||||
|
|
||||
LL | trait Tr3: !SuperA + SuperB {}
|
||||
| ^^^^^^^^^ negative trait bounds are not supported
|
||||
| ^^^^^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the trait bound
|
||||
= help: remove the bound
|
||||
|
||||
error: negative trait bounds are not supported
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-33418.rs:9:10
|
||||
|
|
||||
LL | trait Tr4: !SuperA + SuperB
|
||||
| ^^^^^^^^^
|
||||
LL | + !SuperC + SuperD {}
|
||||
| ^^^^^^^^^ negative trait bounds are not supported
|
||||
| ^^^^^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the trait bounds
|
||||
= help: remove the bounds
|
||||
|
||||
error: negative trait bounds are not supported
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-33418.rs:12:10
|
||||
|
|
||||
LL | trait Tr5: !SuperA
|
||||
| ^^^^^^^^^
|
||||
LL | + !SuperB {}
|
||||
| ^^^^^^^^^ negative trait bounds are not supported
|
||||
| ^^^^^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the trait bounds
|
||||
= help: remove the bounds
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
|
@ -0,0 +1,12 @@
|
||||
// In this regression test for #67146, we check that the
|
||||
// negative outlives bound `!'a` is rejected by the parser.
|
||||
// This regression was first introduced in PR #57364.
|
||||
|
||||
fn main() {}
|
||||
|
||||
fn f1<T: !'static>() {}
|
||||
//~^ ERROR negative bounds are not supported
|
||||
fn f2<'a, T: Ord + !'a>() {}
|
||||
//~^ ERROR negative bounds are not supported
|
||||
fn f3<'a, T: !'a + Ord>() {}
|
||||
//~^ ERROR negative bounds are not supported
|
@ -0,0 +1,26 @@
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-67146-negative-outlives-bound-syntactic-fail.rs:7:8
|
||||
|
|
||||
LL | fn f1<T: !'static>() {}
|
||||
| ^^^^^^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the bound
|
||||
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-67146-negative-outlives-bound-syntactic-fail.rs:9:18
|
||||
|
|
||||
LL | fn f2<'a, T: Ord + !'a>() {}
|
||||
| ^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the bound
|
||||
|
||||
error: negative bounds are not supported
|
||||
--> $DIR/issue-67146-negative-outlives-bound-syntactic-fail.rs:11:12
|
||||
|
|
||||
LL | fn f3<'a, T: !'a + Ord>() {}
|
||||
| ^^^^^ negative bounds are not supported
|
||||
|
|
||||
= help: remove the bound
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
Loading…
x
Reference in New Issue
Block a user