Support coercions and intrinsics for unsized ADTs - #4722
Merged
Conversation
nsvke
force-pushed
the
add-unsize
branch
3 times, most recently
from
July 27, 2026 10:54
9c0d26f to
533d32e
Compare
nsvke
force-pushed
the
add-unsize
branch
2 times, most recently
from
August 3, 2026 07:58
0704e97 to
9282cab
Compare
Contributor
Author
nsvke
marked this pull request as draft
August 4, 2026 16:06
Merged
nsvke
marked this pull request as ready for review
August 5, 2026 09:12
This patch implements the 'unsize' lang item to the compiler. gcc/rust/ChangeLog: * typecheck/rust-tyty.cc (BaseType::unsize_to): New function. (BaseType::satisfies_bound): Use it. * typecheck/rust-tyty.h (class BaseType): New declaration. * util/rust-lang-item.cc (Rust::LangItem::lang_items): Add unsize to the BiMap. * util/rust-lang-item.h (class LangItem): Add UNSIZE to the Kind enum. gcc/testsuite/ChangeLog: * rust/compile/unsize2.rs: New test. Signed-off-by: Enes Cevik <enes@nsvke.com>
This patch enables fundamental array-to-slice (`[T; N]` to `[T]`) unsized coercions. Previously, the compiler only handled `dyn Trait` unsized coercions and immediately rejected array-to-slice conversions. Now, the compiler correctly identifies valid array-to-slice coercions and generates the appropriate `Adjustment::UNSIZE` tags. gcc/rust/ChangeLog: * typecheck/rust-coercion.cc (TypeCoercionRules::coerce_unsized): Add array-to-slice coercion support. gcc/testsuite/ChangeLog: * rust/compile/coercion.rs: New test. Signed-off-by: Enes Cevik <enes@nsvke.com>
This patch adds basic support for ADT to ADT unsized coercions. It also refactors the main coerce_unsized function into smaller helper functions for better readability and maintainability. gcc/rust/ChangeLog: * backend/rust-compile-base.h (resolve_adjustments): Fix typo. (resolve_unsized_adt_adjustment): New declaration. * backend/rust-compile-expr.cc (CompileExpr::visit): Fix typo. (HIRCompileBase::resolve_unsized_adt_adjustment): New function. (HIRCompileBase::resolve_unsized_adjustment): Use it. (CompileExpr::generate_possible_fn_trait_call): Likewise. (HIRCompileBase::coercion_site): Fix typo. * typecheck/rust-coercion.cc (TypeCoercionRules::coerce_unsized): Use helper functions. (TypeCoercionRules::unwrap_ptrs_and_refs): New function. (TypeCoercionRules::coerce_unsized_array_to_slice): Likewise. (TypeCoercionRules::coerce_unsized_dyn): Likewise. (TypeCoercionRules::coerce_unsized_adt): Liksewise. (TypeCoercionRules::apply_reborrow_adjustment): Likewise. * typecheck/rust-coercion.h (struct CoercionSetup): New struct. (coerce_unsized): Add is_inner parameter. (unwrap_ptrs_and_refs): New declaration. (coerce_unsized_array_to_slice): Likewise. (coerce_unsized_dyn): Likewise. (coerce_unsized_adt): Likewise. * util/rust-lang-item.cc (Rust::LangItem::lang_items): Add coerce_unsized to the BiMap. * util/rust-lang-item.h (class LangItem): Add COERCE_UNSIZED to the Kind enum. gcc/testsuite/ChangeLog: * rust/compile/coercion2.rs: New test. Signed-off-by: Enes Cevik <enes@nsvke.com>
This patch introduces support for Dynamically Sized Types (DST) within Algebraic Data Types (ADTs). Previously, the compiler embedded DST fields directly into the ADT memory layout, even for ADTs following unsize coercion rules. With this update, the compiler correctly identifies unsized ADTs, preserves their static layout by avoiding direct embedding, and appropriately generates fat pointers (containing the data pointer and metadata) for references to these ADTs. gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::visit): Add indirect field access for DST's and new borrow approach for fat pointers. (resolve_unsized_adt_adjustment): Correct GIMPLE generation for nested unsized ADTs. * backend/rust-compile-type.cc (TyTyResolveCompile::visit): Add DST ADT support. (TyTyResolveCompile::create_dyn_adt_record): New function. * backend/rust-compile-type.h (create_dyn_adt_record): New declaration. * backend/rust-intrinsic-handlers.cc (get_inner_dst): New function. (size_of_val_handler): Add DST ADT support. (min_align_of_val_handler): Likewise. * typecheck/rust-coercion.cc (TypeCoercionRules::coerce_unsized_adt): Skip zero-sized fields during coercion. * backend/rust-compile.cc (HIRCompileBase::coerce_to_dyn_object): Remove unnecessary ref wrapping. * typecheck/rust-tyty.cc (ADTType::is_unsized): New function. (ReferenceType::is_dyn_object): Add DST ADT support. (ReferenceType::is_dyn_adt_type): New function. (PointerType::is_dyn_object): Add DST ADT support. (PointerType::is_dyn_adt_type): New function. * typecheck/rust-tyty.h (is_unsized): New declaration. gcc/testsuite/ChangeLog: * rust/execute/unsized-adt-nested-coercion.rs: New test. * rust/execute/unsized-adt-size.rs: New test. Signed-off-by: Enes Cevik <enes@nsvke.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This patch introduces support for Dynamically Sized Types (DST) within
Algebraic Data Types (ADTs). Previously, the compiler embedded DST
fields directly into the ADT memory layout, even for ADTs following
unsize coercion rules. With this update, the compiler correctly
identifies unsized ADTs, preserves their static layout by avoiding
direct embedding, and appropriately generates fat pointers (containing
the data pointer and metadata) for references to these ADTs.
This patch adds basic support for ADT to ADT unsized coercions. It also
refactors the main coerce_unsized function into smaller helper functions
for better readability and maintainability.
This patch enables fundamental array-to-slice (
[T; N]to[T])unsized coercions. Previously, the compiler only handled
dyn Traitunsized coercions and immediately rejected array-to-slice conversions.
Now, the compiler correctly identifies valid array-to-slice coercions
and generates the appropriate
Adjustment::UNSIZEtags.This patch implements the 'unsize' lang item to the compiler.