Skip to content
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

Detect wrongly interchanged keys during runtime #103

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

Conversation

elftausend
Copy link

@elftausend elftausend commented Jul 14, 2023

This pull request refers to the following statement located in the docs:

If you have multiple slot maps it’s an error to use the key of one slot map on another slot map. The result is safe, but unspecified, and can not be detected at runtime, so it can lead to a hard to find bug.

However, there may be a way to detect such occurrences during runtime, though.
It utilises the core::panic::Location and #[track_caller] to match a SlotMap with a DefaultKey. This mechanic is used in RefCell as well.

I implemented a proof-of-concept for SlotMap only and would like to hear your opinion about this idea.

@orlp
Copy link
Owner

orlp commented Oct 31, 2023

This is certainly an interesting idea to do in debug mode. I think for FFI / serialization purposes it should be a best-effort optional thing though. So the location would be inside an Option and the test would be self.loc.zip(other.loc).map(|a, b| a == b).unwrap_or(true).

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.

2 participants