Skip to content

Commit ca22f89

Browse files
authored
Auto merge of #35260 - hilasha2:found-function, r=nikomatsakis
Changed error message for enum variants Fixes #35241 r? @jonathandturner
2 parents 545a3a9 + aaf5d5b commit ca22f89

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/librustc/infer/error_reporting.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,45 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
521521
}
522522
}
523523

524+
fn note_possible_function_call(&self,
525+
diag: &mut DiagnosticBuilder<'tcx>,
526+
values: Option<ValuePairs<'tcx>>,
527+
span: Span)
528+
{
529+
if let Some(infer::Types(ty::error::ExpectedFound {
530+
expected, found
531+
})) = values {
532+
let (def_id, ret_ty) = match found.sty {
533+
ty::TyFnDef(def, _, fty) => (Some(def), fty.sig.output()),
534+
ty::TyFnPtr(fty) => (None, fty.sig.output()),
535+
_ => return
536+
};
537+
538+
let ok = self.probe(|_| {
539+
match self.replace_late_bound_regions_with_fresh_var(
540+
span,
541+
super::LateBoundRegionConversionTime::HigherRankedType,
542+
&ret_ty)
543+
{
544+
(ty::FnConverging(ret_ty), _) => {
545+
self.can_equate(&ret_ty, &expected).is_ok()
546+
}
547+
(ty::FnDiverging, _) => true
548+
}
549+
});
550+
551+
if ok {
552+
let message = match def_id {
553+
Some(def_id) => {
554+
format!("`{}` is a function", self.tcx.item_path_str(def_id))
555+
}
556+
_ => format!("found a function")
557+
};
558+
diag.help(&format!("{} - maybe try calling it?", message));
559+
}
560+
}
561+
}
562+
524563
pub fn note_type_err(&self,
525564
diag: &mut DiagnosticBuilder<'tcx>,
526565
origin: TypeOrigin,
@@ -529,7 +568,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
529568
{
530569
let expected_found = match values {
531570
None => None,
532-
Some(values) => match self.values_str(&values) {
571+
Some(ref values) => match self.values_str(values) {
533572
Some((expected, found)) => Some((expected, found)),
534573
None => {
535574
// Derived error. Cancel the emitter.
@@ -563,6 +602,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
563602

564603
self.note_error_origin(diag, &origin);
565604
self.check_and_note_conflicting_crates(diag, terr, span);
605+
self.note_possible_function_call(diag, values, span);
566606
self.tcx.note_and_explain_type_err(diag, terr, span);
567607
}
568608

src/test/compile-fail/issue-35241.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2016 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 Foo {
12+
Bar(u32),
13+
}
14+
15+
fn get_foo() -> Foo {
16+
Foo::Bar
17+
//~^ ERROR E0308
18+
//~| HELP `Foo::Bar` is a function - maybe try calling it?
19+
}
20+
21+
fn get_u32() -> u32 {
22+
Foo::Bar
23+
//~^ ERROR E0308
24+
}
25+
26+
fn abort_2() {
27+
abort
28+
//~^ ERROR E0308
29+
//~| HELP `abort` is a function - maybe try calling it?
30+
}
31+
32+
fn abort() -> ! { panic!() }
33+
34+
fn call(f: fn() -> u32) -> u32 {
35+
f
36+
//~^ ERROR E0308
37+
//~| HELP found a function - maybe try calling it?
38+
}
39+
40+
fn main() {}

0 commit comments

Comments
 (0)