Skip to content

dslx: parse semantic sum declarations and constructors#4457

Open
dank-openai wants to merge 1 commit into
google:mainfrom
xlsynth:dank/upstream/semantic-sum-frontend
Open

dslx: parse semantic sum declarations and constructors#4457
dank-openai wants to merge 1 commit into
google:mainfrom
xlsynth:dank/upstream/semantic-sum-frontend

Conversation

@dank-openai

@dank-openai dank-openai commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add DSLX frontend support for semantic-sum enum declarations with unit, tuple, and struct payload variants.
  • Add semantic-sum constructor expressions and match payload patterns so the AST preserves the source payload shape.
  • Teach formatting, cloning, LSP symbols, type-info plumbing, conversion scaffolding, and diagnostics about the new AST nodes while leaving full typechecking and lowering semantics for follow-up work.
  • Preserve existing numeric-enum parsing behavior, including explicitly typed empty enums.

Example

enum Option<T: type> {
  None,
  Some(T),
}

This PR makes declarations and constructors like Option::Some(x) parse and format as semantic-sum syntax. Later changes can build typechecking and conversion semantics on top of these AST nodes.

Validation

Validation ran on Ubuntu 24.04 at commit 1f8ad4299fa61d5d35d2bbd4fe4780f79314f2d8:

  • bazel test //xls/dslx/fmt:ast_fmt_test //xls/dslx/frontend:parser_test //xls/dslx/frontend:ast_cloner_test //xls/dslx/frontend:scanner_test //xls/dslx:dslx_fmt_test //xls/dslx:constexpr_evaluator_test //xls/dslx/bytecode:bytecode_emitter_test //xls/dslx/exhaustiveness:exhaustiveness_match_test //xls/dslx/lsp:document_symbols_test //xls/dslx/translators:dslx_to_verilog_test //xls/fuzzer:value_generator_test //xls/dslx/ir_convert:ir_converter_test //xls/dslx/ir_convert:ir_converter_legacy_test //xls/dslx/tests/errors:error_modules_test //xls/dslx/run_routines:run_routines_test //xls/dslx/type_system:type_info_to_proto_test //xls/dslx/type_system_v2:typecheck_module_v2_enum_test --test_output=errors --keep_going --test_timeout=900 --cache_test_results=no

Misc.

This is PR 1/7 for phase 1 implementation of sum types.

@dank-openai dank-openai force-pushed the dank/upstream/semantic-sum-frontend branch from 19ce53c to 1f8ad42 Compare June 23, 2026 22:02
@dank-openai dank-openai requested a review from richmckeever June 23, 2026 23:28
@dank-openai dank-openai marked this pull request as ready for review June 23, 2026 23:28
dank-openai added a commit to xlsynth/xlsynth that referenced this pull request Jun 23, 2026
Adds Phase 1 semantic sum value representation and encoding support, including diagnostics and tests for sum type mismatch formatting.
dank-openai added a commit to xlsynth/xlsynth that referenced this pull request Jun 23, 2026
…uctors

Adds frontend parsing, formatting, visitors, and diagnostics for semantic sum syntax while preserving typed empty numeric enum parsing until semantic-sum runtime/type support lands higher in the stack.
Adds frontend parsing, formatting, visitors, and diagnostics for semantic sum syntax while preserving typed empty numeric enum parsing until semantic-sum runtime/type support lands higher in the stack.

branch:dank/upstream/semantic-sum-frontend
@dank-openai dank-openai force-pushed the dank/upstream/semantic-sum-frontend branch from 1f8ad42 to cd405f4 Compare June 23, 2026 23:45
Comment thread xls/dslx/frontend/ast.h
PayloadShape payload_shape,
std::vector<TypeAnnotation*> tuple_members,
std::vector<StructMemberNode*> struct_members,
Expr* discriminant = nullptr,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The usual convention in here is to use std::optional<T*> to indicate a nullable T* and force users of it to check if it has a value.

goto done;
}

if (IsLocalSumConstructorRef(lhs, outer_bindings)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we make the condition for this more generic or defer the check to sometime later than parsing? Resolving references generally is not something the parser is well equipped for, and the direction is to be less equipped for it.

fn f(x: Option) -> Option {
match x {
Option::EmptyTuple() => Option::EmptyTuple(),
Option::EmptyStruct { } => Option::EmptyStruct {},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems like it would be less surprising to have no space between the empty braces on both sides here (and in the declaration of the enum).

Comment thread xls/dslx/frontend/ast.h
// `payload_shape()` is the source of truth for tuple-vs-struct spelling. Empty
// child vectors are valid for both `Case()` and `Case { }`, so callers must
// not recover the shape from vector emptiness alone.
class SumVariantPayloadPattern : public AstNode {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few observations about this:

  • This class itself is an ad hoc sum type in C++(one for tuple + one for struct). It's an element in what is already a more standard sum type, NameDefTree::Leaf.
  • It would be nice to foresee how we'd use struct patterns with regular structs. Having a struct pattern thing that is not married to sum types would solve that.
  • This is actually a subtree, and breaks the concept of what is a "leaf" in NameDefTree. I've always thought NameDefTree was unintuitive anyway.

Maybe we should do away with the NameDefTree class and just define PatternTree = std::variant<TuplePattern*, StructPattern*, NameDef*, NameRef*, etc. from what is now Leaf>. We replace NameDefTree uses with PatternTree. We split your proposed SumVariantPayloadPattern into TuplePattern and StructPattern. TuplePattern's constructor ref is nullable. StructPattern's can later be a ColonRef | TRTA, and we can use it for regular structs then.

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