46
35
u/Comun4 Oct 14 '26
Wouldnt this break aliasing rules?
112
u/fortyeightzero Oct 14 '26
nah the comment says it's safe
46
13
u/MichiRecRoom Oct 14 '26
Actually, the comment only says the first
unsafe {}statement is safe. The second one is in an unknown state.2
Oct 14 '26
The comment is wrong, assuming
selfis&Self. That aliases mutable and immutable refs. That is unsound.1
u/tony-husk 26d ago
I just checked and this definitely compiles
1
13
u/letmehaveanameyoudum Oct 14 '26
as a C programmer, what the actual fuck am i looking at (explain mate)
21
u/pragmojo Oct 14 '26
Rust's borrow checker disallows holding two mutable pointers to the same address at the same time. This code uses unsafe blocks to get two mutable pointers to the same address at the same time.
6
2
u/schungx Oct 14 '26
If the intention is aliasing then this is a legit usage. If both
selfvariants are supposed to point to the same thing (judging from their names) then the aliasing behavior is intended.7
u/ChaiTRex Oct 14 '26
It's not legitimate.
unsafecan be fine to use, but undefined behavior is never allowed. Aliasing a mutable reference is undefined behavior.A workaround would be something like using mutable pointers rather than mutable references. Mutable pointers allow aliasing.
9
7
11
1
u/CorgiApprehensive159 Oct 20 '26
I, the author of the post, decided to take advantage of my 'popularity' to show my game https://livedox.itch.io/vix - it has no code in it like in the post (almost)
4
u/AnArmoredPony Oct 14 '26
as a java developer, I uhh I forgot what I wanted to say fuCK
2
u/jimmiebfulton Oct 15 '26
I hadn't thought that anyone but Rust people would find this sub humorous. It's definitely one of the darker, more obscure jerk subs I'm in.
2
2
2
1
1
u/alulalol Oct 18 '26
I've done this a lot in doukutsu-rs because I couldn't be assed to fight the borrow checker https://github.com/doukutsu-rs/doukutsu-rs/blob/7e6870e55f072e3f056dc1eef3974a1af26c7aae/src/framework/ui.rs#L111
1
u/cristi1990an Oct 19 '26
The syntax is horrible because even in unsafe code, bypassing the borrow checker this way is heavily discouraged. Simple reads and writes through these references can be UB.
1
u/ApprehensiveBag3083 Oct 14 '26
and they say c++ has creepy syntax LoL
At least in c++ i dont need to type those kind of garbage Lool
4
u/Athropod101 Oct 14 '26
I mean you don’t type it in Rust either, lol. This is like cherrypicking some C++ template hidden behind like 3 namespaces, with the generic being some obscure type behind another namespace, and all of that wrapped around std::make_unique.
Funny as Hell, yeah, but not really indicative of the language.
2
0
Oct 14 '26
[deleted]
16
u/SnooHamsters6620 Oct 14 '26
Don't worry, I whisper a protection spell to Ferris every time I compile.
130
u/renchuwang Oct 14 '26
Well since you're only defining but not using, it is actually safe!