Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TypeName Validation and Lowering

Status: implemented for every current TypeName variant, including string and integer literal types, parameter references, applications, and callable sequences.

TypeName records semantic type-reference structure. It does not describe a shared target grammar. One selected language adapter must either lower the complete value into structured output or reject it before any source text is rendered.

This chapter defines the 0.7 type-name seam. The frozen pre-0.6.8 presentation configuration is documented only in 0.6.8 Legacy Compatibility and Migration.

Ownership

The core owns:

  • the language-independent TypeName variants and their intrinsic coherence;
  • recursive discovery of every CodeNode::TypeRef;
  • the order of source-tree rewrite, type-name lowering, import collection, alias resolution, and final rendering;
  • validation of every adapter-produced type block; and
  • all-or-error behavior when any type name is invalid or unsupported.

The selected language adapter owns:

  • whether the complete type name is representable;
  • target precedence, punctuation, delimiters, and wrapping;
  • the spelling and placement of every accepted type construct;
  • decoded string-literal quoting and escaping; and
  • target-derived type references such as Python’s typing.Literal.

No TypeExpressionCapability, presentation matrix, or universal syntax configuration sits between those responsibilities. Detailed type grammar varies together and remains local to one adapter.

Legacy Generic / Function and modern Application / Callable inputs share semantic validation and traversal; they do not create a second preparation pipeline. Every adapter must preserve the supplied structure or return SigilStitchError. An unsupported expansion, kind, label, or presence rule is not silently erased, and the library performs no type-level evaluation, argument inference, or pack-length solving.

Generic declaration lowerers consume borrowed GenericParamView values from the owner’s single ordered binding store. Kind expressions remain structural until that declaration’s language-owned lowering. Haskell callers supply required extensions themselves through file headers or compiler flags; sigil-stitch does not infer pragmas from type expressions.

One Fallible Interface

RendererLang exposes one complete type-name lowering method:

fn lower_type_name(
    &self,
    type_name: &TypeName,
) -> Result<CodeBlock, SigilStitchError>;

There is no public validated wrapper. TypeName is immutable, lowering is a pure operation, and the successful non-empty CodeBlock is the proof that the selected adapter accepted the value. Crate-owned callers perform intrinsic validation before the hook and validate its output afterward.

Validation paths may invoke this pure lowering operation for every type root and discard the successful blocks while retaining all independent failures. Render preparation retains successful blocks for import collection and rendering. This avoids separate validation and lowering implementations that can disagree.

The method belongs to RendererLang, not only CodeLang, because a direct CodeBlock may contain %T references without using declaration specs.

Preparation Pipeline

Declaration lowering first produces source CodeBlocks. The selected adapter then rewrites each source block exactly once before type names are lowered and imports are collected:

declaration lowerers and caller CodeBlocks
    -> CodeBlock tree containing TypeRef(TypeName) and optional FloatValue nodes
    -> RendererLang::rewrite_nodes exactly once
    -> validate the rewritten source tree
    -> recursively prepare source nodes
       FloatValue -> lower and validate expression -> prepare introduced types
       TypeRef -> intrinsic validation -> lower_type_name -> validate type block
    -> collect imports from the lowered CodeBlock tree
    -> resolve aliases
    -> render through one layout adapter with no further rewrite or lowering

No source text is emitted until every type name in every prepared file member has succeeded. A failure in a direct, nested, or sequenced block aborts the complete file just like a declaration-lowering failure.

Declaration lowerers continue to place semantic TypeName values in %T slots. They do not call lower_type_name, render a type early, or duplicate type grammar.

rewrite_nodes() is a source-tree correction seam, not a type-lowering hook. It runs for headers, direct caller blocks, and declaration-lowered blocks. It does not run for blocks returned by lower_type_name(), opaque raw content, or the type metadata attached to RawContentWithImports. Floating-value lowering results are likewise completed target expressions, not source-rewrite inputs; their introduced TypeName references still follow ordinary lowering. Listed raw-import types are lowered and validated only so their derived imports can be collected.

Lowered Block Contract

A successful adapter result must:

  • be non-empty;
  • contain only structure meaningful inside one type expression;
  • balance every Indent and Dedent marker;
  • preserve soft layout choices as SoftBreak and nested groups;
  • retain import-bearing leaves as terminal TypeRef nodes; and
  • contain no unresolved compound TypeName.

The terminal type-reference leaves are target-aware Primitive and Raw values plus unqualified Importable references whose aliases are resolved later. A qualified importable reference and every compound variant must be fully lowered by the adapter.

The crate rejects adapter output that contains statement or control-flow nodes or deferred floating values, is empty, leaves a compound type reference unresolved, or otherwise cannot be interpreted as one type expression. This catches incomplete external adapters instead of allowing recursive or silently empty output.

CodeBlock remains the only shared structured source container. It carries target-associated type-expression structure rather than defining a portable cross-language program. There is no separate type document tree and no public BoxDoc-producing language hook.

Python Declaration and Callable Layout

Python declaration lowerers keep class bases, alias type parameters, function parameters, callable parameters, and nested type arguments as structured CodeBlock layouts. They do not flatten comma-separated type material into fixed strings. Broken groups receive one continuation indentation unit; flat groups do not acquire indentation. Empty parameter lists use literal empty delimiters rather than an empty Layout node, because lowered-block validation rejects empty layout nodes.

Python builder defaults distinguish grammar roles: an unannotated parameter renders value=1, while an annotated parameter renders value: int = 1. This distinction is owned by the Python function lowerer; ordinary assignments and field initializers retain assignment spacing.

Imports Stay Structural

An adapter expresses a target-derived import by retaining an importable TypeName leaf in its lowered block. It does not return a parallel import list.

For example, Python lowers a string literal type to the structural equivalent of:

%T[%S]

where %T contains TypeName::importable("typing", "Literal") and %S contains the decoded string value. Import collection therefore discovers typing.Literal from the same structure that renders Literal["value"]. Alias resolution cannot drift from the generated type syntax.

Direct CodeRenderer use retains its existing contract: the caller supplies the resolved ImportGroup. FileSpec owns complete source preparation, derived import collection, and import-header emission.

String Literal Types

The focused 0.7 extension is:

TypeName::StringLiteral(String)

The string is the decoded semantic value. It contains neither target quotes nor target escape sequences. The adapter uses its language-local string literal rules when that value is valid in a type position.

  • TypeScript lowers one value to a string literal type such as 'active'.
  • Python lowers one value through typing.Literal["active"]. A non-empty direct Union containing only StringLiteral members becomes one typing.Literal[...], preserving member order and duplicates. Mixed unions and nested unions lower recursively through ordinary Python union grammar; this direct-union rule never flattens them.
  • A built-in adapter without an exact string singleton type rejects the variant instead of widening it to String, str, or another primitive.

Several values compose through ordinary union structure:

TypeName::Union([
    TypeName::string_literal("active"),
    TypeName::string_literal("inactive"),
])

The core does not add LiteralValue, StringEnum, or LiteralSet, and does not reinterpret the decoded string payload as source code.

Integer Literal Types

TypeName::IntegerLiteral(String) and TypeName::integer_literal(...) store one exact integer as canonical ASCII decimal text, without a machine-word bound. The constructor and derived Deserialize only store their payload. Intrinsic validation accepts 0 or -?[1-9][0-9]* and rejects all other spellings with InvalidTypeName, including -0, plus signs and leading zeros. There is no normalizing constructor, separate public value type or custom Serde logic.

  • TypeScript lowers to a number singleton only when a finite binary64 number preserves the integer exactly. Its lowerer compares the original canonical text with the full fixed-point decimal integer represented by that number; successful float parsing or shortest-round-trip formatting is not sufficient. 2^53 is exact, 2^53 + 1 is not, and larger powers of two may still be exact.
  • Python lowers to typing.Literal[200], retaining an importable Literal leaf plus unquoted integer text. Integer and mixed literal unions follow ordinary recursive union lowering; direct string-only unions retain their existing rule.
  • Scala lowers values in -2147483648..=2147483647 to unsuffixed Int singletons. Other values in -9223372036854775808..=9223372036854775807 use Long singleton syntax with an uppercase L, such as 2147483648L. Values outside that native range are rejected. This target-local range check does not bound the shared integer domain or add a carrier-selection interface. Literal grammar follows Scala’s integer syntax and literal-based singleton types.
  • Other built-in adapters and the frozen compatibility lowerer reject the form.

This support describes each adapter’s current generated-source profile:

Built-in targetsInteger singleton representation
TypeScriptExact ordinary number literal
PythonStandard typing.Literal annotation
ScalaNative Int or Long literal
Bash, C, C++, C#, Dart, Go, Haskell, Java, JavaScript, Kotlin, Lua, OCaml, PHP, Ruby, Rust, Swift, ZshUnsupported in the current adapter profile

Kotlin’s compiler-internal integer literal types are not source-denotable. Haskell’s type-level naturals and C++/Rust/Swift constant arguments do not denote types inhabited by the ordinary integer itself. Integer literal facilities in RBS, PHPStan/Psalm PHPDoc, LuaLS annotations, or Flow belong to separate typing profiles; the Ruby, PHP, Lua, and JavaScript adapters do not emit those profiles. Their existence is not a reason to put that syntax in native type slots or to silently discard singleton intent.

A target rejection uses UnsupportedTypeName. No target may round, truncate, widen to a primitive or substitute a BigInt singleton. Allocation and target resource limits still apply; the shared mathematical domain is not an unlimited input-resource guarantee. The derived JSON payload is a string, not a numeric token. It adds no cross-version wire/binary compatibility promise.

Compatibility

Adding StringLiteral makes the pre-0.6.8 public TypeName enum source incompatible for downstream exhaustive matches. The 0.7 change therefore also marks TypeName as #[non_exhaustive] and documents the required wildcard match. The compatibility bridge preserves supported 0.6.8 Rust constructors and captures the specific TypeName JSON values documented before 0.7 as checked fixtures. This is not a promise that every Serde representation remains stable: sigil-stitch defines no binary serialization protocol, enum-ordinal contract, struct-field order, or serializer-specific byte format. No forward-compatible interpretation of unknown serialized variants is added; deserialization returns an error instead of changing generated code silently.

RendererLang::lower_type_name has a provided compatibility implementation. It reproduces pre-0.6.8 behavior for old variants through the frozen TypePresentationConfig, TypePresentation, GenericSyntaxConfig, and qualified-name accessors. It rejects StringLiteral and every later semantic variant that did not exist in 0.6.8.

Every built-in adapter overrides the complete method and does not consult the compatibility configuration. The legacy accessors and data types are deprecated, receive no new fields or variants, and remain referenced only by the compatibility implementation and its tests.

The pre-0.6.8 TypeName::to_doc_with_lang() convenience remains as a deprecated terminal compatibility facade. The current file and standalone rendering pipelines do not call it, and no replacement BoxDoc-producing language hook is introduced.

Verification Contract

The implementation must prove:

  • every language in tests/renderer_parity_tests.rs handles every old TypeName variant through its local lowerer or rejects it explicitly;
  • direct and pretty paths agree at wide widths and preserve intentional soft breaks at narrow widths;
  • unsupported compound forms fail in direct, nested, and Sequence blocks;
  • lowered imports survive nesting and alias collisions;
  • TypeScript and Python correctly handle empty strings, both quote characters, backslashes, newlines, NUL, and Unicode;
  • Python union lowering retains the canonical Literal import;
  • a compatibility adapter that implements only pre-0.6.8 methods preserves its old output and rejects StringLiteral and IntegerLiteral;
  • canonical integer validation is identical for constructors, direct variants and decoded data, and TypeScript accepts exact values above its safe-integer range while rejecting rounded and overflowing values;
  • Scala preserves signed Int/Long boundaries and rejects values outside its native integer-literal range on both renderer paths;
  • empty, statement-bearing, or unresolved adapter output fails closed; and
  • old serialized TypeName fixtures retain their exact shapes.