Skip to content

Commit 898c76c

Browse files
committed
Make error messages great again (and fix clippy and add test)
1 parent 191d3b7 commit 898c76c

File tree

10 files changed

+69
-43
lines changed

10 files changed

+69
-43
lines changed

compiler/rustc_typeck/src/check/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fn typeck_with_fallback<'tcx>(
366366

367367
let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
368368
let param_env = tcx.param_env(def_id);
369-
let fcx = if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
369+
let mut fcx = if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
370370
let fn_sig = if crate::collect::get_infer_ret_ty(&decl.output).is_some() {
371371
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
372372
<dyn AstConv<'_>>::ty_of_fn(&fcx, id, header.unsafety, header.abi, decl, None, None)
@@ -459,7 +459,11 @@ fn typeck_with_fallback<'tcx>(
459459

460460
// Closure and generator analysis may run after fallback
461461
// because they don't constrain other type variables.
462+
// Closure analysis only runs on closures. Therefore they only need to fulfill non-const predicates (as of now)
463+
let prev_constness = fcx.param_env.constness();
464+
fcx.param_env = fcx.param_env.without_const();
462465
fcx.closure_analyze(body);
466+
fcx.param_env = fcx.param_env.with_constness(prev_constness);
463467
assert!(fcx.deferred_call_resolutions.borrow().is_empty());
464468
// Before the generator analysis, temporary scopes shall be marked to provide more
465469
// precise information on types to be captured.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct X<const N: usize = {
22
(||1usize)()
3-
//~^ ERROR the trait bound
3+
//~^ ERROR cannot call non-const closure
44
}>;
55

66
fn main() {}
+5-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
error[E0277]: the trait bound `[closure@$DIR/issue-93647.rs:2:6: 2:8]: Fn<()>` is not satisfied
2-
--> $DIR/issue-93647.rs:2:5
3-
|
4-
LL | (||1usize)()
5-
| ^^^^^^^^^^^^ expected an `Fn<()>` closure, found `[closure@$DIR/issue-93647.rs:2:6: 2:8]`
6-
|
7-
= help: the trait `~const Fn<()>` is not implemented for closure `[closure@$DIR/issue-93647.rs:2:6: 2:8]`
8-
note: the trait `Fn<()>` is implemented for `[closure@$DIR/issue-93647.rs:2:6: 2:8]`, but that implementation is not `const`
1+
error[E0015]: cannot call non-const closure in constants
92
--> $DIR/issue-93647.rs:2:5
103
|
114
LL | (||1usize)()
125
| ^^^^^^^^^^^^
13-
= note: wrap the `[closure@$DIR/issue-93647.rs:2:6: 2:8]` in a closure with no arguments: `|| { /* code */ }`
6+
|
7+
= note: closures need an RFC before allowed to be called in constants
8+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
149

1510
error: aborting due to previous error
1611

17-
For more information about this error, try `rustc --explain E0277`.
12+
For more information about this error, try `rustc --explain E0015`.

src/test/ui/consts/issue-28113.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const X: u8 =
44
|| -> u8 { 5 }()
5-
//~^ ERROR the trait bound
5+
//~^ ERROR cannot call non-const closure
66
;
77

88
fn main() {}

src/test/ui/consts/issue-28113.stderr

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
error[E0277]: the trait bound `[closure@$DIR/issue-28113.rs:4:5: 4:13]: Fn<()>` is not satisfied
2-
--> $DIR/issue-28113.rs:4:5
3-
|
4-
LL | || -> u8 { 5 }()
5-
| ^^^^^^^^^^^^^^^^ expected an `Fn<()>` closure, found `[closure@$DIR/issue-28113.rs:4:5: 4:13]`
6-
|
7-
= help: the trait `~const Fn<()>` is not implemented for closure `[closure@$DIR/issue-28113.rs:4:5: 4:13]`
8-
note: the trait `Fn<()>` is implemented for `[closure@$DIR/issue-28113.rs:4:5: 4:13]`, but that implementation is not `const`
1+
error[E0015]: cannot call non-const closure in constants
92
--> $DIR/issue-28113.rs:4:5
103
|
114
LL | || -> u8 { 5 }()
125
| ^^^^^^^^^^^^^^^^
13-
= note: wrap the `[closure@$DIR/issue-28113.rs:4:5: 4:13]` in a closure with no arguments: `|| { /* code */ }`
6+
|
7+
= note: closures need an RFC before allowed to be called in constants
8+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
149

1510
error: aborting due to previous error
1611

17-
For more information about this error, try `rustc --explain E0277`.
12+
For more information about this error, try `rustc --explain E0015`.

src/test/ui/consts/issue-56164.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const fn foo() { (||{})() }
2-
//~^ ERROR the trait bound
2+
//~^ ERROR cannot call non-const closure
3+
//~| ERROR erroneous constant
4+
//~| WARN this was previously accepted
35

46
const fn bad(input: fn()) {
57
input()
8+
//~^ ERROR function pointer calls are not allowed
69
}
710

811
fn main() {

src/test/ui/consts/issue-56164.stderr

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
error[E0277]: the trait bound `[closure@$DIR/issue-56164.rs:1:19: 1:21]: Fn<()>` is not satisfied
1+
error[E0015]: cannot call non-const closure in constant functions
22
--> $DIR/issue-56164.rs:1:18
33
|
44
LL | const fn foo() { (||{})() }
5-
| ^^^^^^^^ expected an `Fn<()>` closure, found `[closure@$DIR/issue-56164.rs:1:19: 1:21]`
5+
| ^^^^^^^^
6+
|
7+
= note: closures need an RFC before allowed to be called in constant functions
8+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
9+
10+
error: function pointer calls are not allowed in constant functions
11+
--> $DIR/issue-56164.rs:7:5
612
|
7-
= help: the trait `~const Fn<()>` is not implemented for closure `[closure@$DIR/issue-56164.rs:1:19: 1:21]`
8-
note: the trait `Fn<()>` is implemented for `[closure@$DIR/issue-56164.rs:1:19: 1:21]`, but that implementation is not `const`
13+
LL | input()
14+
| ^^^^^^^
15+
16+
error: erroneous constant used
917
--> $DIR/issue-56164.rs:1:18
1018
|
1119
LL | const fn foo() { (||{})() }
12-
| ^^^^^^^^
13-
= note: wrap the `[closure@$DIR/issue-56164.rs:1:19: 1:21]` in a closure with no arguments: `|| { /* code */ }`
20+
| ^^^^^^ referenced constant has errors
21+
|
22+
= note: `#[deny(const_err)]` on by default
23+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1425

15-
error: aborting due to previous error
26+
error: aborting due to 3 previous errors
27+
28+
For more information about this error, try `rustc --explain E0015`.
29+
Future incompatibility report: Future breakage diagnostic:
30+
error: erroneous constant used
31+
--> $DIR/issue-56164.rs:1:18
32+
|
33+
LL | const fn foo() { (||{})() }
34+
| ^^^^^^ referenced constant has errors
35+
|
36+
= note: `#[deny(const_err)]` on by default
37+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
38+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1639

17-
For more information about this error, try `rustc --explain E0277`.

src/test/ui/consts/issue-68542-closure-in-array-len.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// in the length part of an array.
44

55
struct Bug {
6-
a: [(); (|| { 0 })()] //~ ERROR the trait bound
6+
a: [(); (|| { 0 })()] //~ ERROR cannot call non-const closure
77
}
88

99
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
error[E0277]: the trait bound `[closure@$DIR/issue-68542-closure-in-array-len.rs:6:14: 6:16]: Fn<()>` is not satisfied
2-
--> $DIR/issue-68542-closure-in-array-len.rs:6:13
3-
|
4-
LL | a: [(); (|| { 0 })()]
5-
| ^^^^^^^^^^^^ expected an `Fn<()>` closure, found `[closure@$DIR/issue-68542-closure-in-array-len.rs:6:14: 6:16]`
6-
|
7-
= help: the trait `~const Fn<()>` is not implemented for closure `[closure@$DIR/issue-68542-closure-in-array-len.rs:6:14: 6:16]`
8-
note: the trait `Fn<()>` is implemented for `[closure@$DIR/issue-68542-closure-in-array-len.rs:6:14: 6:16]`, but that implementation is not `const`
1+
error[E0015]: cannot call non-const closure in constants
92
--> $DIR/issue-68542-closure-in-array-len.rs:6:13
103
|
114
LL | a: [(); (|| { 0 })()]
125
| ^^^^^^^^^^^^
13-
= note: wrap the `[closure@$DIR/issue-68542-closure-in-array-len.rs:6:14: 6:16]` in a closure with no arguments: `|| { /* code */ }`
6+
|
7+
= note: closures need an RFC before allowed to be called in constants
8+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
149

1510
error: aborting due to previous error
1611

17-
For more information about this error, try `rustc --explain E0277`.
12+
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
3+
#![feature(const_trait_impl, once_cell)]
4+
5+
use std::sync::LazyLock;
6+
7+
static EXTERN_FLAGS: LazyLock<String> = LazyLock::new(|| {
8+
let x = || String::new();
9+
x()
10+
});
11+
12+
fn main() {}

0 commit comments

Comments
 (0)