diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-08-20 07:08:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-20 07:08:59 +0200 |
| commit | c4b83ebe7c0e551c5e22abca259ef22d4cb0c509 (patch) | |
| tree | 23806a75e76323681aa26dae4c953f3a3b8dcbbb /compiler/rustc_const_eval/src/transform | |
| parent | 368f08a65f66cdd0b26298fb86a175188d9c34bb (diff) | |
| parent | 34e0d9a0bb409bb6c7fc45f1c222340b02989b8b (diff) | |
| download | rust-c4b83ebe7c0e551c5e22abca259ef22d4cb0c509.tar.gz rust-c4b83ebe7c0e551c5e22abca259ef22d4cb0c509.zip | |
Rollup merge of #100507 - cameron1024:suggest-lazy, r=compiler-errors
suggest `once_cell::Lazy` for non-const statics Addresses https://github.com/rust-lang/rust/issues/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?
Diffstat (limited to 'compiler/rustc_const_eval/src/transform')
| -rw-r--r-- | compiler/rustc_const_eval/src/transform/check_consts/ops.rs | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs index 33802261644..c9cfc1f3f46 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs @@ -1,6 +1,7 @@ //! Concrete error types for all operations which may be invalid in a certain const context. use hir::def_id::LocalDefId; +use hir::ConstContext; use rustc_errors::{ error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, }; @@ -331,6 +332,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { 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 } } |
