-
Notifications
You must be signed in to change notification settings - Fork 351
rust: changes to allow 2024 edition #1150
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
base: master-dev
Are you sure you want to change the base?
Conversation
@bluenote10 @sletz this is the same pr as the one mentioned in #1005 it changes the scope of what is considered unsafe rust code and with this solves the warning that turned into an error in the 2024 edition of rust. this change is backwards compatible to older rust editions. |
I would need to take a closer, but on first glance I'm somewhat torn: As far as I can see the error in the 2024 edition is a genuine one, i.e., the current logic has undefined behavior. Just silencing it with more |
The code is valid if you can garanty that you don't initialize the class while you use it elsewhere. To get close to a sound solution one needs to deprecate the init method. |
The "if" in that sentence is exactly the reason why it is unsound. For instance, from the Rustonomicon:
Similarly, if we'd stick to the safety comments convention, we'd exactly lack a justification why that unsafe block is safe as far as I can see. As long as there is a condition like if you can garanty that you don't initialize the class while you use it elsewhere then you would have to pass the I'd assume that would be pretty inconvenient, but the warning/error is genuine, and just claiming safety where there isn't doesn't really solve the problem. I need to find some time to look into the other ideas mentioned in #1005. |
The current implementation (with and without my changes) has the following relevant qualities:
If we accept that we have a mutable static we should restrict the possibility to mutate it.
(i think issue #737 is also related because the use of sample rate becomes more likely if one has more complex table filling functions.) |
if we wrap those ideas into a type and provide functions then the meaning doesn't really change just the scope of the unsafe clauses.
|
By using import("stdfaust.lib");
envelope(gate) = gate : en.adsr(0.01, 0.01, 0.3, 0.1);
synth(gate, freq) = envelope(gate) * os.osc(freq);
process = synth; I've made the following manual edits to the generated code (full code example): Instead of using plain/global statics, I'm using a thread-local static: The The usage inside Notice that there are no The only assumption is that the DSP is consistently used from the same thread. Considering that this is pretty much the standard in audio programming, I'd be tempted to go for a fully safe thread local solution instead of trying to cook up a sound unsafe usage. |
The problem is that this is not a helpful solution for the multi thread case which is the only case that I am interested in. |
It could be extended to work there as well, by also storing a |
Are interested to implement this? |
I see that you use RefCell adds the overhead of a atomic check at every access that's not good. and not necessary. |
I can look into it, but it may take a while until I get to it.
A RefCell doesn't use an atomic bool, just a regular bool (because cells are not sync), and that's quite negligible according to my benchmarks. The Rust code runs exactly at C++ speed or even faster. In fact, I also want to look into
It is necessary. We must not violate Rust's core safety model. We will not be able to solve "static mut" without any kind of check and declare it "safe" as far as I can see. |
I suggested checks in this discussion. |
Yes, and they looked promising, but they essentially looked like an attempt at re-implementing what e.g. |
This pr does not make the code less correct than before. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pr does not make the code less correct than before. Are you open that this change (without the proposed locks) is merged until you implemented your solution?
If it simplifies things for you, we can go ahead and merge it.
Conceptually it's just a step in the wrong direction. You're right that the code was unsound all the time. The purpose of this warning/error is however to point out that it is unsound, and to solve it properly. Adding an unsound unsafe { ... }
block like we do now is basically what the warning/error was trying to prevent. In a sense, an invalid logic with a warning is marginally "better" than an invalid logic where the warning has been incorrectly suppressed.
@@ -273,7 +275,7 @@ void RustCodeContainer::produceClass() | |||
tab(n + 1, *fOut); | |||
*fOut << "#[cfg_attr(not(target_os = \"windows\"), link(name = \"m\"))]"; | |||
tab(n + 1, *fOut); | |||
*fOut << "extern \"C\" {"; | |||
*fOut << "unsafe extern \"C\" {"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for adding these unsafe
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edition 2024 changed rule to make it explicit.
@sletz what is your opinion? |
No description provided.