-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: ForbiddenValues
trait to enable more optimizations
#2888
Closed
Closed
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
426b973
Create 0000-forbidden-value-trait.md
tommit 6e88bc9
Merge pull request #1 from tommit/forbidden-value-trait
tommit e22910a
Revert "RFC: ForbiddenValue trait to enable more optimizations"
tommit 467b0a6
Merge pull request #2 from tommit/revert-1-forbidden-value-trait
tommit d1ca975
Create 0000-forbidden-value-trait.md
tommit 44e8f12
Update 0000-forbidden-value-trait.md
tommit 5de38b6
Ability to define multiple forbidden values
tommit 27824e1
Fixed a couple of mistakes
tommit 5765e77
An unresolved question about undefined behaviour
tommit ca5aab7
Removed generic parameters (must not have them)
tommit 42277e2
Question about a union of sets of forbidden values
tommit a9645a3
Latest question answered in the contract
tommit 23e5c9b
Forbidden value uniqueness requirement
tommit 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 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,52 @@ | ||
- Start Date: 2020-03-24 | ||
- RFC PR #: | ||
- Rust Issue #: | ||
|
||
# Summary | ||
|
||
Some types have one or more forbidden values. Such values can be sometimes used to perform [optimizations](https://github.com/rust-lang/rust/pull/45225). For example, rustc currently uses a known forbidden value of a certain type `A` (like `&T`, `Box<T>`, `NonNull<T>`, `NonZeroI8` and some enum types) to represent the `None` enum variant of `Option<A>`. I propose to give users the ability to enable the same optimizations for types rustc currently doesn't know a forbidden value exists for. | ||
|
||
# Motivation | ||
|
||
This would increase the number of situations where the compiler can do the aforementioned, genuinely useful optimizations. | ||
|
||
# Detailed design | ||
|
||
Add the following trait somewhere in the standard library: | ||
```rust | ||
use std::{array::FixedSizeArray, mem::size_of}; | ||
|
||
unsafe trait ForbiddenValues { | ||
type Forbidden: FixedSizeArray<[u8; size_of::<Self>()]>; | ||
|
||
const FORBIDDEN: Self::Forbidden; | ||
} | ||
``` | ||
|
||
To implement `ForbiddenValues` for type `T`, the following conditions must be met: | ||
|
||
1. For each value `t` of type `T`, and for each item `f` in `T::FORBIDDEN`: | ||
```rust | ||
*(&raw const t as *const [u8; size_of::<T>()]) != f | ||
``` | ||
2. `T` must not be a type that has forbidden values the compiler already knows about (e.g. `char`) | ||
|
||
Then compilers would be allowed to use `T::FORBIDDEN` to represent forbidden values of type `T` in whatever optimizations they decide to perform. | ||
|
||
# Alternatives | ||
|
||
This is a simple proposal, but a step further would be to make all primitive and standard library types that have forbidden value(s) implement `ForbiddenValues`. And an even further step would be have Rust (the language) specify which optimizations are guaranteed to happen. | ||
|
||
Presumably the following syntax should work in the future: | ||
```rust | ||
unsafe trait ForbiddenValues { | ||
const COUNT: usize; | ||
const FORBIDDEN_VALUES: [[u8; size_of::<Self>()]; Self::COUNT]; | ||
} | ||
``` | ||
|
||
# Unresolved questions | ||
|
||
Are there some alignment issues with `ForbiddenValues::FORBIDDEN`? I don't think so, because the compiler is free to use those bytes however it likes. | ||
|
||
Given that producing a forbidden value of a `bool`, `char`, or an enum type is considered undefined behaviour, I suppose it makes sense to ask if it should be considered undefined behaviour to produce a value `t` of type `T` such that for some item `f` in `T::FORBIDDEN`, `*(&raw const t as *const [u8; size_of::<T>()]) == f`. |
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.
How about other types, are there any other types that the compiler already knows about? Is it possible to lists them out?
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.
Not sure how relevant this is now that the issue's closed, but https://doc.rust-lang.org/std/#primitives is a list of primitive types in the language. Which of these counts as "having forbidden values" depends on what you mean by that. For example, "uninitialized" is a forbidden / insta-UB-causing value for all of them, and
str
has to be valid UTF-8 within safe code but unsafe code can violate that without triggering UB, and I don't know if you'd want to include library types likeNonZero*
, and so on and so forth. But if we try to just ignore all of this crippling scope vagueness, I thinkbool
is the only other primitive type that is "likechar
" insofar as it clearly has to be implemented as one of the integer types with certain values excluded (inbool
's case, every value except 0 or 1).