8000 semantic::Type is a flat, ad-hoc variant instead of a well-defined type grammar · Issue #342 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

semantic::Type is a flat, ad-hoc variant instead of a well-defined type grammar #342

Open

Description

@akoskovacs

Summary

semantic::Type (src/semantic/types.hxx) represents every type as one flat 15-way std::variant (Type::kind, types.hxx:110-127) that mixes several unrelated concerns with no grouping or stated relationships between them. Individually each case is reasonable; together they show the representation growing one struct at a time per checker feature rather than from a designed grammar.

Primitive scalars are split across two unrelated representations

PrimitiveType covers Integer/Char/String/Bool/Atom/Unit (types.hxx:15-27), while the explicit fixed-width numerics are a completely separate pair of structs, SizedIntType/SizedFloatType (types.hxx:29-36) — a different variant alternative for "a number," with nothing in the file saying whether SizedIntType{64, true} (Int, per the comment at types.hxx:157) is meant to relate to PrimitiveType::Integer at all.

A stale comment disagreeing with the code it documents

PrimitiveType's comment explains String had to become its own Kind rather than [Char], because modeling it as [Char] made the two indistinguishable to the checker (types.hxx:16-18). But the doc comment on Type::string() right below it still says:

static auto string() -> TypePtr;   // alias for list(charT())      // types.hxx:131

— which is exactly the representation the comment two lines above says was abandoned for a real bug. The implementation (types.cxx:15-17) actually constructs PrimitiveType{PrimitiveType::String} directly, so the code is right and the comment is simply wrong; harmless here, but a symptom of the same variant not having one place that describes what it means.

Nominal and structural types are flat siblings with no marker

NamedType/RecordType (identified by declaration) and TupleType/ListType/MapType/OptionalType/UnionType/IntersectionType (identified by shape) all sit as equal variant alternatives with nothing distinguishing the two categories. Code that needs that distinction — module-qualified identity comparison, namedTypesMatch (types.hxx:205-212) — has to pattern-match the variant and know which cases are which, rather than being able to ask the type a question.

Inference placeholders live in the same tree as resolved types

TypeVar, UnknownType, and ConstrainedType (types.hxx:80-107) are checker bookkeeping — "not yet resolved" or "any type satisfying this trait" — stored as ordinary alternatives beside fully-resolved runtime types. A TypePtr can be either, and callers must remember to call isFullyConcrete (types.hxx:222) before treating one as an actual, determined type; nothing in the representation itself enforces that a "real" type can't secretly be a placeholder.

ConstrainedType's own comment goes further — the constraint it represents "is consulted via TraitRegistry::satisfies, not stored structurally on the Type" (types.hxx:96-99), so the tree doesn't even fully describe what a ConstrainedType means without reaching into a separate registry.

Suggested next step

Before extending the checker further for 0.4, write down the intended type grammar — what separates nominal from structural types, how sized numerics relate to the primitive ones, where inference-only placeholders may legally appear — and restructure Type to reflect it, instead of continuing to add flat variant cases as new checker features need them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0