Skip to content

[SwiftParser] Move typed raw syntax nodes into SwiftParser - #3381

Open
rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:productize-raw-x-syntax
Open

[SwiftParser] Move typed raw syntax nodes into SwiftParser#3381
rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:productize-raw-x-syntax

Conversation

@rintaro

@rintaro rintaro commented Jul 13, 2026

Copy link
Copy Markdown
Member

The typed RawXXXSyntax nodes and RawSyntaxNodeProtocol are only used while parsing, so move them from SwiftSyntax into SwiftParser as internal types. SwiftSyntax keeps only the untyped RawSyntax, exposing what the parser needs via the existing @_spi(RawSyntax) boundary. Making the raw nodes internal narrows SwiftSyntax's public API and reduces compiled size, since the optimizer can drop the metadata and witness tables that public conformances emit and eliminate code no longer reachable across the module boundary.

validateLayout(layout:as:) has to stay in SwiftSyntax (it runs whenever a raw layout node is created), so instead of relying on the moved raw nodes it validates against the public syntax types: each type has an internal isKindOf(_ kind: SyntaxKind) static method (generated on every syntax node, base node, collection, and child-choice enum), and validation matches each child's RawSyntax.kind against the kinds allowed for the expected type. The validator is handed the concrete type's isKindOf, so this needs neither a SyntaxProtocol requirement nor any public/SPI surface for isKindOf.

Measured on a release build (swift build -c release), SwiftSyntax's object code shrinks from 5.53 MB to 4.53 MB (−978 KB, −18%) — including ~205 KB less __DATA, which holds protocol witness tables and type metadata. SwiftParser grows from 1.28 MB to 1.53 MB (+243 KB) as it absorbs the internal raw nodes, for a net −735 KB (−11.1%) across the two modules.

@rintaro
rintaro force-pushed the productize-raw-x-syntax branch 4 times, most recently from fef7c63 to d14adae Compare July 18, 2026 18:01
@rintaro

rintaro commented Jul 18, 2026

Copy link
Copy Markdown
Member Author

@swift-ci Please test

@rintaro
rintaro force-pushed the productize-raw-x-syntax branch from d14adae to f48659f Compare July 18, 2026 22:05
The typed `RawXXXSyntax` nodes and `RawSyntaxNodeProtocol` are only
used while parsing, so move them from SwiftSyntax into SwiftParser as
internal types. SwiftSyntax keeps only the untyped `RawSyntax`,
exposing the handful of members the parser needs through the
`@_spi(RawSyntax)` boundary.

Making the raw nodes internal narrows SwiftSyntax's public surface and
reduces compiled code size: the optimizer can drop the type metadata
and protocol witness tables that public conformances must emit, and
eliminate or specialize the raw node code that is no longer reachable
across the module boundary.

`validateLayout(layout:as:)` stays in SwiftSyntax and is still called
from `RawSyntax.layout()`, but it no longer depends on the typed raw
nodes. It matches each child's `RawSyntax.kind` against the kinds
allowed for the expected syntax type by calling that type's
`isKindOf(_ kind: SyntaxKind)`, an internal static method generated on
every syntax node, base node, collection, and child-choice enum. The
validator is handed the concrete type's `isKindOf`, so it is neither a
`SyntaxProtocol` requirement nor part of the public API.
@rintaro
rintaro force-pushed the productize-raw-x-syntax branch from f48659f to ff4b8f4 Compare July 28, 2026 18:31
@rintaro

rintaro commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

@swift-ci Please test

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