suggest lazy-static for non-const statics

This commit is contained in:
cameron 2022-08-14 23:07:47 +01:00
parent 75b7e52e92
commit 34e0d9a0bb
9 changed files with 14 additions and 0 deletions

View File

@ -1,6 +1,7 @@
//! Concrete error types for all operations which may be invalid in a certain const context. //! Concrete error types for all operations which may be invalid in a certain const context.
use hir::def_id::LocalDefId; use hir::def_id::LocalDefId;
use hir::ConstContext;
use rustc_errors::{ use rustc_errors::{
error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
}; };
@ -331,6 +332,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
ccx.const_kind(), ccx.const_kind(),
)); ));
if let ConstContext::Static(_) = ccx.const_kind() {
err.note("consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell");
}
err err
} }
} }

View File

@ -14,6 +14,7 @@ LL | static settings_dir: String = format!("");
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
= note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0507]: cannot move out of static item `settings_dir` error[E0507]: cannot move out of static item `settings_dir`

View File

@ -22,6 +22,7 @@ LL | field2: SafeEnum::Variant4("str".to_string())
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
= note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
error[E0010]: allocations are not allowed in statics error[E0010]: allocations are not allowed in statics
--> $DIR/check-static-values-constraints.rs:94:5 --> $DIR/check-static-values-constraints.rs:94:5

View File

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

View File

@ -5,6 +5,7 @@ LL | static foo: Foo = bar();
| ^^^^^ | ^^^^^
| |
= note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,7 @@ LL | static foo: &Y::X = &*Y::foo(Y::x as *const Y::X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
error[E0133]: use of extern static is unsafe and requires unsafe function or block error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/issue-16538.rs:14:30 --> $DIR/issue-16538.rs:14:30

View File

@ -21,6 +21,7 @@ LL | static foo: &Y::X = &*Y::foo(Y::x as *const Y::X);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -16,6 +16,7 @@ note: impl defined here, but it is not `const`
LL | impl Deref for A { LL | impl Deref for A {
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
= note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
error: aborting due to previous error error: aborting due to previous error

View File

@ -5,6 +5,7 @@ LL | static a: [isize; 2] = [foo(); 2];
| ^^^^^ | ^^^^^
| |
= note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
error: aborting due to previous error error: aborting due to previous error