Skip to content

Commit b5f00be

Browse files
committed
parse_generic_bounds: account for negative lifetime bounds
1 parent 4625ba4 commit b5f00be

8 files changed

+83
-48
lines changed

src/librustc_parse/parser/ty.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ impl<'a> Parser<'a> {
375375
let last_span = *negative_bounds.last().unwrap();
376376
let mut err = self.struct_span_err(
377377
negative_bounds,
378-
"negative trait bounds are not supported",
378+
"negative bounds are not supported",
379379
);
380-
err.span_label(last_span, "negative trait bounds are not supported");
380+
err.span_label(last_span, "negative bounds are not supported");
381381
if let Some(bound_list) = colon_span {
382382
let bound_list = bound_list.to(self.prev_span);
383383
let mut new_bound_list = String::new();
@@ -392,7 +392,7 @@ impl<'a> Parser<'a> {
392392
}
393393
err.span_suggestion_hidden(
394394
bound_list,
395-
&format!("remove the trait bound{}", pluralize!(negative_bounds_len)),
395+
&format!("remove the bound{}", pluralize!(negative_bounds_len)),
396396
new_bound_list,
397397
Applicability::MachineApplicable,
398398
);
@@ -418,25 +418,23 @@ impl<'a> Parser<'a> {
418418
/// ```
419419
/// BOUND = TY_BOUND | LT_BOUND
420420
/// ```
421-
fn parse_generic_bound(
422-
&mut self,
423-
) -> PResult<'a, Result<GenericBound, Span>> {
421+
fn parse_generic_bound(&mut self) -> PResult<'a, Result<GenericBound, Span>> {
424422
let anchor_lo = self.prev_span;
425423
let lo = self.token.span;
426424
let has_parens = self.eat(&token::OpenDelim(token::Paren));
427425
let inner_lo = self.token.span;
428426
let is_negative = self.eat(&token::Not);
429427
let question = self.eat(&token::Question).then_some(self.prev_span);
430-
if self.token.is_lifetime() {
431-
Ok(Ok(self.parse_generic_lt_bound(lo, inner_lo, has_parens, question)?))
428+
let bound = if self.token.is_lifetime() {
429+
self.parse_generic_lt_bound(lo, inner_lo, has_parens, question)?
432430
} else {
433-
let (poly_span, bound) = self.parse_generic_ty_bound(lo, has_parens, question)?;
434-
if is_negative {
435-
Ok(Err(anchor_lo.to(poly_span)))
436-
} else {
437-
Ok(Ok(bound))
438-
}
439-
}
431+
self.parse_generic_ty_bound(lo, has_parens, question)?
432+
};
433+
Ok(if is_negative {
434+
Err(anchor_lo.to(self.prev_span))
435+
} else {
436+
Ok(bound)
437+
})
440438
}
441439

442440
/// Parses a lifetime ("outlives") bound, e.g. `'a`, according to:
@@ -497,16 +495,15 @@ impl<'a> Parser<'a> {
497495
lo: Span,
498496
has_parens: bool,
499497
question: Option<Span>,
500-
) -> PResult<'a, (Span, GenericBound)> {
498+
) -> PResult<'a, GenericBound> {
501499
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
502500
let path = self.parse_path(PathStyle::Type)?;
503501
if has_parens {
504502
self.expect(&token::CloseDelim(token::Paren))?;
505503
}
506-
let poly_span = lo.to(self.prev_span);
507-
let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span);
504+
let poly_trait = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_span));
508505
let modifier = question.map_or(TraitBoundModifier::None, |_| TraitBoundModifier::Maybe);
509-
Ok((poly_span, GenericBound::Trait(poly_trait, modifier)))
506+
Ok(GenericBound::Trait(poly_trait, modifier))
510507
}
511508

512509
pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {

src/test/ui/issues/issue-58857.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ struct Conj<A> {a : A}
22
trait Valid {}
33

44
impl<A: !Valid> Conj<A>{}
5-
//~^ ERROR negative trait bounds are not supported
5+
//~^ ERROR negative bounds are not supported
66

77
fn main() {}

src/test/ui/issues/issue-58857.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: negative trait bounds are not supported
1+
error: negative bounds are not supported
22
--> $DIR/issue-58857.rs:4:7
33
|
44
LL | impl<A: !Valid> Conj<A>{}
5-
| ^^^^^^^^ negative trait bounds are not supported
5+
| ^^^^^^^^ negative bounds are not supported
66
|
7-
= help: remove the trait bound
7+
= help: remove the bound
88

99
error: aborting due to previous error
1010

src/test/ui/parser/issue-33418.fixed

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// run-rustfix
22

33
trait Tr {}
4-
//~^ ERROR negative trait bounds are not supported
4+
//~^ ERROR negative bounds are not supported
55
trait Tr2: SuperA {}
6-
//~^ ERROR negative trait bounds are not supported
6+
//~^ ERROR negative bounds are not supported
77
trait Tr3: SuperB {}
8-
//~^ ERROR negative trait bounds are not supported
8+
//~^ ERROR negative bounds are not supported
99
trait Tr4: SuperB + SuperD {}
10-
//~^ ERROR negative trait bounds are not supported
10+
//~^ ERROR negative bounds are not supported
1111
trait Tr5 {}
12-
//~^ ERROR negative trait bounds are not supported
12+
//~^ ERROR negative bounds are not supported
1313

1414
trait SuperA {}
1515
trait SuperB {}

src/test/ui/parser/issue-33418.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// run-rustfix
22

33
trait Tr: !SuperA {}
4-
//~^ ERROR negative trait bounds are not supported
4+
//~^ ERROR negative bounds are not supported
55
trait Tr2: SuperA + !SuperB {}
6-
//~^ ERROR negative trait bounds are not supported
6+
//~^ ERROR negative bounds are not supported
77
trait Tr3: !SuperA + SuperB {}
8-
//~^ ERROR negative trait bounds are not supported
8+
//~^ ERROR negative bounds are not supported
99
trait Tr4: !SuperA + SuperB
1010
+ !SuperC + SuperD {}
11-
//~^ ERROR negative trait bounds are not supported
11+
//~^ ERROR negative bounds are not supported
1212
trait Tr5: !SuperA
1313
+ !SuperB {}
14-
//~^ ERROR negative trait bounds are not supported
14+
//~^ ERROR negative bounds are not supported
1515

1616
trait SuperA {}
1717
trait SuperB {}

src/test/ui/parser/issue-33418.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
error: negative trait bounds are not supported
1+
error: negative bounds are not supported
22
--> $DIR/issue-33418.rs:3:9
33
|
44
LL | trait Tr: !SuperA {}
5-
| ^^^^^^^^^ negative trait bounds are not supported
5+
| ^^^^^^^^^ negative bounds are not supported
66
|
7-
= help: remove the trait bound
7+
= help: remove the bound
88

9-
error: negative trait bounds are not supported
9+
error: negative bounds are not supported
1010
--> $DIR/issue-33418.rs:5:19
1111
|
1212
LL | trait Tr2: SuperA + !SuperB {}
13-
| ^^^^^^^^^ negative trait bounds are not supported
13+
| ^^^^^^^^^ negative bounds are not supported
1414
|
15-
= help: remove the trait bound
15+
= help: remove the bound
1616

17-
error: negative trait bounds are not supported
17+
error: negative bounds are not supported
1818
--> $DIR/issue-33418.rs:7:10
1919
|
2020
LL | trait Tr3: !SuperA + SuperB {}
21-
| ^^^^^^^^^ negative trait bounds are not supported
21+
| ^^^^^^^^^ negative bounds are not supported
2222
|
23-
= help: remove the trait bound
23+
= help: remove the bound
2424

25-
error: negative trait bounds are not supported
25+
error: negative bounds are not supported
2626
--> $DIR/issue-33418.rs:9:10
2727
|
2828
LL | trait Tr4: !SuperA + SuperB
2929
| ^^^^^^^^^
3030
LL | + !SuperC + SuperD {}
31-
| ^^^^^^^^^ negative trait bounds are not supported
31+
| ^^^^^^^^^ negative bounds are not supported
3232
|
33-
= help: remove the trait bounds
33+
= help: remove the bounds
3434

35-
error: negative trait bounds are not supported
35+
error: negative bounds are not supported
3636
--> $DIR/issue-33418.rs:12:10
3737
|
3838
LL | trait Tr5: !SuperA
3939
| ^^^^^^^^^
4040
LL | + !SuperB {}
41-
| ^^^^^^^^^ negative trait bounds are not supported
41+
| ^^^^^^^^^ negative bounds are not supported
4242
|
43-
= help: remove the trait bounds
43+
= help: remove the bounds
4444

4545
error: aborting due to 5 previous errors
4646

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// In this regression test for #67146, we check that the
2+
// negative outlives bound `!'a` is rejected by the parser.
3+
// This regression was first introduced in PR #57364.
4+
5+
fn main() {}
6+
7+
fn f1<T: !'static>() {}
8+
//~^ ERROR negative bounds are not supported
9+
fn f2<'a, T: Ord + !'a>() {}
10+
//~^ ERROR negative bounds are not supported
11+
fn f3<'a, T: !'a + Ord>() {}
12+
//~^ ERROR negative bounds are not supported
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: negative bounds are not supported
2+
--> $DIR/issue-67146-negative-outlives-bound-syntactic-fail.rs:7:8
3+
|
4+
LL | fn f1<T: !'static>() {}
5+
| ^^^^^^^^^^ negative bounds are not supported
6+
|
7+
= help: remove the bound
8+
9+
error: negative bounds are not supported
10+
--> $DIR/issue-67146-negative-outlives-bound-syntactic-fail.rs:9:18
11+
|
12+
LL | fn f2<'a, T: Ord + !'a>() {}
13+
| ^^^^^ negative bounds are not supported
14+
|
15+
= help: remove the bound
16+
17+
error: negative bounds are not supported
18+
--> $DIR/issue-67146-negative-outlives-bound-syntactic-fail.rs:11:12
19+
|
20+
LL | fn f3<'a, T: !'a + Ord>() {}
21+
| ^^^^^ negative bounds are not supported
22+
|
23+
= help: remove the bound
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)