Skip to content

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Sep 23, 2025

Added Stack::clear for #14029.

Copy link
Member Author

sapphi-red commented Sep 23, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 23, 2025

CodSpeed Instrumentation Performance Report

Merging #14028 will not alter performance

Comparing 09-23-feat_data_structures_add_stack_clear_ (1521a1f) with main (70bd141)

Summary

✅ 37 untouched

@sapphi-red sapphi-red force-pushed the 09-23-feat_data_structures_add_stack_clear_ branch from c5758c6 to 3c3cdb2 Compare September 23, 2025 05:50
@sapphi-red sapphi-red force-pushed the 09-22-feat_mangler_mangle_private_class_members branch from ed0d229 to 20c266b Compare September 23, 2025 05:50
@sapphi-red sapphi-red marked this pull request as ready for review September 23, 2025 06:37
Copy link
Member

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.is_empty() check may not actually be required. When the stack's content type (T) is non-Drop, self.drop_contents() becomes a no-op anyway. So if we could remove self.is_empty() check,this function becomes just a single instruction and no branches, when T is non-Drop.

drop_contents has a safety comment saying "Stack must be allocated", but I'm not sure it's actually required. I think it's fine to call ptr::drop_in_place with an empty slice. But let's leave it as is - maybe there was a reason I added this safety constraint which I don't remember right now.

@sapphi-red
Copy link
Member Author

When the stack is not allocated, self.start is a dangling pointer. In that case, the return value of ptr::slice_from_raw_parts_mut(self.start().as_ptr(), self.len()) is not valid. I think that's the reason of the "Stack must be allocated" comment.
This self.is_empty() check indirectly checks that the stack is allocated from the fact that "non-empty stack would always have the stack allocated".

@sapphi-red sapphi-red changed the base branch from 09-22-feat_mangler_mangle_private_class_members to graphite-base/14028 September 23, 2025 13:19
@sapphi-red sapphi-red force-pushed the 09-23-feat_data_structures_add_stack_clear_ branch from 3c3cdb2 to 13de0f7 Compare September 23, 2025 13:23
@sapphi-red sapphi-red changed the base branch from graphite-base/14028 to main September 23, 2025 13:24
@overlookmotel
Copy link
Member

overlookmotel commented Sep 23, 2025

When the stack is not allocated, self.start is a dangling pointer. In that case, the return value of ptr::slice_from_raw_parts_mut(self.start().as_ptr(), self.len()) is not valid. I think that's the reason of the "Stack must be allocated" comment. This self.is_empty() check indirectly checks that the stack is allocated from the fact that "non-empty stack would always have the stack allocated".

Yeah, I think that's what I had in mind at the time I wrote the code. But now I think probably it is legal to create a slice starting at a dangling pointer, as long as it's 0 length (dangling pointers are correctly aligned).

Anyway, it doesn't really matter much. This PR just made me think about it again.

@graphite-app
Copy link
Contributor

graphite-app bot commented Sep 24, 2025

Merge activity

Copilot AI review requested due to automatic review settings September 24, 2025 11:10
@graphite-app graphite-app bot force-pushed the 09-23-feat_data_structures_add_stack_clear_ branch from 13de0f7 to 1521a1f Compare September 24, 2025 11:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@sapphi-red
Copy link
Member Author

sapphi-red commented Sep 24, 2025

Yeah, I think that's what I had in mind at the time I wrote the code. But now I think probably it is legal to create a slice starting at a dangling pointer, as long as it's 0 length (dangling pointers are correctly aligned).

Ah, I see. I think it's legal. I've just found this:

data must be non-null and aligned even for zero-length slices or slices of ZSTs. One reason for this is that enum layout optimizations may rely on references (including slices of any length) being aligned and non-null to distinguish them from other data. You can obtain a pointer that is usable as data for zero-length slices using NonNull::dangling().
https://doc.rust-lang.org/std/slice/fn.from_raw_parts_mut.html#:~:text=data%20must%20be,NonNull%3A%3Adangling().

...which is what we're doing here.

@graphite-app graphite-app bot merged commit 1521a1f into main Sep 24, 2025
27 checks passed
@graphite-app graphite-app bot deleted the 09-23-feat_data_structures_add_stack_clear_ branch September 24, 2025 11:16
@overlookmotel
Copy link
Member

You can obtain a pointer that is usable as data for zero-length slices using NonNull::dangling()

Nice. I hadn't spotted that in docs before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants