Skip to content

Commit cb5f979

Browse files
committed
Diagnostic: resolve bare fn in expected closure
Closes #15273 (I did not find how to get the identifier in the message :/) Also creates the span_help! macro associated with #18126
1 parent 3fa2b56 commit cb5f979

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/librustc/middle/typeck/check/writeback.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -282,7 +282,9 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
282282
}
283283
_ => {
284284
span_err!(self.tcx().sess, reason.span(self.tcx()), E0100,
285-
"cannot coerce non-statically resolved bare fn");
285+
"cannot coerce non-statically resolved bare fn to closure");
286+
span_help!(self.tcx().sess, reason.span(self.tcx()),
287+
"consider embedding the function in a closure");
286288
}
287289
}
288290

src/libsyntax/diagnostics/macros.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ macro_rules! span_note(
3939
})
4040
)
4141

42+
#[macro_export]
43+
macro_rules! span_help(
44+
($session:expr, $span:expr, $($message:tt)*) => ({
45+
($session).span_help($span, format!($($message)*).as_slice())
46+
})
47+
)
48+
4249
#[macro_export]
4350
macro_rules! register_diagnostics(
4451
($($code:tt),*) => (

src/test/compile-fail/coerce-bare-fn-to-closure-and-proc.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ fn foo() {}
1313
fn main() {
1414
let f = foo;
1515
let f_closure: || = f;
16-
//~^ ERROR: cannot coerce non-statically resolved bare fn
16+
//~^ ERROR: cannot coerce non-statically resolved bare fn to closure
17+
//~^ HELP: consider embedding the function in a closure
1718
let f_proc: proc() = f;
18-
//~^ ERROR: cannot coerce non-statically resolved bare fn
19+
//~^ ERROR: cannot coerce non-statically resolved bare fn to closure
20+
//~^ HELP: consider embedding the function in a closure
1921
}

0 commit comments

Comments
 (0)