ast: Implement reconstruct() logic for Pattern nodes#4431
Draft
Pasta-coder wants to merge 1 commit intoRust-GCC:masterfrom
Draft
ast: Implement reconstruct() logic for Pattern nodes#4431Pasta-coder wants to merge 1 commit intoRust-GCC:masterfrom
Pasta-coder wants to merge 1 commit intoRust-GCC:masterfrom
Conversation
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>
7b2721e to
745dab0
Compare
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 implements the
reconstruct()pattern for thePatternAST 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
derivemacros), where patterns must be duplicated without sharing NodeIDs to avoid resolution collisions.Key changes include:
reconstruct_implfor allPatternsubclasses.reconstructsupport to helper classes (RangePatternBound,StructPatternField,PatternItems).reconstruct()wrappers for item families (TupleStructItems,TuplePatternItems,SlicePatternItems) to ensure type safety and avoid manual downcasting.PathInExpression::reconstruct()for struct patterns.derive(PartialEq)expansion logic to usereconstruct()instead of rebuilding paths manually.
gcc/rust/ChangeLog: