Skip to content

Commit f720e97

Browse files
Add E0618
1 parent 0189cec commit f720e97

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/librustc_typeck/check/callee.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::method::MethodCallee;
1515
use hir::def::Def;
1616
use hir::def_id::{DefId, LOCAL_CRATE};
1717
use rustc::{infer, traits};
18-
use rustc::ty::{self, TyCtxt, LvaluePreference, Ty};
18+
use rustc::ty::{self, TyCtxt, TypeFoldable, LvaluePreference, Ty};
1919
use rustc::ty::subst::Subst;
2020
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
2121
use syntax::abi;
@@ -209,17 +209,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
209209
}
210210
}
211211
}
212-
let mut err = if let Some(path) = unit_variant {
213-
let mut err = self.type_error_struct(call_expr.span, |_| {
214-
format!("`{}` is being called, but it is not a function", path)
215-
}, callee_ty);
212+
let mut err = type_error_struct!(self.tcx.sess, call_expr.span, callee_ty, E0618,
213+
"expected function, found `{}`",
214+
if let Some(ref path) = unit_variant {
215+
path.to_string()
216+
} else {
217+
callee_ty.to_string()
218+
});
219+
if let Some(path) = unit_variant {
216220
err.help(&format!("did you mean to write `{}`?", path));
217-
err
218-
} else {
219-
self.type_error_struct(call_expr.span, |actual| {
220-
format!("expected function, found `{}`", actual)
221-
}, callee_ty)
222-
};
221+
}
223222

224223
if let hir::ExprCall(ref expr, _) = call_expr.node {
225224
let def = if let hir::ExprPath(ref qpath) = expr.node {

src/librustc_typeck/diagnostics.rs

+28
Original file line numberDiff line numberDiff line change
@@ -4173,6 +4173,34 @@ as possible. For better explanations, see The Rust Book:
41734173
https://doc.rust-lang.org/book/
41744174
"##,
41754175

4176+
E0618: r##"
4177+
Attempted to call something which isn't a function nor a method.
4178+
4179+
Erroneous code examples:
4180+
4181+
```compile_fail,E0618
4182+
enum X {
4183+
Entry,
4184+
}
4185+
4186+
X::Entry(); // error: expected function, found `X::Entry`
4187+
4188+
// Or even simpler:
4189+
let x = 0i32;
4190+
x(); // error: expected function, found `i32`
4191+
```
4192+
4193+
Only functions and methods can be called using `()`. Example:
4194+
4195+
```
4196+
// We declare a function:
4197+
fn i_am_a_function() {}
4198+
4199+
// And we call it:
4200+
i_am_a_function();
4201+
```
4202+
"##,
4203+
41764204
}
41774205

41784206
register_diagnostics! {

src/test/compile-fail/E0618.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
enum X {
12+
Entry,
13+
}
14+
15+
fn main() {
16+
X::Entry(); //~ ERROR expected function, found `X::Entry` [E0618]
17+
//~| HELP did you mean to write `X::Entry`?
18+
let x = 0i32;
19+
x(); //~ ERROR expected function, found `i32` [E0618]
20+
}

src/test/compile-fail/empty-struct-unit-expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ enum E {
2424
fn main() {
2525
let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
2626
let e4 = E::Empty4();
27-
//~^ ERROR `E::Empty4` is being called, but it is not a function
27+
//~^ ERROR expected function, found `E::Empty4` [E0618]
2828
//~| HELP did you mean to write `E::Empty4`?
2929
let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
3030
let xe4 = XE::XEmpty4();
31-
//~^ ERROR `XE::XEmpty4` is being called, but it is not a function
31+
//~^ ERROR expected function, found `XE::XEmpty4` [E0618]
3232
//~| HELP did you mean to write `XE::XEmpty4`?
3333
}

0 commit comments

Comments
 (0)