Skip to content

ast: Implement reconstruct() logic for Pattern nodes#4431

Draft
Pasta-coder wants to merge 1 commit intoRust-GCC:masterfrom
Pasta-coder:reconstruct-rust-pattern
Draft

ast: Implement reconstruct() logic for Pattern nodes#4431
Pasta-coder wants to merge 1 commit intoRust-GCC:masterfrom
Pasta-coder:reconstruct-rust-pattern

Conversation

@Pasta-coder
Copy link
Contributor

@Pasta-coder Pasta-coder commented Feb 10, 2026

This patch implements the reconstruct() pattern for the Pattern AST hierarchy. This is the AST reconstruction refactor (following Types and Paths in #3799 and #4395 ), designed to allow deep copying of AST nodes while guaranteeing the generation of fresh NodeIDs.

This is essential for macro expansion (specifically derive macros), where patterns must be duplicated without sharing NodeIDs to avoid resolution collisions.

Key changes include:

  • Implementing reconstruct_impl for all Pattern subclasses.
  • Adding reconstruct support to helper classes (RangePatternBound, StructPatternField, PatternItems).
  • Implementing typed reconstruct() wrappers for item families (TupleStructItems, TuplePatternItems, SlicePatternItems) to ensure type safety and avoid manual downcasting.
  • Adding null-safety checks in copy constructors and reconstruction logic to handle optional fields (e.g., open-ended ranges or stripped nodes) without crashing.
  • Integrating with PathInExpression::reconstruct() for struct patterns.
  • Updating derive(PartialEq) expansion logic to use reconstruct()
    instead of rebuilding paths manually.

gcc/rust/ChangeLog:

* expand/rust-derive-cmp-common.cc (EnumMatchBuilder::tuple): Use
reconstruct() to clone variant paths.
(EnumMatchBuilder::strukt): Likewise. 
* ast/rust-ast.h (Pattern::reconstruct): New public API.
(Pattern::reconstruct_impl): New pure virtual method.
* ast/rust-pattern.h (Pattern::reconstruct_impl): New virtual method.
(LiteralPattern::reconstruct_impl): Implement reconstruction.
(IdentifierPattern::reconstruct_impl): Likewise.
(WildcardPattern::reconstruct_impl): Likewise.
(RestPattern::reconstruct_impl): Likewise.
(RangePatternBound::reconstruct): New method.
(RangePatternBound::reconstruct_impl): New virtual method.
(RangePatternBoundLiteral::reconstruct_impl): Implement reconstruction.
(RangePatternBoundPath::reconstruct_impl): Likewise.
(RangePatternBoundQualPath::reconstruct_impl): Likewise.
(RangePattern::RangePattern): Fix copy constructor null safety.
(RangePattern::reconstruct_impl): Implement reconstruction with guards.
(ReferencePattern::ReferencePattern): Fix copy constructor null safety.
(ReferencePattern::reconstruct_impl): Implement reconstruction.
(StructPatternField::reconstruct): New method.
(StructPatternField::reconstruct_impl): New virtual method.
(StructPatternField::operator=): Default the assignment operator.
(StructPatternFieldTuplePat::reconstruct_impl): Implement reconstruction.
(StructPatternFieldIdentPat::reconstruct_impl): Likewise.
(StructPatternFieldIdent::reconstruct_impl): Likewise.
(StructPatternElements::reconstruct): New method.
(StructPattern::reconstruct_impl): Implement reconstruction using path reconstruct.
(PatternItems::reconstruct): New protected method.
(PatternItems::reconstruct_impl): New virtual method.
(TupleStructItems::reconstruct): New typed reconstruction method.
(TupleStructItems::reconstruct_impl): New pure virtual implementation.
(TupleStructItemsNoRest::reconstruct_impl): Implement reconstruction.
(TupleStructItemsHasRest::reconstruct_impl): Likewise.
(TupleStructPattern::reconstruct_impl): Implement reconstruction.
(TuplePatternItems::reconstruct): New typed reconstruction method.
(TuplePatternItems::reconstruct_impl): New pure virtual implementation.
(TuplePatternItemsNoRest::reconstruct_impl): Implement reconstruction.
(TuplePatternItemsHasRest::reconstruct_impl): Likewise.
(TuplePattern::reconstruct_impl): Implement reconstruction.
(GroupedPattern::reconstruct_impl): Likewise.
(SlicePatternItems::reconstruct): New typed reconstruction method.
(SlicePatternItems::reconstruct_impl): New pure virtual implementation.
(SlicePatternItemsNoRest::reconstruct_impl): Implement reconstruction.
(SlicePatternItemsHasRest::reconstruct_impl): Likewise.
(SlicePattern::reconstruct_impl): Implement reconstruction.
(AltPattern::reconstruct_impl): Likewise.

@Pasta-coder Pasta-coder marked this pull request as draft February 10, 2026 05:57
This patch implements the `reconstruct()` pattern for the `Pattern` AST
hierarchy. This is Phase 3 of the AST reconstruction refactor (following
Types and Paths), designed to allow deep copying of AST nodes while
guaranteeing the generation of fresh NodeIDs.

This is essential for macro expansion (specifically `derive` macros),
where patterns must be duplicated without sharing NodeIDs to avoid
resolution collisions.

Key changes include:
- Implementing `reconstruct_impl` for all `Pattern` subclasses.
- Adding `reconstruct` support to helper classes (`RangePatternBound`,
  `StructPatternField`, `PatternItems`).
- Implementing typed `reconstruct()` wrappers for item families
  (`TupleStructItems`, `TuplePatternItems`, `SlicePatternItems`) to
  ensure type safety and avoid manual downcasting.
- Adding null-safety checks in copy constructors and reconstruction
  logic to handle optional fields (e.g., open-ended ranges or stripped
  nodes) without crashing.
- Integrating with `PathInExpression::reconstruct()` for struct patterns.
- Updating `derive(PartialEq)` expansion logic to use `reconstruct()`
  instead of rebuilding paths manually.

gcc/rust/ChangeLog:

	* ast/rust-ast.h (Pattern::reconstruct): New public API.
	(Pattern::reconstruct_impl): New pure virtual method.
	* ast/rust-pattern.h (Pattern::reconstruct_impl): New virtual method.
	(LiteralPattern::reconstruct_impl): Implement reconstruction.
	(IdentifierPattern::reconstruct_impl): Likewise.
	(WildcardPattern::reconstruct_impl): Likewise.
	(RestPattern::reconstruct_impl): Likewise.
	(RangePatternBound::reconstruct): New method.
	(RangePatternBound::reconstruct_impl): New virtual method.
	(RangePatternBoundLiteral::reconstruct_impl): Implement reconstruction.
	(RangePatternBoundPath::reconstruct_impl): Likewise.
	(RangePatternBoundQualPath::reconstruct_impl): Likewise.
	(RangePattern::RangePattern): Fix copy constructor null safety.
	(RangePattern::reconstruct_impl): Implement reconstruction with guards.
	(ReferencePattern::ReferencePattern): Fix copy constructor null safety.
	(ReferencePattern::reconstruct_impl): Implement reconstruction.
	(StructPatternField::reconstruct): New method.
	(StructPatternField::reconstruct_impl): New virtual method.
	(StructPatternField::operator=): Default the assignment operator.
	(StructPatternFieldTuplePat::reconstruct_impl): Implement reconstruction.
	(StructPatternFieldIdentPat::reconstruct_impl): Likewise.
	(StructPatternFieldIdent::reconstruct_impl): Likewise.
	(StructPatternElements::reconstruct): New method.
	(StructPattern::reconstruct_impl): Implement reconstruction using path reconstruct.
	(PatternItems::reconstruct): New protected method.
	(PatternItems::reconstruct_impl): New virtual method.
	(TupleStructItems::reconstruct): New typed reconstruction method.
	(TupleStructItems::reconstruct_impl): New pure virtual implementation.
	(TupleStructItemsNoRest::reconstruct_impl): Implement reconstruction.
	(TupleStructItemsHasRest::reconstruct_impl): Likewise.
	(TupleStructPattern::reconstruct_impl): Implement reconstruction.
	(TuplePatternItems::reconstruct): New typed reconstruction method.
	(TuplePatternItems::reconstruct_impl): New pure virtual implementation.
	(TuplePatternItemsNoRest::reconstruct_impl): Implement reconstruction.
	(TuplePatternItemsHasRest::reconstruct_impl): Likewise.
	(TuplePattern::reconstruct_impl): Implement reconstruction.
	(GroupedPattern::reconstruct_impl): Likewise.
	(SlicePatternItems::reconstruct): New typed reconstruction method.
	(SlicePatternItems::reconstruct_impl): New pure virtual implementation.
	(SlicePatternItemsNoRest::reconstruct_impl): Implement reconstruction.
	(SlicePatternItemsHasRest::reconstruct_impl): Likewise.
	(SlicePattern::reconstruct_impl): Implement reconstruction.
	(AltPattern::reconstruct_impl): Likewise.
	* expand/rust-derive-cmp-common.cc (EnumMatchBuilder::tuple): Use
	reconstruct() to clone variant paths.
	(EnumMatchBuilder::strukt): Likewise.

Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
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.

1 participant