about summary refs log tree commit diff
path: root/compiler/rustc_transmute/src/maybe_transmutable
AgeCommit message (Collapse)AuthorLines
2025-06-09transmutability: shift abstraction boundaryJack Wrenn-114/+172
Previously, `rustc_transmute`'s layout representations were genericized over `R`, a reference. Now, it's instead genericized over representations of type and region. This allows us to move reference transmutability logic from `rustc_trait_selection` to `rustc_transmutability` (and thus unit test it independently of the compiler), and — in a follow-up PR — will make it possible to support analyzing function pointer transmutability with minimal surgery.
2025-05-02Rollup merge of #140509 - tmiasko:merge-contiguous-ranges, r=jswrennMatthias Krüger-1/+1
transmutability: merge contiguous runs with a common destination Based on #140380. r? `@jswrenn` `@joshlf`
2025-04-30transmutability: merge contiguous runs with a common destinationTomasz Miąsko-1/+1
2025-04-30transmutability: ensure_sufficient_stack when answering queryTomasz Miąsko-117/+125
2025-04-29transmutability: uninit transition matches unit byte onlyTomasz Miąsko-128/+29
The previous implementation was inconsistent about transitions that apply for an init byte. For example, when answering a query, an init byte could use corresponding init transition. Init byte could also use uninit transition, but only when the corresponding init transition was absent. This behaviour was incompatible with DFA union construction. Define an uninit transition to match an uninit byte only and update implementation accordingly. To describe that `Tree::uninit` is valid for any value, build an automaton that accepts any byte value. Additionally, represent byte ranges uniformly as a pair of integers to avoid special case for uninit byte.
2025-04-25transmutability: Support char, NonZeroXxxJoshua Liebow-Feeser-6/+76
Note that `NonZero` support is not wired up, as the author encountered bugs while attempting this. A future commit will wire up `NonZero` support.
2025-04-23transmutability: Mark edges by ranges, not valuesJoshua Liebow-Feeser-72/+343
In the `Tree` and `Dfa` representations of a type's layout, store byte ranges rather than needing to separately store each byte value. This permits us to, for example, represent a `u8` using a single 0..=255 edge in the DFA rather than using 256 separate edges. This leads to drastic performance improvements. For example, on the author's 2024 MacBook Pro, the time to convert the `Tree` representation of a `u64` to its equivalent DFA representation drops from ~8.5ms to ~1us, a reduction of ~8,500x. See `bench_dfa_from_tree`. Similarly, the time to execute a transmutability query from `u64` to `u64` drops from ~35us to ~1.7us, a reduction of ~20x. See `bench_transmute`.
2025-04-20transmutability: remove NFA intermediate representationJack Wrenn-26/+38
Prior to this commit, the transmutability analysis used an intermediate NFA representation of type layout. We then determinized this representation into a DFA, upon which we ran the core transmutability analysis. Unfortunately, determinizing NFAs is expensive. In this commit, we avoid NFAs entirely by observing that Rust `union`s are the only source of nondeterminism and that it is comparatively cheap to compute the DFA union of DFAs. We also implement Graphviz DOT debug formatting of DFAs. Fixes rust-lang/project-safe-transmute#23 Fixes rust-lang/project-safe-transmute#24
2025-04-16transmutability: Refactor tests for simplicityJoshua Liebow-Feeser-66/+74
2025-02-28Remove `allow(unused_variables)` for `rustc_transmute`.Nicholas Nethercote-4/+3
This was hiding some genuine sins, including unused arguments in numerous functions/methods (incl. trait methods), and some unnecessary computation.
2024-11-18use `TypingEnv` when no `infcx` is availablelcnr-2/+2
the behavior of the type system not only depends on the current assumptions, but also the currentnphase of the compiler. This is mostly necessary as we need to decide whether and how to reveal opaque types. We track this via the `TypingMode`.
2024-10-04Fix some pub(crate) that were undetected bc of instrumentMichael Goulet-1/+1
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-2/+2
2024-09-16layout computation: eagerly error for unexpected unsized fieldsLukas Markeffsky-1/+1
2024-09-03Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgaubors-2/+2
Add `unreachable_pub`, round 4 A follow-up to #129732. r? `@Urgau`
2024-09-03Add `warn(unreachable_pub)` to `rustc_transmute`.Nicholas Nethercote-2/+2
2024-09-02chore: Fix typos in 'compiler' (batch 3)Alexander Cyon-1/+1
2024-08-25Removes dead code from the compilermu001999-5/+1
2024-08-21safe transmute: gracefully bubble-up layout errorsJack Wrenn-10/+3
Changes `.unwrap()`s to `?` to avoid ICEs. Adds ui tests. Fixes #129327
2024-08-18safe transmute: forbid reference lifetime extensionJack Wrenn-4/+3
Modifies `BikeshedIntrinsicFrom` to forbid lifetime extensions on references. This static check can be opted out of with the `Assume::lifetimes` flag. Fixes #129097
2024-07-29Reformat `use` declarations.Nicholas Nethercote-17/+12
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-04-30Remove `extern crate tracing` from numerous crates.Nicholas Nethercote-0/+2
2024-04-08Compute transmutability from `rustc_target::abi::Layout`Jack Wrenn-12/+17
In its first step of computing transmutability, `rustc_transmutability` constructs a byte-level representation of type layout (`Tree`). Previously, this representation was computed for ADTs by inspecting the ADT definition and performing our own layout computations. This process was error-prone, verbose, and limited our ability to analyze many types (particularly default-repr types). In this PR, we instead construct `Tree`s from `rustc_target::abi::Layout`s. This helps ensure that layout optimizations are reflected our analyses, and increases the kinds of types we can now analyze, including: - default repr ADTs - transparent unions - `UnsafeCell`-containing types Overall, this PR expands the expressvity of `rustc_transmutability` to be much closer to the transmutability analysis performed by miri. Future PRs will work to close the remaining gaps (e.g., support for `Box`, raw pointers, `NonZero*`, coroutines, etc.).
2024-03-15Safe Transmute: Use 'not yet supported', not 'unspecified' in errorsJack Wrenn-2/+2
We can (and will) support analyzing the transmutability of types whose layouts aren't completely specified by its repr. This change ensures that the error messages remain sensible after this support lands.
2024-03-13safe transmute: require that src referent is smaller than dstJack Wrenn-0/+5
The source referent absolutely must be smaller than the destination referent of a ref-to-ref transmute; the excess bytes referenced cannot arise from thin air, even if those bytes are uninitialized.
2024-02-27safe transmute: revise safety analysisJack Wrenn-78/+99
Migrate to a simplified safety analysis that does not use visibility. Closes https://github.com/rust-lang/project-safe-transmute/issues/15
2023-10-06Remove the `MaybeTransmutableQuery<&'l Dfa<...>, C>` impl.Nicholas Nethercote-16/+0
Because there is also a `MaybeTransmutableQuery<Dfa<...>, C>` impl, and we don't need both.
2023-10-06Fix a comment.Nicholas Nethercote-3/+1
It was duplicated from the method above.
2023-10-06Remove `map_layouts`.Nicholas Nethercote-57/+33
As per the `FIXME` comment, it's an abstraction that makes the code harder to read.
2023-09-06Fix error report for size overflow from transmuteyukang-0/+2
2023-06-12Safe Transmute: Refactor error handling and Answer typeBryan Garza-83/+81
- Create `Answer` type that is not just a type alias of `Result` - Remove a usage of `map_layouts` to make the code easier to read - Don't hide errors related to Unknown Layout when computing transmutability
2023-05-24Safe Transmute: Update definition of Condition typeBryan Garza-11/+14
- Change `Condition` to not contain `Answer`s but instead just contain other `Condition`s directly. - Also improve error reporting for `DstHasStricterAlignment`
2023-05-24Safe Transmute: Check mutability before creating dst -> src obligationBryan Garza-0/+22
- Only create dst -> src obligation if Dst is mutable - Add some long comments to explain parts of the transmutability code that were unclear to me when reading - Update/add tests
2023-05-24Safe Transmute: Fix propagation of errorsBryan Garza-14/+29
- Make sure that the most specific Reason is the one that bubbles up when we are folding over the `Answer` tree. `Reason::DstIsBitIncompatible` is the least specific, so that should be used only when there isn't anything else available. - Small fixes where we used the wrong Reason variant. - Tiny cleanups
2023-05-24Safe Transmute: Change Answer type to ResultBryan Garza-109/+85
This patch updates the `Answer` type from `rustc_transmute` so that it just a type alias to `Result`. This makes it so that the standard methods for `Result` can be used to process the `Answer` tree, including being able to make use of the `?` operator on `Answer`s. Also, remove some unused functions
2023-05-24Safe Transmute: Enable handling references, including recursive typesBryan Garza-34/+136
This patch enables support for references in Safe Transmute, by generating nested obligations during trait selection. Specifically, when we call `confirm_transmutability_candidate(...)`, we now recursively traverse the `rustc_transmute::Answer` tree and create obligations for all the `Answer` variants, some of which include multiple nested `Answer`s. Also, to handle recursive types, enable support for coinduction for the Safe Transmute trait (`BikeshedIntrinsicFrom`) by adding the `#[rustc_coinduction]` annotation. Also fix some small logic issues when reducing the `or` and `and` combinations in `rustc_transmute`, so that we don't end up with additional redundant `Answer`s in the tree. Co-authored-by: Jack Wrenn <jack@wrenn.fyi>
2023-04-13Improve safe transmute error reportingBryan Garza-16/+17
This patch updates the error reporting when Safe Transmute is not possible between 2 types by including the reason. Also, fix some small bugs that occur when computing the `Answer` for transmutability.
2023-03-02rustc_middle: Remove trait `DefIdTree`Vadim Petrochenkov-3/+1
This trait was a way to generalize over both `TyCtxt` and `Resolver`, but now `Resolver` has access to `TyCtxt`, so this trait is no longer necessary.
2022-12-19clippy::complexity fixesMatthias Krüger-5/+1
filter_next needless_question_mark bind_instead_of_map manual_find derivable_impls map_identity redundant_slicing skip_while_next unnecessary_unwrap needless_bool
2022-09-04Auto merge of #100726 - jswrenn:transmute, r=oli-obkbors-3/+3
safe transmute: use `Assume` struct to provide analysis options This task was left as a TODO in #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](https://github.com/rust-lang/compiler-team/issues/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: - https://github.com/rust-lang/compiler-team/issues/411 - https://github.com/rust-lang/rust/issues/99571
2022-09-01Always import all tracing macros for the entire crate instead of piecemeal ↵Oli Scherer-3/+3
by module
2022-08-22safe transmute: use `Assume` struct to provide analysis optionsJack Wrenn-3/+3
This was left as a TODO in #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: https://github.com/rust-lang/compiler-team/issues/411 ref: https://github.com/rust-lang/rust/issues/99571
2022-07-27safe transmute: lowercase tracing levelsJack Wrenn-5/+5
ref: https://github.com/rust-lang/rust/pull/92268#discussion_r927095154
2022-07-27safe transmute: tweak tracingJack Wrenn-8/+8
ref: https://github.com/rust-lang/rust/pull/92268#discussion_r925246903 ref: https://github.com/rust-lang/rust/pull/92268#discussion_r925250811 ref: https://github.com/rust-lang/rust/pull/92268#discussion_r925255782
2022-07-27Initial (incomplete) implementation of transmutability trait.Jack Wrenn-0/+528
This initial implementation handles transmutations between types with specified layouts, except when references are involved. Co-authored-by: Igor null <m1el.2027@gmail.com>