Skip to contentSkip to conversation

rust: alloc: extend safety requirements of Vec::set_len()

PATCH Benno Lossin 1 participant 1 message Mar 17, 2025 Rust

lore
Share & tools
lore
#0 lore raw
Quoted 37 lines · Danilo Krummrich
Quoted 30 lines · Benno Lossin
Quoted 8 lines · Danilo Krummrich
Quoted 7 lines · Tamir Duberstein
Quoted 6 lines · Danilo Krummrich
Quoted 5 lines · Tamir Duberstein
Quoted 3 lines · Danilo Krummrich

Let's go with just "must be taken ownership of" then. Unless there's subsequent feedback, I won't send a new version for this, since you both already gave your (conditional) RB for this.

I didn't realize the forget argument immediately, so I sadly have to take my RB back.

Quoted 17 lines
Quoted 16 lines
Quoted 6 lines
Quoted 1 line

What does it mean to take ownership, if not to run the destructor?

Taking responsibility over the decision of what should happen to the value, i.e. forget about the value, drop it right away, keep it alive at a different memory location.

Quoted 6 lines
Quoted 2 lines

Given Benno's insight, I think the safety text is correct as it existed before this patch.

Without the addition there's no requirement for the caller to take ownership, but that's what we want here. Without the requirement it would be on set_len() to take a decision on what should happen with the value.

It isn't a safety requirement, right? I think it's fine to document, but given that neglecting to run a destructor is safe, it doesn't seem to affect safety.

Quoted 5 lines
Quoted 2 lines

I don't think there's a rule that safety requirements of unsafe functions must only be used to guarantee memory safety, etc.

Well that's their only point. Safety documentation should only be concerned with memory safety.

Quoted 8 lines

Oops, I wanted to say "must not only be used to *directly* guarantee memory safety, etc.".

For instance, it is also used to justify type invariants (that exist to prevent UB).

So, what I wanted to say is that setting self.len by iteself is never directly a safety critical thing.

In my mental model there is no difference between "possibly immediately UB" things and "possibly later UB" things. Both of the following examples produce UB:

    let mut vec: KVec<u32> = KVec::new();
    unsafe { vec.set_len(1) };
    let elem = vec[0]; // UB

and

   let mut vec: KVec<u32> = KVec::new();
   let ptr = vec.as_ptr();
   let elem = unsafe { ptr.read() }; // UB

While in the first example, the UB only occurs when calling a safe function, it is clearly the fault of the unsafe block before that caused it.

Quoted 10 lines
Quoted 7 lines

You can still put it in the normal documentation though.

Quoted 3 lines

But you're right, if set_len() guarantees a default of what happens to the value(s) when the caller doesn't take ownership, we could avoid the safety requirement.

Yeah the default is to forget the value.

It's not really a defined default though, it's just what ultimately happens as the consequence of how Vec in general and set_len() are implemented.

What would be an alternative? I'd argue that forgetting is the only sensible option.

Quoted 7 lines

But let's define it then; what about:

"[Vec::set_len] takes (or kepps) ownership of all elements within the range [0; new_len] and abandons ownership of all values outside of this range, if any."

The caller may take ownership of the abandoned elements."

For some reason I don't like the wording of ownership in this case... I'll try to come up with something better, since you're splitting the function into inc and dec anyways (which I think is a good idea), so I'll reply there.

---
Cheers,
Benno