Skip to content
Closed
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
42 changes: 41 additions & 1 deletion src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,45 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}
}

fn note_possible_function_call(&self,
diag: &mut DiagnosticBuilder<'tcx>,
values: Option<ValuePairs<'tcx>>,
span: Span)
{
if let Some(infer::Types(ty::error::ExpectedFound {
expected, found
})) = values {
let (def_id, ret_ty) = match found.sty {
ty::TyFnDef(def, _, fty) => (Some(def), fty.sig.output()),
ty::TyFnPtr(fty) => (None, fty.sig.output()),
_ => return
};

let ok = self.probe(|_| {
match self.replace_late_bound_regions_with_fresh_var(
span,
super::LateBoundRegionConversionTime::HigherRankedType,
&ret_ty)
{
(ty::FnConverging(ret_ty), _) => {
self.can_equate(&ret_ty, &expected).is_ok()
}
(ty::FnDiverging, _) => true
}
});

if ok {
let message = match def_id {
Some(def_id) => {
format!("`{}` is a function", self.tcx.item_path_str(def_id))
}
_ => format!("found a function")
};
diag.help(&format!("{} - maybe try calling it?", message));
}
}
}

pub fn note_type_err(&self,
diag: &mut DiagnosticBuilder<'tcx>,
origin: TypeOrigin,
Expand All @@ -529,7 +568,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
{
let expected_found = match values {
None => None,
Some(values) => match self.values_str(&values) {
Some(ref values) => match self.values_str(values) {
Some((expected, found)) => Some((expected, found)),
None => {
// Derived error. Cancel the emitter.
Expand Down Expand Up @@ -563,6 +602,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {

self.note_error_origin(diag, &origin);
self.check_and_note_conflicting_crates(diag, terr, span);
self.note_possible_function_call(diag, values, span);
self.tcx.note_and_explain_type_err(diag, terr, span);
}

Expand Down
40 changes: 40 additions & 0 deletions src/test/compile-fail/issue-35241.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2016 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 Foo {
Bar(u32),
}

fn get_foo() -> Foo {
Foo::Bar
//~^ ERROR E0308
//~| HELP `Foo::Bar` is a function - maybe try calling it?
}

fn get_u32() -> u32 {
Foo::Bar
//~^ ERROR E0308
}

fn abort_2() {
abort
//~^ ERROR E0308
//~| HELP `abort` is a function - maybe try calling it?
}

fn abort() -> ! { panic!() }

fn call(f: fn() -> u32) -> u32 {
f
//~^ ERROR E0308
//~| HELP found a function - maybe try calling it?
}

fn main() {}