-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Initial implementation of transmutability trait. #92268
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
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 did not review the new crate at all yet
if obligation.potentially_has_param_types_or_consts() { | ||
return candidates.ambiguous = false; |
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's going on here? Why can we set the ambiguity to false if there are generics involved? Because we know that all impls must be from trait bounds? Not sure that is true. There may be user impls on generic types giving us an ambiguous result, and now you're overwriting it as unambiguous. I don't know if this can happen or if it's fine as we'll later error out due to ambiguity, but this early return should be documented
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'm also deeply unsure about this code. If I comment it out, the compiler panics here while building stage 1 artifacts. I think I recall putting this check here after discovering that the trait definition itself was causing problems.
Really, I just stumbled my way through assemble_candidates_for_transmutability
and would be very grateful for insight of what should go there.
compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
Outdated
Show resolved
Hide resolved
r? @oli-obk |
compiler/rustc_transmute/src/lib.rs
Outdated
Yes, | ||
|
||
/// `Src` is NOT transmutable into `Dst`. | ||
No, |
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.
Re. better diagnostics: I suspect eventually we'll make this No
carry information about the incompatibility that makes the transmute invalid. The challenge with this is going to be ensure that the single counter-example that's eventually surfaced in the diagnostic is the most useful one.
1d8176e
to
f848e36
Compare
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #92915) made this pull request unmergeable. Please resolve the merge conflicts. |
Ping from triage: Can you please post the status of this PR? |
Hey @JohnCSimon, so sorry for missing your comment (I had just defended my thesis, went offline for a bit, and evidently missed a lot of notifications). This PR needs some minor work before it's ready to graduate out of draft status, namely: implementing the NFA lowering for enums and unions. I've budgeted that work for this Thursday. Basic support for references, error messages, and recursive types will come in follow-up PRs. |
This comment has been minimized.
This comment has been minimized.
Hm, I don't get the diagnostics about |
compiler/rustc_transmute/src/nfa.rs
Outdated
|
||
/// Constructs an `Nfa` that describes the layout of the given discriminant. | ||
pub fn from_disr(discr: Discr<'tcx>, tcx: TyCtxt<'tcx>) -> Self { | ||
// FIXME(@jswrenn): I'm certain this is missing needed endian nuance. |
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.
Maybe rustc_middle::mir::interpret::write_target_uint
?
compiler/rustc_transmute/src/nfa.rs
Outdated
} | ||
AdtKind::Enum => { | ||
// is the layout well-defined? | ||
if !(adt_def.repr().c() || adt_def.repr().int.is_some()) { |
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.
should be &&
here.
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.
Doesn't this have a well-defined layout?
#[repr(u8)]
enum Foo { A, B, C }
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.
Yes, it does.
However, #[repr(u8)] enum Foo { A(u8,u32,u8) }
does not
has a different representation from #[repr(u8, C)]
.
Edit: It was wrong
@bors r=oli-obk |
⌛ Testing commit e8a1925 with merge 6ac6e3903baf610c3dfe8071e0919b0e5c1a480d... |
💔 Test failed - checks-actions |
This comment has been minimized.
This comment has been minimized.
@bors r=oli-obk |
@bors p=15 (moving before rollup, also pr is older than half a year already) |
☀️ Test successful - checks-actions |
Finished benchmarking commit (e4417cf): comparison url. Instruction count
Max RSS (memory usage)Results
CyclesResults
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression Footnotes |
This was left as a TODO in rust-lang#92268, and brings the trait more in line with what was defined in MCP411. `Assume::visibility` has been renamed to `Assume::safety`, as library safety is what's actually being assumed; visibility is just the mechanism by which it is currently checked (this may change). ref: rust-lang/compiler-team#411 ref: rust-lang#99571
safe transmute: use `Assume` struct to provide analysis options This task was left as a TODO in rust-lang#92268; resolving it brings [`BikeshedIntrinsicFrom`](https://doc.rust-lang.org/nightly/core/mem/trait.BikeshedIntrinsicFrom.html) more in line with the API defined in [MCP411](rust-lang/compiler-team#411). **Before:** ```rust pub unsafe trait BikeshedIntrinsicFrom< Src, Context, const ASSUME_ALIGNMENT: bool, const ASSUME_LIFETIMES: bool, const ASSUME_VALIDITY: bool, const ASSUME_VISIBILITY: bool, > where Src: ?Sized, {} ``` **After:** ```rust pub unsafe trait BikeshedIntrinsicFrom<Src, Context, const ASSUME: Assume = { Assume::NOTHING }> where Src: ?Sized, {} ``` `Assume::visibility` has also been renamed to `Assume::safety`, as library safety invariants are what's actually being assumed; visibility is just the mechanism by which it is currently checked (and that may change). r? `@oli-obk` --- Related: - rust-lang/compiler-team#411 - rust-lang#99571
safe transmute: use `Assume` struct to provide analysis options This task was left as a TODO in rust-lang#92268; resolving it brings [`BikeshedIntrinsicFrom`](https://doc.rust-lang.org/nightly/core/mem/trait.BikeshedIntrinsicFrom.html) more in line with the API defined in [MCP411](rust-lang/compiler-team#411). **Before:** ```rust pub unsafe trait BikeshedIntrinsicFrom< Src, Context, const ASSUME_ALIGNMENT: bool, const ASSUME_LIFETIMES: bool, const ASSUME_VALIDITY: bool, const ASSUME_VISIBILITY: bool, > where Src: ?Sized, {} ``` **After:** ```rust pub unsafe trait BikeshedIntrinsicFrom<Src, Context, const ASSUME: Assume = { Assume::NOTHING }> where Src: ?Sized, {} ``` `Assume::visibility` has also been renamed to `Assume::safety`, as library safety invariants are what's actually being assumed; visibility is just the mechanism by which it is currently checked (and that may change). r? `@oli-obk` --- Related: - rust-lang/compiler-team#411 - rust-lang#99571
T'was the night before Christmas and all through the codebase, not a miri was stirring — no hint of
unsafe
!This PR provides an initial, incomplete implementation of MCP 411: Lang Item for Transmutability. The
core::mem::BikeshedIntrinsicFrom
trait provided by this PR is implemented on-the-fly by the compiler for typesSrc
andDst
when the bits of all possible values of typeSrc
are safely reinterpretable as a value of typeDst
.What this PR provides is:
Assume::VALIDITY
)What isn't yet implemented:
Assume
structThese features will be implemented in future PRs.