about summary refs log tree commit diff
path: root/compiler/rustc_transmute/src/layout/tree.rs
AgeCommit message (Collapse)AuthorLines
2025-09-28remove explicit deref of AbiAlign for most methodsJubilee Young-1/+1
Much of the compiler calls functions on Align projected from AbiAlign. AbiAlign impls Deref to its inner Align, so we can simplify these away. Also, it will minimize disruption when AbiAlign is removed. For now, preserve usages that might resolve to PartialOrd or PartialEq, as those have odd inference.
2025-09-23Add an attribute to check the number of lanes in a SIMD vector after ↵Caleb Zulawski-0/+1
monomorphization Unify zero-length and oversized SIMD errors
2025-09-09Driveby fixesBoxy-3/+1
2025-09-09erase_regions to erase_and_anonymize_regionsBoxy-19/+20
2025-07-07compiler: Parse `p-` specs in datalayout string, allow definition of custom ↵Edoardo Marangoni-1/+1
default data address space
2025-06-27tag_for_variant: properly pass TypingEnvRalf Jung-1/+3
2025-06-09transmutability: shift abstraction boundaryJack Wrenn-21/+27
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-06-03Change `tag_field` to `FieldIdx` in `Variants::Multiple`Scott McMurray-1/+1
It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.
2025-04-25transmutability: Support char, NonZeroXxxJoshua Liebow-Feeser-23/+123
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-4/+4
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-21cleanup redundant pattern instancesJonathan Gruner-1/+1
2025-02-28Remove `allow(unused_variables)` for `rustc_transmute`.Nicholas Nethercote-8/+6
This was hiding some genuine sins, including unused arguments in numerous functions/methods (incl. trait methods), and some unnecessary computation.
2025-01-27Add `TooGeneric` variant to `LayoutError` and emit `Unknown` oneFedericoBruzzone-0/+1
- `check-pass` test for a MRE of #135020 - fail test for #135138 - switch to `TooGeneric` for checking CMSE fn signatures - switch to `TooGeneric` for compute `SizeSkeleton` (for transmute) - fix broken tests
2024-12-18add comment explaining why ty_and_layout_field is not usedRalf Jung-1/+4
Co-authored-by: Jack Wrenn <me@jswrenn.com>
2024-12-18make no-variant types a dedicated Variants variantRalf Jung-8/+7
2024-12-18Variants::Single: do not use invalid VariantIdx for uninhabited enumsRalf Jung-6/+4
2024-12-01fix safe-transmute handling of enumsRalf Jung-30/+27
2024-10-28Rollup merge of #132255 - workingjubilee:layout-is-🏚️, r=compiler-errorsJubilee-3/+1
Add `LayoutS::is_uninhabited` and use it Use accessors for the things that accessors are good at: reducing everyone's need to be nosy and peek at the internals of every data structure.
2024-10-28compiler: Add `is_uninhabited` and use LayoutS accessorsJubilee Young-3/+1
This reduces the need of the compiler to peek on the fields of LayoutS.
2024-10-27rustc_transmute: Directly use types from rustc_abiJubilee Young-5/+5
2024-10-01TransmuteFrom: Gracefully handle unnormalized types and normalization errorsJack Wrenn-2/+3
Fixes #130413
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-1/+1
2024-09-16layout computation: eagerly error for unexpected unsized fieldsLukas Markeffsky-6/+6
2024-09-16make `LayoutCx` not genericLukas Markeffsky-8/+8
2024-09-14Correctly account for niche-optimized tagsBen Kimock-10/+25
2024-09-03Add `warn(unreachable_pub)` to `rustc_transmute`.Nicholas Nethercote-1/+1
2024-08-21safe transmute: gracefully bubble-up layout errorsJack Wrenn-11/+7
Changes `.unwrap()`s to `?` to avoid ICEs. Adds ui tests. Fixes #129327
2024-08-18safe transmute: forbid reference lifetime extensionJack Wrenn-53/+88
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-16/+7
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-06-14safe transmute: support non-ZST, variantful, uninhabited enumsJack Wrenn-22/+14
Previously, `Tree::from_enum`'s implementation branched into three disjoint cases: 1. enums that uninhabited 2. enums for which all but one variant is uninhabited 3. enums with multiple inhabited variants This branching (incorrectly) did not differentiate between variantful and variantless uninhabited enums. In both cases, we assumed (and asserted) that uninhabited enums are zero-sized types. This assumption is false for enums like: enum Uninhabited { A(!, u128) } ...which, currently, has the same size as `u128`. This faulty assumption manifested as the ICE reported in #126460. In this PR, we revise the first case of `Tree::from_enum` to consider only the narrow category of "enums that are uninhabited ZSTs". These enums, whose layouts are described with `Variants::Single { index }`, are special in their layouts otherwise resemble the `!` type and cannot be descended into like typical enums. This first case captures uninhabited enums like: enum Uninhabited { A(!, !), B(!) } The second case is revised to consider the broader category of "enums that defer their layout to one of their variants"; i.e., enums whose layouts are described with `Variants::Single { index }` and that do have a variant at `index`. This second case captures uninhabited enums that are not ZSTs, like: enum Uninhabited { A(!, u128) } ...which represent their variants with `Variants::Single`. Finally, the third case is revised to cover the broader category of "enums with multiple variants", which captures uninhabited, non-ZST enums like: enum Uninhabited { A(u8, !), B(!, u32) } ...which represent their variants with `Variants::Multiple`. This PR also adds a comment requested by RalfJung in his review of #126358 to `compiler/rustc_const_eval/src/interpret/discriminant.rs`. Fixes #126460
2024-06-13safe transmute: support `Variants::Single` enumsJack Wrenn-21/+54
Previously, the implementation of `Tree::from_enum` incorrectly treated enums with `Variants::Single` and `Variants::Multiple` identically. This is incorrect for `Variants::Single` enums, which delegate their layout to that of a variant with a particular index (or no variant at all if the enum is empty). This flaw manifested first as an ICE. `Tree::from_enum` attempted to compute the tag of variants other than the one at `Variants::Single`'s `index`, and fell afoul of a sanity-checking assertion in `compiler/rustc_const_eval/src/interpret/discriminant.rs`. This assertion is non-load-bearing, and can be removed; the routine its in is well-behaved even without it. With the assertion removed, the proximate issue becomes apparent: calling `Tree::from_variant` on a variant that does not exist is ill-defined. A sanity check the given variant has `FieldShapes::Arbitrary` fails, and the analysis is (correctly) aborted with `Err::NotYetSupported`. This commit corrects this chain of failures by ensuring that `Tree::from_variant` is not called on variants that are, as far as layout is concerned, nonexistent. Specifically, the implementation of `Tree::from_enum` is now partitioned into three cases: 1. enums that are uninhabited 2. enums for which all but one variant is uninhabited 3. enums with multiple inhabited variants `Tree::from_variant` is now only invoked in the third case. In the first case, `Tree::uninhabited()` is produced. In the second case, the layout is delegated to `Variants::Single`'s index. Fixes #125811
2024-06-10ScalarInt: size mismatches are a bug, do not delay the panicRalf Jung-1/+1
2024-05-01Align: add bytes_usize and bits_usizeRalf Jung-1/+1
2024-04-19ScalarInt: add methods to assert being a (u)int of given sizeRalf Jung-1/+1
2024-04-08Compute transmutability from `rustc_target::abi::Layout`Jack Wrenn-234/+207
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-22Add `tag_for_variant` queryJack Wrenn-25/+19
This query allows for sharing code between `rustc_const_eval` and `rustc_transmutability`. Also moves `DummyMachine` to `rustc_const_eval`.
2024-03-15Safe Transmute: Use 'not yet supported', not 'unspecified' in errorsJack Wrenn-7/+7
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-1/+4
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-2/+3
Migrate to a simplified safety analysis that does not use visibility. Closes https://github.com/rust-lang/project-safe-transmute/issues/15
2024-01-09Fix an ICE that occurs after an error has already been reportedOli Scherer-0/+1
2023-09-06Fix error report for size overflow from transmuteyukang-0/+3
2023-07-27Don't attempt to compute layout of type referencing errorMichael Goulet-1/+1
2023-07-14refactor(rustc_middle): Substs -> GenericArgMahdi Dibaiee-7/+7
2023-07-01Put `LayoutError` behind reference to shrink resultNilstrieb-4/+4
`LayoutError` is 24 bytes, which is bigger than the `Ok` types, so let's shrink that.
2023-06-12Safe Transmute: Refactor error handling and Answer typeBryan Garza-2/+2
- 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: Enable handling references, including recursive typesBryan Garza-0/+11
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-16more clippy fixes: clippy::{iter_cloned_collect, unwarp_or_else_default, ↵Matthias Krüger-1/+1
option_map_or_none}
2023-04-13Improve safe transmute error reportingBryan Garza-12/+16
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-02-14s/eval_usize/eval_target_usize/ for clarityOli Scherer-2/+3
2022-11-05Auto merge of #103831 - chenyukang:yukang/fix-103751-ice, r=nagisabors-2/+2
Fix capacity overflow issue during transmutability check Fixes #103751