4 ms·
It would not warn; that's not how unsafe works in Rust. Or rather, if you are calling something unsafe, you have to write "unsafe { }". That's a sort of "warni
by steveklabnik 8y ago
It would not warn; that's not how unsafe works in Rust.
Or rather, if you are calling something unsafe, you have to write "unsafe { }". That's a sort of "warning" in a sense. But if that unsafe code is wrapped in a safe interface, it's that code's responsibility to be correct. Bugs do happen!
- amelius 8y agoOk, I guess it would be nice then if code which is unsafe but still thread-safe can be marked as such, and Rust should give a warning if used incorrectly. That way, you can parallelize any piece of code without having to think about it.
- steveklabnik 8y agoAlmost all code ends up going through unsafe, as calling into the OS to do any I/O ends up being unsafe. A warning like this would warn on almost all useful programs, making the warning useless. You already don't need to think about it. If you're not calling unsafe yourself, then it's not your job.
- amelius 8y agoWhat I meant is that sometimes, when you have to write unsafe code, it's easier to make it not thread-safe. Then later on, someone uses that piece of code and tries to parallelize it, and runs into problems. This can be prevented with language support.
- steveklabnik 8y agoRust does disallow you from doing some kinds of unsafe operations between threads, unless you explicitly opt in. That said, unsafe is not generally more convenient than safe and so people empirically don’t do this in the first place.
- amelius 8y agoLet me ask you then: would you use a Rust library in multithreaded code, without checking if that library is thread-safe? Wouldn't you like your compiler to tell you?
- steveklabnik 8y agoAll the time. The compiler does tell you if it’s thread safe. That’s the point.
- GolDDranks 8y agoThere is so called marker traits Send and Sync that are used to track whether something is tread-safe or not. https://doc.rust-lang.org/beta/nomicon/send-and-sync.html https://doc.rust-lang.org/beta/nomicon/send-and-sync.html Regardless of whether you use unsafe or not, thread safety is tracked with these and they allow the compiler to detect and prohibit data races.