]> git.lizzy.rs Git - rust.git/commitdiff
Add E0618
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 14 Jun 2017 16:15:13 +0000 (18:15 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 15 Jun 2017 12:00:58 +0000 (14:00 +0200)
src/librustc_typeck/check/callee.rs
src/librustc_typeck/diagnostics.rs
src/test/compile-fail/E0618.rs [new file with mode: 0644]
src/test/compile-fail/empty-struct-unit-expr.rs

index d5ee66a2f0a07e0fa5ee8f0c80cb6769a6d9c9fc..385ed7eb0e3842c750999ba88f79f1e0614103ae 100644 (file)
@@ -15,7 +15,7 @@
 use hir::def::Def;
 use hir::def_id::{DefId, LOCAL_CRATE};
 use rustc::{infer, traits};
-use rustc::ty::{self, TyCtxt, LvaluePreference, Ty};
+use rustc::ty::{self, TyCtxt, TypeFoldable, LvaluePreference, Ty};
 use rustc::ty::subst::Subst;
 use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
 use syntax::abi;
@@ -209,17 +209,16 @@ fn confirm_builtin_call(&self,
                         }
                     }
                 }
-                let mut err = if let Some(path) = unit_variant {
-                    let mut err = self.type_error_struct(call_expr.span, |_| {
-                        format!("`{}` is being called, but it is not a function", path)
-                    }, callee_ty);
+                let mut err = type_error_struct!(self.tcx.sess, call_expr.span, callee_ty, E0618,
+                                                 "expected function, found `{}`",
+                                                 if let Some(ref path) = unit_variant {
+                                                     path.to_string()
+                                                 } else {
+                                                     callee_ty.to_string()
+                                                 });
+                if let Some(path) = unit_variant {
                     err.help(&format!("did you mean to write `{}`?", path));
-                    err
-                } else {
-                    self.type_error_struct(call_expr.span, |actual| {
-                        format!("expected function, found `{}`", actual)
-                    }, callee_ty)
-                };
+                }
 
                 if let hir::ExprCall(ref expr, _) = call_expr.node {
                     let def = if let hir::ExprPath(ref qpath) = expr.node {
index 76c664d7997a4fe69652ccf9d07798998f6351b7..c1240be6d2b4ed2b3514bdf8a1c0b9abba47f8d2 100644 (file)
@@ -4173,6 +4173,34 @@ struct Foo {
 https://doc.rust-lang.org/book/
 "##,
 
+E0618: r##"
+Attempted to call something which isn't a function nor a method.
+
+Erroneous code examples:
+
+```compile_fail,E0618
+enum X {
+    Entry,
+}
+
+X::Entry(); // error: expected function, found `X::Entry`
+
+// Or even simpler:
+let x = 0i32;
+x(); // error: expected function, found `i32`
+```
+
+Only functions and methods can be called using `()`. Example:
+
+```
+// We declare a function:
+fn i_am_a_function() {}
+
+// And we call it:
+i_am_a_function();
+```
+"##,
+
 }
 
 register_diagnostics! {
diff --git a/src/test/compile-fail/E0618.rs b/src/test/compile-fail/E0618.rs
new file mode 100644 (file)
index 0000000..1ba2e8e
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2017 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.
+
+enum X {
+    Entry,
+}
+
+fn main() {
+    X::Entry(); //~ ERROR expected function, found `X::Entry` [E0618]
+                //~| HELP did you mean to write `X::Entry`?
+    let x = 0i32;
+    x(); //~ ERROR expected function, found `i32` [E0618]
+}
index 273ce91a7c5b8a0d1b03442486c0a9f89a232a4e..9655007604de6e79d8c81644af01708e618335f1 100644 (file)
@@ -24,10 +24,10 @@ enum E {
 fn main() {
     let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
     let e4 = E::Empty4();
-    //~^ ERROR `E::Empty4` is being called, but it is not a function
+    //~^ ERROR expected function, found `E::Empty4` [E0618]
     //~| HELP did you mean to write `E::Empty4`?
     let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
     let xe4 = XE::XEmpty4();
-    //~^ ERROR `XE::XEmpty4` is being called, but it is not a function
+    //~^ ERROR expected function, found `XE::XEmpty4` [E0618]
     //~| HELP did you mean to write `XE::XEmpty4`?
 }