Skip to content

std.container.rbtree: Return range elements by ref #10755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

CyberShadow
Copy link
Member

@CyberShadow CyberShadow commented Apr 19, 2025

I don't see any downsides, and it will allow updating value types in-place as we're iterating over the tree.

Some counter-arguments I can think of:

  • What if this is used to change the key such that the tree is no longer ordered?

    • Well you can do that already anyway if Elem is a reference type, it's not like we're returning const.
  • What if the tree is changed while we're holding a reference to a node?

    • We're allocating nodes with new, so even if they're gone from the tree, it shouldn't result in a dangling pointer.

Did I miss anything?

CC @schveiguy

@CyberShadow CyberShadow requested a review from PetarKirov as a code owner April 19, 2025 09:21
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @CyberShadow!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10755"

@CyberShadow
Copy link
Member Author

Looks like the compiler was incorrectly allowing ref in foreach over such ranges, too:

import std.container.rbtree;

void main()
{
	auto t = new RedBlackTree!int();
	t.insert(2);
	foreach (ref r; t[]) // "ref" compiles, but doesn't do anything
		r++;
	assert(t[].front == 3); // fails
}

@Herringway
Copy link
Contributor

Unfortunately, it seems the fix dlang/dmd#16381 is stalled or dead.

@CyberShadow
Copy link
Member Author

Unfortunately, it seems the fix dlang/dmd#16381 is stalled or dead.

From what I see that seems to target the array index. Or does it also affect values of ranges with non-ref elements?

@Herringway
Copy link
Contributor

Unfortunately, it seems the fix dlang/dmd#16381 is stalled or dead.

From what I see that seems to target the array index. Or does it also affect values of ranges with non-ref elements?

It does. It targetted two issues.

@schveiguy
Copy link
Member

  • What if this is used to change the key such that the tree is no longer ordered?

This is the only qualm I have about it. But you are right that returning by value may still allow mutation that would mess up the comparison.

I'd say that maybe a comment about this should be in there, but apparently the Range struct is private, so it doesn't even get ddoc'd!

Copy link
Member

@schveiguy schveiguy left a comment

Choose a reason for hiding this comment

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

You should add ref to the container's front and back as well, for consistency.

@CyberShadow
Copy link
Member Author

I wrote this note:

* Note: The element is returned by `ref`, however,
* this should not be used to modify the element in a way which
* would affect the order defined by the `less` predicate.

but I'm not sure if it should be repeated 4 times.

@schveiguy
Copy link
Member

Hm... maybe add it to the RedBlackTree ddoc comment?

@CyberShadow
Copy link
Member Author

Done, how's this?

Copy link
Member

@schveiguy schveiguy left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants