Skip to content

Sema: a generic type declared after the type that uses it is 'unknown type' #1440

Closed

Description

@ehartford

Summary

A generic type declared after a non-generic type that uses it is rejected by Sema with unknown type. Declaration order must not matter (it does not for non-generic types).

Repro (origin/main 1c76062)

type Holder { g: Gen[i32] }

fn main:
    let h = Holder { g: Gen { t: 5 } }
    print(f"{h.g.t}")

type Gen[T] { t: T }
error: unknown type: Gen
 --> c1.w:1:18
1 | type Holder { g: Gen[i32] }

Same for a generic enum (enum Holder: H(g: Gen[Inner]) | N above enum Gen[T]: Of(t: T) | Nothing). Moving Gen above Holder compiles and runs.

Where

src/Sema.w ~4239-4262 (resolve_type_expr, TY_GENERIC_INST branch): lookup_named_type_visible(gi_base_sym) misses because the generic template is not registered yet when the earlier declaration's field types are resolved, and type_decl_nodes does not contain it either, so the path ends in emit_error("unknown type: " ...). Generic templates need to be registered (or resolution deferred) before any declaration's member types are resolved.

Found while fixing #1430 (the #1430 fixtures declare their generic templates first for this reason).

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Verified fixed on main 55739d4 (release binary): Gen[T] declared after Holder prints 5.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions