-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
#[linkage = "weak"] const fn
accepted without warning
#134451
Comments
Const items are always evaluated at compile time, which happens before linking, so this is expected. Perhaps const functions should not be allowed weak linkage, because it allows their compile-time and runtime behavior to differ. |
I think it's valuable to have "weak constants" in Rust, just disallowing weak linkage for const fns is not we want. For example, in no_std env, it's very common to define a global channel with a fixed size: // This is a simplified code
const CH_SIZE: usize = 4;
static CHANNEL: Channel<CH_SIZE> = Channel::new(); And, as a library author, we may want the |
#[linkage = "weak"]
doesn't work for const fn
|
Indeed, we may wish to investigate making this a compiler error. But first, while an expert on the linking side of things has spoken, let us consult the experts on the CTFE side of things. cc @rust-lang/wg-const-eval do any of you wish to entertain the possibility of a const fn having its symbol name overwritten at runtime, and users like @HaoboGu consequently seriously misunderstanding the model? |
@HaoboGu The idea of "generic singletons" is an interesting idea, but it's absolutely not the way things work, and if it did work, it would have to be designed in such a way as to actually be evaluated without taking linkage into account. |
No I don't think Even on regular |
Constants are called that because they must be constant. It would be unsound to let anyone change them. The type system relies hard to the fact that the same constant has the same value everywhere in the crate graph. If you want to write your code in a way that is generic over some constant, you need to make it explicitly generic. We have mechanisms for that already (associated consts in a trait). I know that making an entire module generic can be annoying, but the solution for that definitely does not involve fundamentally changing the nature of consts in Rust; the solution is to explore module-level generics. |
Getting slightly further afield, but |
Thanks for everyone's explanation! Now I know doing it when linking is not proper :)
I agree! Currently it's very difficult to achieve this, especially in embedded world. There are many ways to "bypass" it, for example, embassy generates lots of feature gates, let users choose the
It's a little bit weird if we convert a config struct to a trait, and tell users to fill an associated const if they want to customize the config. But yeah, now I see this should work. Thanks for the pointer! |
Speaking of singletons, this reminds me of rust-lang/rfcs#3632 / rust-lang/rfcs#3635. So maybe there is a space for "externally defineable consts" in a similar vein? |
yes, seems very reasonable:) |
Plausible. cc @m-ou-se if you have any thoughts (or not if not, it's just a data point of possible interest). |
Yeah that's definitely on our list. We're now calling our project "externally implementable items", and consts are also on our list of items, although not our first priority. We'll see how far we can get. :) |
Cool! This issue is now about diagnostics! We should at least warn, probably compile error on this. |
#[linkage = "weak"]
doesn't work for const fn#[linkage = "weak"] const fn
accepted without warning
I'm trying to define a "weak" const in a library, which allows users to redefine their const. On stable Rust, there's no way to do it. On nightly,
linkage
feature seems the solution, but I found that it doesn't work as well. The following is the repro code:I uploaded a repo that could repro it: https://github.com/HaoboGu/linkage_weak_bug
The text was updated successfully, but these errors were encountered: