-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest lazy initialization when calling a non-const function in a static initializer #100410
Comments
also, in this particular case it could have suggested using |
Mentoring instructions: add that note immediately after rust/compiler/rustc_const_eval/src/transform/check_consts/ops.rs Lines 328 to 332 in 8b2637f
|
@rustbot claim |
Can't we just punt this until after #74465 lands? |
@mqudsi I don't understand why it has to be blocked on that? we can certainly update the message to suggest |
I guess it's just being conservative when evaluating the costs vs benefits?
I'm not a zero-dependency zealot (but I do appreciate the distinction between I could be mistaken, but I've been following it and I don't think any serious issues have been raised with the core/std port of (I use |
tbh people who feel like that probably already feel that way regardless of what the compiler says We already suggest async_trait for async functions in traits, I don't think this is that much of a stretch. If std::once is just around the corner like you say, then we won't suggest an external crate for long anyway :) |
Fair enough! |
…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?
…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?
#100507 fixed this. |
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=67155cb72a24e0a080d9dee88483fbec
The current output is:
I have a friend learning rust who concluded, reasonably, that global mutexes are not allowed and you have to declare them in
main
and pass them down to functions with an argument. It would be great if the compiler could suggest the common alternative approach instead, which is lazy initialization:Ideally the output should look like:
I know the compiler generally has a policy of not favoring individual crates, but once_cell is literally becoming part of the standard library so I think this is reasonable and doesn't show favoritism.
(I'm aware Mutex::new will be a
const fn
in 1.63, but this same diagnostic would be helpful in any other case where the initializer isn't const.)The text was updated successfully, but these errors were encountered: