Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reword error when data-less enum variant called as function #36520

Merged
merged 1 commit into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/librustc_typeck/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{DeferredCallResolution, Expectation, FnCtxt, TupleArgumentsFlag};
use CrateCtxt;
use hir::def::Def;
use hir::def_id::{DefId, LOCAL_CRATE};
use hir::print;
use rustc::{infer, traits};
use rustc::ty::{self, LvaluePreference, Ty};
use syntax::parse::token;
Expand Down Expand Up @@ -194,15 +195,28 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let error_fn_sig;

let fn_sig = match callee_ty.sty {
ty::TyFnDef(.., &ty::BareFnTy { ref sig, .. }) |
ty::TyFnPtr(&ty::BareFnTy { ref sig, .. }) => sig,
_ => {
let mut err = self.type_error_struct(call_expr.span,
|actual| {
format!("expected function, found `{}`",
actual)
},
callee_ty);
ty::TyFnDef(.., &ty::BareFnTy {ref sig, ..}) |
ty::TyFnPtr(&ty::BareFnTy {ref sig, ..}) => sig,
ref t => {
let mut unit_variant = None;
if let &ty::TyAdt(adt_def, ..) = t {
if adt_def.is_enum() {
if let hir::ExprCall(ref expr, _) = call_expr.node {
unit_variant = Some(print::expr_to_string(expr))
}
}
}
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);
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 tcx = self.tcx;
Expand Down
8 changes: 6 additions & 2 deletions src/test/compile-fail/empty-struct-unit-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ enum E {

fn main() {
let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
let e4 = E::Empty4(); //~ ERROR expected function, found `E`
let e4 = E::Empty4();
//~^ ERROR `E::Empty4` is being called, but it is not a function
//~| HELP did you mean to write `E::Empty4`?
let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
let xe4 = XE::XEmpty4(); //~ ERROR expected function, found `empty_struct::XE`
let xe4 = XE::XEmpty4();
//~^ ERROR `XE::XEmpty4` is being called, but it is not a function
//~| HELP did you mean to write `XE::XEmpty4`?
}