Skip to content

Support coercions and intrinsics for unsized ADTs - #4722

Merged
philberty merged 4 commits into
Rust-GCC:masterfrom
nsvke:add-unsize
Aug 15, 2026
Merged

Support coercions and intrinsics for unsized ADTs#4722
philberty merged 4 commits into
Rust-GCC:masterfrom
nsvke:add-unsize

Conversation

@nsvke

@nsvke nsvke commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

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 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.


This patch implements the 'unsize' lang item to the compiler.

@nsvke
nsvke force-pushed the add-unsize branch 3 times, most recently from 9c0d26f to 533d32e Compare July 27, 2026 10:54
@nsvke
nsvke force-pushed the add-unsize branch 2 times, most recently from 0704e97 to 9282cab Compare August 3, 2026 07:58
@nsvke

nsvke commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

@philberty

@nsvke
nsvke marked this pull request as draft August 4, 2026 16:06
@nsvke nsvke mentioned this pull request Aug 5, 2026
@nsvke
nsvke marked this pull request as ready for review August 5, 2026 09:12
nsvke added 4 commits August 9, 2026 14:35
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>

@philberty philberty left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@philberty
philberty added this pull request to the merge queue Aug 15, 2026
Merged via the queue into Rust-GCC:master with commit 53122e3 Aug 15, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants