From 94aa9d4a92a3b3eaefcf8433db859a84ee453b8f Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:13:05 +0000 Subject: [PATCH 1/2] fix(compiler): preserve templated scalar initializer types --- .../fix-templated-scalar-initializers.md | 7 + packages/compiler/src/core/checker.ts | 16 +- .../values/templated-scalar-values.test.ts | 155 ++++++++++++++++++ 3 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 .chronus/changes/fix-templated-scalar-initializers.md create mode 100644 packages/compiler/test/checker/values/templated-scalar-values.test.ts diff --git a/.chronus/changes/fix-templated-scalar-initializers.md b/.chronus/changes/fix-templated-scalar-initializers.md new file mode 100644 index 00000000000..d64419ae29b --- /dev/null +++ b/.chronus/changes/fix-templated-scalar-initializers.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Fix scalar initializers on template instantiations to preserve the instantiated scalar type and validate arguments against the instantiated parameter types. diff --git a/packages/compiler/src/core/checker.ts b/packages/compiler/src/core/checker.ts index a28bf5f9844..59ba35521a4 100644 --- a/packages/compiler/src/core/checker.ts +++ b/packages/compiler/src/core/checker.ts @@ -2363,7 +2363,7 @@ export function createChecker(program: Program, resolver: NameResolver): Checker ): FunctionParameter { const links = getSymbolLinks(node.symbol); - if (links.declaredType) { + if (links.declaredType && ctx.mapper === undefined) { return links.declaredType as FunctionParameter; } if ( @@ -2407,7 +2407,7 @@ export function createChecker(program: Program, resolver: NameResolver): Checker parameterType = createType({ ...base, mixed: false, - type: node.type ? getTypeForNode(node.type) : unknownType, + type: node.type ? getTypeForNode(node.type, ctx) : unknownType, implementation: node.symbol.value!, }); } @@ -4795,6 +4795,7 @@ export function createChecker(program: Program, resolver: NameResolver): Checker case "Model": case "Interface": case "Union": + case "Scalar": if (isTemplateInstance(type)) { // this is an alias for some instantiation, so late-bind the instantiation lateBindMemberContainer(type); @@ -6559,6 +6560,14 @@ export function createChecker(program: Program, resolver: NameResolver): Checker type.symbol = createSymbol(type.node, type.name, SymbolFlags.Model | SymbolFlags.LateBound); mutate(type.symbol).type = type; break; + case "Scalar": + type.symbol = createSymbol( + type.node, + type.name, + SymbolFlags.Scalar | SymbolFlags.LateBound, + ); + mutate(type.symbol).type = type; + break; case "Interface": type.symbol = createSymbol( type.node, @@ -7547,6 +7556,9 @@ export function createChecker(program: Program, resolver: NameResolver): Checker stdTypes[type.name as any as keyof StdTypes] = type as any; } + lateBindMemberContainer(type); + lateBindMembers(type); + return finishType(type, { skipDecorators: ctx.hasFlags(CheckFlags.InTemplateDeclaration) }); } diff --git a/packages/compiler/test/checker/values/templated-scalar-values.test.ts b/packages/compiler/test/checker/values/templated-scalar-values.test.ts new file mode 100644 index 00000000000..5bbca3384a6 --- /dev/null +++ b/packages/compiler/test/checker/values/templated-scalar-values.test.ts @@ -0,0 +1,155 @@ +import { expect, it } from "vitest"; +import { expectDiagnostics, t } from "../../../src/testing/index.js"; +import { Tester } from "../../tester.js"; + +it("assigns a value constructed through an alias to an instantiated scalar", async () => { + await Tester.compile(` + alias TT = Test; + const namespaceRef: TT = TT.from("a"); + scalar Test { init from(a: string); } + `); +}); + +it.each([false, true])( + "constructs an aliased scalar instantiation (declaration first: %s)", + async (declarationFirst) => { + const declaration = "scalar Test { init from(a: string); }"; + const { TT, namespaceRef } = await Tester.compile(t.code` + ${declarationFirst ? declaration : ""} + alias ${t.scalar("TT")} = Test; + const ${t.scalarValue("namespaceRef")}: TT = TT.from("a"); + ${declarationFirst ? "" : declaration} + `); + + expect(namespaceRef.scalar).toBe(TT); + expect(namespaceRef.type).toBe(TT); + expect(TT.constructors.get("from")!.scalar).toBe(TT); + }, +); + +it.each(["string", "int32"] as const)( + "keeps constructor values tied to their instantiation when %s is used first", + async (first) => { + const second = first === "string" ? "int32" : "string"; + const { First, Second, a, b, again } = await Tester.compile(t.code` + scalar Test { init from(a: string); } + alias ${t.scalar("First")} = Test<${first}>; + const ${t.scalarValue("a")} = First.from("a"); + alias ${t.scalar("Second")} = Test<${second}>; + const ${t.scalarValue("b")} = Second.from("b"); + const ${t.scalarValue("again")} = First.from("c"); + `); + + expect(First).not.toBe(Second); + for (const value of [a, again]) { + expect(value.scalar).toBe(First); + expect(value.type).toBe(First); + } + expect(b.scalar).toBe(Second); + expect(b.type).toBe(Second); + }, +); + +it.each(["string", "int32"] as const)( + "instantiates initializer parameters when %s is used first", + async (first) => { + const second = first === "string" ? "int32" : "string"; + const firstArg = first === "string" ? '"a"' : "123"; + const secondArg = second === "string" ? '"b"' : "456"; + const { First, Second, a, b, again, program } = await Tester.compile(t.code` + scalar Test { init from(a: T); } + alias ${t.scalar("First")} = Test<${first}>; + const ${t.scalarValue("a")}: First = First.from(${firstArg}); + alias ${t.scalar("Second")} = Test<${second}>; + const ${t.scalarValue("b")}: Second = Second.from(${secondArg}); + const ${t.scalarValue("again")}: First = First.from(${firstArg}); + `); + + expect(First.constructors.get("from")!.parameters[0].type).toBe( + program.checker.getStdType(first), + ); + expect(Second.constructors.get("from")!.parameters[0].type).toBe( + program.checker.getStdType(second), + ); + expect(a.scalar).toBe(First); + expect(a.type).toBe(First); + expect(b.scalar).toBe(Second); + expect(b.type).toBe(Second); + expect(again.scalar).toBe(First); + expect(again.type).toBe(First); + }, +); + +it("constructs a scalar directly using its default template argument", async () => { + const { Instance, value } = await Tester.compile(t.code` + scalar Test { init from(a: T); } + alias ${t.scalar("Instance")} = Test; + const ${t.scalarValue("value")} = Test.from("a"); + `); + + expect(value.scalar).toBe(Instance); + expect(value.type).toBe(Instance); +}); + +it("preserves the instantiation through scalar and constructor aliases", async () => { + const { Instance, value } = await Tester.compile(t.code` + scalar Test { init from(a: T); } + alias ${t.scalar("Instance")} = Test; + alias Indirect = Instance; + alias From = Indirect.from; + const ${t.scalarValue("value")} = From("a"); + `); + + expect(value.scalar).toBe(Instance); + expect(value.type).toBe(Instance); +}); + +it.each([ + "scalar Derived extends Base; alias Instance = Derived;", + "scalar Derived extends Base; alias Instance = Derived;", +])("constructs the derived scalar with an inherited initializer: %s", async (derived) => { + const { Expected, value, program } = await Tester.compile(t.code` + scalar Base { init from(a: T); } + ${derived} + alias ${t.scalar("Expected")} = Instance; + const ${t.scalarValue("value")}: Instance = Instance.from("a"); + `); + + expect(value.scalar).toBe(Expected); + expect(value.type).toBe(Expected); + expect(Expected.constructors.get("from")!.scalar).toBe(Expected); + expect(Expected.constructors.get("from")!.parameters[0].type).toBe( + program.checker.getStdType("string"), + ); +}); + +it.each([ + ["Test", "123", "string"], + ["Test", '"a"', "int32"], + ["Derived", "123", "string"], +])("rejects an invalid argument to %s", async (instance, argument, parameterType) => { + const diagnostics = await Tester.diagnose(` + scalar Test { init from(a: T); } + scalar Derived extends Test; + alias Strings = Test; + alias Numbers = Test; + alias Instance = ${instance}; + const value = Instance.from(${argument}); + `); + + expectDiagnostics(diagnostics, { + code: "invalid-argument", + message: `Argument of type '${argument}' is not assignable to parameter of type '${parameterType}'`, + }); +}); + +it.each(["Test", "Alias"])("preserves non-generic constructor identity through %s", async (ref) => { + const { Test, value } = await Tester.compile(t.code` + scalar ${t.scalar("Test")} { init from(a: string); } + alias Alias = Test; + const ${t.scalarValue("value")}: Test = ${ref}.from("a"); + `); + + expect(value.scalar).toBe(Test); + expect(value.type).toBe(Test); +}); From c1b12d787e60457134a51c9a3bf91be64e9592db Mon Sep 17 00:00:00 2001 From: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:19:18 +0000 Subject: [PATCH 2/2] fix(compiler): preserve warnings for late-bound scalar initializers Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com> --- packages/compiler/src/core/checker.ts | 6 +++++- .../values/templated-scalar-values.test.ts | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/compiler/src/core/checker.ts b/packages/compiler/src/core/checker.ts index 7f76fd24059..a0ec2a3cfd7 100644 --- a/packages/compiler/src/core/checker.ts +++ b/packages/compiler/src/core/checker.ts @@ -1854,7 +1854,11 @@ export function createChecker(program: Program, resolver: NameResolver): Checker if (sym.flags & SymbolFlags.LateBound) { compilerAssert(sym.type, `Expected late bound symbol to have type`); - return sym.type; + if (sym.type.kind !== "ScalarConstructor") { + return sym.type; + } + // Scalar constructors must also pass through the deprecation check below. + baseType = sym.type; } else if (sym.flags & SymbolFlags.TemplateParameter) { compilerAssert(symNode, "Expected template parameter symbol to have a node"); const mapped = checkTemplateParameterDeclaration( diff --git a/packages/compiler/test/checker/values/templated-scalar-values.test.ts b/packages/compiler/test/checker/values/templated-scalar-values.test.ts index 5bbca3384a6..9ffced266b7 100644 --- a/packages/compiler/test/checker/values/templated-scalar-values.test.ts +++ b/packages/compiler/test/checker/values/templated-scalar-values.test.ts @@ -153,3 +153,18 @@ it.each(["Test", "Alias"])("preserves non-generic constructor identity through % expect(value.scalar).toBe(Test); expect(value.type).toBe(Test); }); + +it("preserves deprecation diagnostics for late-bound templated constructors", async () => { + const diagnostics = await Tester.diagnose(` + scalar Test { + #deprecated "Use a different initializer" + init from(a: T); + } + alias Instance = Test; + const value: Instance = Instance.from("a"); + `); + + expectDiagnostics(diagnostics, [ + { code: "deprecated", severity: "warning", message: "Deprecated: Use a different initializer" }, + ]); +});