Skip to content

Commit c4b83eb

Browse files
authored
Rollup merge of #100507 - cameron1024:suggest-lazy, r=compiler-errors
suggest `once_cell::Lazy` for non-const statics Addresses #100410 Some questions: - removing the `if` seems to include too many cases (e.g. calls to non-const functions inside a `const fn`), but this code excludes the following case: ```rust const FOO: Foo = non_const_fn(); ``` Should we suggest `once_cell` in this case as well? - The original issue mentions suggesting `AtomicI32` instead of `Mutex<i32>`, should this PR address that as well?
2 parents 368f08a + 34e0d9a commit c4b83eb

File tree

9 files changed

+14
-0
lines changed

9 files changed

+14
-0
lines changed

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
33
use hir::def_id::LocalDefId;
4+
use hir::ConstContext;
45
use rustc_errors::{
56
error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
67
};
@@ -331,6 +332,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
331332
ccx.const_kind(),
332333
));
333334

335+
if let ConstContext::Static(_) = ccx.const_kind() {
336+
err.note("consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell");
337+
}
338+
334339
err
335340
}
336341
}

src/test/ui/borrowck/issue-64453.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | static settings_dir: String = format!("");
1414
| ^^^^^^^^^^^
1515
|
1616
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
17+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
1718
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
1819

1920
error[E0507]: cannot move out of static item `settings_dir`

src/test/ui/check-static-values-constraints.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LL | field2: SafeEnum::Variant4("str".to_string())
2222
| ^^^^^^^^^^^
2323
|
2424
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
25+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
2526

2627
error[E0010]: allocations are not allowed in statics
2728
--> $DIR/check-static-values-constraints.rs:94:5

src/test/ui/consts/issue-32829-2.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LL | invalid();
1313
| ^^^^^^^^^
1414
|
1515
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
16+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
1617

1718
error[E0015]: cannot call non-const fn `invalid` in statics
1819
--> $DIR/issue-32829-2.rs:54:9
@@ -21,6 +22,7 @@ LL | invalid();
2122
| ^^^^^^^^^
2223
|
2324
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
25+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
2426

2527
error: aborting due to 3 previous errors
2628

src/test/ui/consts/mir_check_nonconst.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | static foo: Foo = bar();
55
| ^^^^^
66
|
77
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
8+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
89

910
error: aborting due to previous error
1011

src/test/ui/issues/issue-16538.mir.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | static foo: &Y::X = &*Y::foo(Y::x as *const Y::X);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
8+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
89

910
error[E0133]: use of extern static is unsafe and requires unsafe function or block
1011
--> $DIR/issue-16538.rs:14:30

src/test/ui/issues/issue-16538.thir.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LL | static foo: &Y::X = &*Y::foo(Y::x as *const Y::X);
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2222
|
2323
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
24+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
2425

2526
error: aborting due to 3 previous errors
2627

src/test/ui/issues/issue-25901.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ note: impl defined here, but it is not `const`
1616
LL | impl Deref for A {
1717
| ^^^^^^^^^^^^^^^^
1818
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
19+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
1920

2021
error: aborting due to previous error
2122

src/test/ui/static/static-vec-repeat-not-constant.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | static a: [isize; 2] = [foo(); 2];
55
| ^^^^^
66
|
77
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
8+
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
89

910
error: aborting due to previous error
1011

0 commit comments

Comments
 (0)