Skip to content

Commit

Permalink
Rollup merge of rust-lang#100507 - cameron1024:suggest-lazy, r=compil…
Browse files Browse the repository at this point in the history
…er-errors

suggest `once_cell::Lazy` for non-const statics

Addresses rust-lang#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?
  • Loading branch information
ChrisDenton authored Aug 20, 2022
2 parents f577300 + 34e0d9a commit e70de69
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/borrowck/issue-64453.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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)

error[E0507]: cannot move out of static item `settings_dir`
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/check-static-values-constraints.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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
--> $DIR/check-static-values-constraints.rs:94:5
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/consts/issue-32829-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LL | invalid();
| ^^^^^^^^^
|
= 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
--> $DIR/issue-32829-2.rs:54:9
Expand All @@ -21,6 +22,7 @@ LL | invalid();
| ^^^^^^^^^
|
= 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/consts/mir_check_nonconst.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | static foo: Foo = bar();
| ^^^^^
|
= 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-16538.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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
--> $DIR/issue-16538.rs:14:30
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-16538.thir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-25901.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ note: impl defined here, but it is not `const`
LL | impl Deref for A {
| ^^^^^^^^^^^^^^^^
= 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

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/static/static-vec-repeat-not-constant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell

error: aborting due to previous error

Expand Down

0 comments on commit e70de69

Please sign in to comment.