[rust-dev] A better type system

Daniel Micay danielmicay at gmail.com
Tue Jun 3 09:32:20 PDT 2014


On 03/06/14 05:58 AM, Sebastian Gesemann wrote:
> On Mon, Jun 2, 2014 at 10:09 PM, Matthew McPherrin wrote:
>> On Mon, Jun 2, 2014 at 8:25 AM, Patrick Walton wrote:
>>> On 6/2/14 12:44 AM, Tommi wrote:
>>>>
>>>> In my original post I stated that it feels like there's something wrong
>>>> with the language when it doesn't allow multiple mutable references to
>>>> the same data, but I didn't really explain why it feels like that. So, I
>>>> just want to add this simple example to help explain my position. It is
>>>> just plain obvious to everybody that the following code snippet is
>>>> memory-safe, but the compiler refuses to compile it due to "cannot
>>>> borrow `stuff[..]` as mutable more than once at a time":
>>>>
>>>> let mut stuff = [1, 2, 3];
>>>> let r1 = stuff.mut_slice_to(2);
>>>> let r2 = stuff.mut_slice_from(1);
>>>
>>> I'd like to have a function that splits up a vector in that way. That
>>> should be doable in the standard library using some unsafe code under the
>>> hood.
>>
>> Isn't this MutableVector's mut_split_at that we already have?
> 
> I thought about mentioning mut_split_at just to make people aware of
> it. But the resulting slices are not overlapping which is apparently
> what Tommi was interested. My understanding is that even if one uses
> an unsafe block to get two overlapping mutable slices, the use of
> those might invoke undefined behaviour because it violates some
> aliasing assumptions the compiler tends to exploit during
> optimizations. Correct me if I'm wrong.
> 
> Cheers!
> sg

It causes undefined behaviour because the language is defined that way.

It's defined that way both to allow for data parallelism and type-based
alias analysis. There's potentially room for another form of & reference
with aliasing, mutable data but the existing ones need to stay as they
are today.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140603/8c419306/attachment.sig>


More information about the Rust-dev mailing list