about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-12-08 12:19:53 +0100
committerMazdak Farrokhzad <twingoow@gmail.com>2019-12-21 19:20:41 +0100
commitb5f00beaa5a77ff8bccbe4330ea0b5047661cbf5 (patch)
tree25c6f4b6e62c7db4c1084bd784ba977c4fb687fd
parent4625ba4872e17a98ea11940fff0e50b3b880b815 (diff)
downloadrust-b5f00beaa5a77ff8bccbe4330ea0b5047661cbf5.tar.gz
rust-b5f00beaa5a77ff8bccbe4330ea0b5047661cbf5.zip
parse_generic_bounds: account for negative lifetime bounds
-rw-r--r--src/librustc_parse/parser/ty.rs35
-rw-r--r--src/test/ui/issues/issue-58857.rs2
-rw-r--r--src/test/ui/issues/issue-58857.stderr6
-rw-r--r--src/test/ui/parser/issue-33418.fixed10
-rw-r--r--src/test/ui/parser/issue-33418.rs10
-rw-r--r--src/test/ui/parser/issue-33418.stderr30
-rw-r--r--src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.rs12
-rw-r--r--src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.stderr26
8 files changed, 83 insertions, 48 deletions
diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs
index c9722045875..db9a0ada525 100644
--- a/src/librustc_parse/parser/ty.rs
+++ b/src/librustc_parse/parser/ty.rs
@@ -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>> {
diff --git a/src/test/ui/issues/issue-58857.rs b/src/test/ui/issues/issue-58857.rs
index 392e4ea0c2e..4350d7e5b40 100644
--- a/src/test/ui/issues/issue-58857.rs
+++ b/src/test/ui/issues/issue-58857.rs
@@ -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() {}
diff --git a/src/test/ui/issues/issue-58857.stderr b/src/test/ui/issues/issue-58857.stderr
index ab9a0130c00..9bc80cc270b 100644
--- a/src/test/ui/issues/issue-58857.stderr
+++ b/src/test/ui/issues/issue-58857.stderr
@@ -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
 
diff --git a/src/test/ui/parser/issue-33418.fixed b/src/test/ui/parser/issue-33418.fixed
index 2aaa3b5b1ea..ed885ae1435 100644
--- a/src/test/ui/parser/issue-33418.fixed
+++ b/src/test/ui/parser/issue-33418.fixed
@@ -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 {}
diff --git a/src/test/ui/parser/issue-33418.rs b/src/test/ui/parser/issue-33418.rs
index 55331520927..9934284abfb 100644
--- a/src/test/ui/parser/issue-33418.rs
+++ b/src/test/ui/parser/issue-33418.rs
@@ -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 {}
diff --git a/src/test/ui/parser/issue-33418.stderr b/src/test/ui/parser/issue-33418.stderr
index 479e7bed101..7f361dbe271 100644
--- a/src/test/ui/parser/issue-33418.stderr
+++ b/src/test/ui/parser/issue-33418.stderr
@@ -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
index 00000000000..5a109ba7c68
--- /dev/null
+++ b/src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.rs
@@ -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
index 00000000000..74437c1a008
--- /dev/null
+++ b/src/test/ui/parser/issue-67146-negative-outlives-bound-syntactic-fail.stderr
@@ -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
+