about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-08-13 02:28:13 +0000
committerbors <bors@rust-lang.org>2018-08-13 02:28:13 +0000
commitab93561b5fa54954159480ddc10bbb69f015e539 (patch)
tree05a25f34dab37f6f5e8096be2ea3fa7262463068
parent5a0d2961ce88f7db90b13771d1e8fc3b50ded7b1 (diff)
parent5c814e2e4e0649972ec6a18c7dbf57259edf2210 (diff)
Auto merge of #53051 - varkor:trait-method-pattern-arguments-error, r=petrochenkov
Emit error for pattern arguments in trait methods

The error and check for this already existed, but the parser didn't try to parse trait method arguments as patterns, so the error was never emitted. This surfaces the error, so we get better errors than simple parse errors.

This improves the error message described in https://github.com/rust-lang/rust/issues/53046.

r? @petrochenkov
-rw-r--r--src/librustc_passes/diagnostics.rs22
-rw-r--r--src/libsyntax/lib.rs1
-rw-r--r--src/libsyntax/parse/parser.rs68
-rw-r--r--src/test/ui/E0642.rs24
-rw-r--r--src/test/ui/E0642.stderr23
5 files changed, 122 insertions, 16 deletions
diff --git a/src/librustc_passes/diagnostics.rs b/src/librustc_passes/diagnostics.rs
index f1ec3371c3b..f1d0a4fee34 100644
--- a/src/librustc_passes/diagnostics.rs
+++ b/src/librustc_passes/diagnostics.rs
@@ -261,6 +261,27 @@ let result = loop { // ok!
 ```
 "##,
 
+E0642: r##"
+Trait methods currently cannot take patterns as arguments.
+
+Example of erroneous code:
+
+```compile_fail,E0642
+trait Foo {
+    fn foo((x, y): (i32, i32)); // error: patterns aren't allowed
+                                //        in trait methods
+}
+```
+
+You can instead use a single name for the argument:
+
+```
+trait Foo {
+    fn foo(x_and_y: (i32, i32)); // ok!
+}
+```
+"##,
+
 E0695: r##"
 A `break` statement without a label appeared inside a labeled block.
 
@@ -306,7 +327,6 @@ register_diagnostics! {
     E0561, // patterns aren't allowed in function pointer types
     E0567, // auto traits can not have generic parameters
     E0568, // auto traits can not have super traits
-    E0642, // patterns aren't allowed in methods without bodies
     E0666, // nested `impl Trait` is illegal
     E0667, // `impl Trait` in projections
     E0696, // `continue` pointing to a labeled block
diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs
index 0948ebea15d..12ce478fe1d 100644
--- a/src/libsyntax/lib.rs
+++ b/src/libsyntax/lib.rs
@@ -27,6 +27,7 @@
 #![feature(slice_sort_by_cached_key)]
 #![feature(str_escape)]
 #![feature(unicode_internals)]
+#![feature(catch_expr)]
 
 #![recursion_limit="256"]
 
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 9011b6e48b9..746e03d771a 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -44,7 +44,7 @@ use ast::{RangeEnd, RangeSyntax};
 use {ast, attr};
 use codemap::{self, CodeMap, Spanned, respan};
 use syntax_pos::{self, Span, MultiSpan, BytePos, FileName, edition::Edition};
-use errors::{self, Applicability, DiagnosticBuilder};
+use errors::{self, Applicability, DiagnosticBuilder, DiagnosticId};
 use parse::{self, SeqSep, classify, token};
 use parse::lexer::TokenAndSpan;
 use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
@@ -1371,7 +1371,7 @@ impl<'a> Parser<'a> {
             let ident = self.parse_ident()?;
             let mut generics = self.parse_generics()?;
 
-            let d = self.parse_fn_decl_with_self(|p: &mut Parser<'a>|{
+            let d = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| {
                 // This is somewhat dubious; We don't want to allow
                 // argument names to be left off if there is a
                 // definition...
@@ -1753,21 +1753,59 @@ impl<'a> Parser<'a> {
             (pat, self.parse_ty()?)
         } else {
             debug!("parse_arg_general ident_to_pat");
-            let ident = Ident::new(keywords::Invalid.name(), self.prev_span);
-            let ty = self.parse_ty()?;
-            let pat = P(Pat {
-                id: ast::DUMMY_NODE_ID,
-                node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None),
-                span: ty.span,
-            });
-            (pat, ty)
+
+            let parser_snapshot_before_pat = self.clone();
+
+            // We're going to try parsing the argument as a pattern (even though it's not
+            // allowed). This way we can provide better errors to the user.
+            let pat_arg: PResult<'a, _> = do catch {
+                let pat = self.parse_pat()?;
+                self.expect(&token::Colon)?;
+                (pat, self.parse_ty()?)
+            };
+
+            match pat_arg {
+                Ok((pat, ty)) => {
+                    let mut err = self.diagnostic().struct_span_err_with_code(
+                        pat.span,
+                        "patterns aren't allowed in methods without bodies",
+                        DiagnosticId::Error("E0642".into()),
+                    );
+                    err.span_suggestion_short_with_applicability(
+                        pat.span,
+                        "give this argument a name or use an underscore to ignore it",
+                        "_".to_owned(),
+                        Applicability::MachineApplicable,
+                    );
+                    err.emit();
+                    // Pretend the pattern is `_`, to avoid duplicate errors from AST validation.
+                    let pat = P(Pat {
+                        node: PatKind::Wild,
+                        span: pat.span,
+                        id: ast::DUMMY_NODE_ID
+                    });
+                    (pat, ty)
+                }
+                Err(mut err) => {
+                    err.cancel();
+                    // Recover from attempting to parse the argument as a pattern. This means
+                    // the type is alone, with no name, e.g. `fn foo(u32)`.
+                    mem::replace(self, parser_snapshot_before_pat);
+                    debug!("parse_arg_general ident_to_pat");
+                    let ident = Ident::new(keywords::Invalid.name(), self.prev_span);
+                    let ty = self.parse_ty()?;
+                    let pat = P(Pat {
+                        id: ast::DUMMY_NODE_ID,
+                        node: PatKind::Ident(
+                            BindingMode::ByValue(Mutability::Immutable), ident, None),
+                        span: ty.span,
+                    });
+                    (pat, ty)
+                }
+            }
         };
 
-        Ok(Arg {
-            ty,
-            pat,
-            id: ast::DUMMY_NODE_ID,
-        })
+        Ok(Arg { ty, pat, id: ast::DUMMY_NODE_ID })
     }
 
     /// Parse a single function argument
diff --git a/src/test/ui/E0642.rs b/src/test/ui/E0642.rs
new file mode 100644
index 00000000000..58ccfc56ab7
--- /dev/null
+++ b/src/test/ui/E0642.rs
@@ -0,0 +1,24 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[derive(Clone, Copy)]
+struct S;
+
+trait T {
+    fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies
+
+    fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies
+
+    fn f(&ident: &S) {} // ok
+    fn g(&&ident: &&S) {} // ok
+    fn h(mut ident: S) {} // ok
+}
+
+fn main() {}
diff --git a/src/test/ui/E0642.stderr b/src/test/ui/E0642.stderr
new file mode 100644
index 00000000000..34c163e2109
--- /dev/null
+++ b/src/test/ui/E0642.stderr
@@ -0,0 +1,23 @@
+error[E0642]: patterns aren't allowed in methods without bodies
+  --> $DIR/E0642.rs:15:12
+   |
+LL |     fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies
+   |            ^^^^^^
+help: give this argument a name or use an underscore to ignore it
+   |
+LL |     fn foo(_: (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies
+   |            ^
+
+error[E0642]: patterns aren't allowed in methods without bodies
+  --> $DIR/E0642.rs:17:12
+   |
+LL |     fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies
+   |            ^^^^^^
+help: give this argument a name or use an underscore to ignore it
+   |
+LL |     fn bar(_: (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies
+   |            ^
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0642`.