]> git.lizzy.rs Git - rust.git/commitdiff
parse_generic_bounds: account for negative lifetime bounds
authorMazdak Farrokhzad <twingoow@gmail.com>
Sun, 8 Dec 2019 11:19:53 +0000 (12:19 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 21 Dec 2019 18:20:41 +0000 (19:20 +0100)
src/librustc_parse/parser/ty.rs
src/test/ui/issues/issue-58857.rs
src/test/ui/issues/issue-58857.stderr
src/test/ui/parser/issue-33418.fixed
src/test/ui/parser/issue-33418.rs
src/test/ui/parser/issue-33418.stderr
src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.rs [new file with mode: 0644]
src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.stderr [new file with mode: 0644]

index c97220458757440786c3e6042adcf4624c33205d..db9a0ada525b1e25bd6b1ee25d2185c809511358 100644 (file)
@@ -375,9 +375,9 @@ fn parse_generic_bounds_common(
             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 @@ fn parse_generic_bounds_common(
                 }
                 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 @@ fn can_begin_bound(&mut self) -> bool {
     /// ```
     /// 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 @@ fn parse_generic_ty_bound(
         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>> {
index 392e4ea0c2ecc2ca65d99dcc1dc02cb320e056b3..4350d7e5b403bcabc727c7c816bfae23f248eada 100644 (file)
@@ -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() {}
index ab9a0130c00b0d49fcbefc96a912b87c1bf4f541..9bc80cc270b0f47b1a608dd32b35082d4455e15c 100644 (file)
@@ -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
 
index 2aaa3b5b1ea502a521a12036a25315e68e8c8486..ed885ae14356633e0043e1fd0fa7ed1ff9c5e622 100644 (file)
@@ -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 {}
index 5533152092719f9a417a0e8ad2b547c496ed8372..9934284abfbbeb11d9dd3b8b4e395d8e9ed6bbb4 100644 (file)
@@ -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 {}
index 479e7bed1016a32081913f88b92cbbeb76576486..7f361dbe2718fc409dd92019ef90b51db9054148 100644 (file)
@@ -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
 
diff --git a/src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.rs b/src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.rs
new file mode 100644 (file)
index 0000000..5a109ba
--- /dev/null
@@ -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
diff --git a/src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.stderr b/src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.stderr
new file mode 100644 (file)
index 0000000..74437c1
--- /dev/null
@@ -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
+