-
Notifications
You must be signed in to change notification settings - Fork 1.6k
NonZero lang item. #499
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
Closed
Closed
NonZero lang item. #499
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| - Start Date: 2014-12-05 | ||
| - RFC PR: (leave this empty) | ||
| - Rust Issue: (leave this empty) | ||
|
|
||
| # Summary | ||
|
|
||
| Add a `NonZero` lang item to be used to hint to the compiler that the wrapped pointer or integral value is never 0/NULL. | ||
|
|
||
| # Motivation | ||
|
|
||
| We currently take advantage of the fact that we know certain kinds of types either are never NULL themselves (`Box`, `&`, function pointers) or contain some pointer which we know to never be NULL (the pointer in a slice, trait object, closure). Specifically, for certain kinds of enums the contain these types, we don't add in an extra field for the discriminant but rather non-nullness of the type to encode which of two possible variants the enum may be. | ||
|
|
||
| This is what enables `size_of::<Box<T>>() == size_of::<Option<Box<T>>>()`. | ||
|
|
||
| Unfortunately we can't take advantage of this in library code leading to extra overhead for `Option<Vec<T>>`, `Rc<T>` and the like. | ||
|
|
||
| # Detailed design | ||
|
|
||
| As part of this we'd introduce a new lang item: | ||
| ``` | ||
| #[lang = "non_zero"] | ||
| struct NonZero<T>(T); | ||
| ``` | ||
|
|
||
| Then, `trans::adt` can treat `NonZero` fields that wrap a pointer or integral type in the same manner as it currently does for `Box<T>` and the like. Thus, we'd represent such enums as `RawNullablePointer/StructWrappedNullablePointer`. | ||
|
|
||
| In using it, creating an instance of `NonZero` would be an unsafe operation (private field, mark `::new` as unsafe). We could also implement `Deref` for convenience (i.e. for `foo: NonZero<*mut i8>`, `*foo` would return `*mut i8` since `*mut i8` is `Copy`). | ||
|
|
||
| # Drawbacks | ||
|
|
||
|
|
||
|
|
||
| # Alternatives | ||
|
|
||
| We could possibly do this with an attribute instead but it's a bit more annoying to mark it as unsafe beyond just the name or hardcoding that specific attribute in rustc. | ||
|
|
||
| # Unresolved questions | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if I put a non-pointer, non-integral type in
NonZero?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the initial implementation in rust-lang/rust#19536 it doesn't do anything special. It would just act as any other struct there and the optimization wouldn't kick in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems like a reasonable approach, but I think we may want to make it an error to use
NonZerofor non-supported types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, we could probably actually do that just by limiting the types constructor will accept.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be baked into the compiler, because there is no trait for the types we'd like to fit here. We could possibly write a marker-only trait with
unsafetraits and default traits.