Skip to content

resolve default template parameters from parent containers #6938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .chronus/changes/fix-templ-param-alias-is-2025-3-9-13-34-54.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Fixes template argument resolution when a default template parameter value is resolved by a parent container (e.g. interface)
For example:
```tsp
interface Resource<T> {
read<U = T>(): U;
}

model Foo {
type: "foo";
}

alias FooResource = Resource<Foo>;

op readFoo is FooResource.read;
```
The `returnType` for `readFoo` would be model `Foo`. Previously the `returnType` resolved to a `TemplateParameter`.
12 changes: 8 additions & 4 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
args: readonly TemplateArgumentNode[],
decls: readonly TemplateParameterDeclarationNode[],
mapper: TypeMapper | undefined,
parentMapper?: TypeMapper,
): Map<TemplateParameter, Type | Value | IndeterminateEntity> {
const params = new Map<string, TemplateParameter>();
const positional: TemplateParameter[] = [];
Expand Down Expand Up @@ -1243,7 +1244,12 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
}

if (init === null) {
const argumentMapper = createTypeMapper(mapperParams, mapperArgs, { node, mapper });
const argumentMapper = createTypeMapper(
mapperParams,
mapperArgs,
{ node, mapper },
parentMapper,
);
const defaultValue = getResolvedTypeParameterDefault(param, decl, argumentMapper);
if (defaultValue) {
commit(param, defaultValue);
Expand Down Expand Up @@ -1425,6 +1431,7 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
argumentNodes,
templateParameters,
mapper,
declaredType.templateMapper,
);

baseType = getOrInstantiateTemplate(
Expand Down Expand Up @@ -4316,9 +4323,6 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
type.name,
SymbolFlags.Interface | SymbolFlags.LateBound,
);
if (isTemplateInstance(type) && type.name === "Foo") {
getSymbolLinks(type.symbol);
}
mutate(type.symbol).type = type;
break;
case "Union":
Expand Down
58 changes: 57 additions & 1 deletion packages/compiler/test/checker/templates.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deepStrictEqual, fail, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { getSourceLocation } from "../../src/core/diagnostics.js";
import { Diagnostic, Model, StringLiteral, Type } from "../../src/core/types.js";
import { Diagnostic, Model, Operation, StringLiteral, Type } from "../../src/core/types.js";
import { isUnknownType } from "../../src/index.js";
import {
BasicTestRunner,
Expand Down Expand Up @@ -400,6 +400,62 @@ describe("compiler: templates", () => {
strictEqual(t.value, "bye");
});

it("can reference parent parameters in default", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
@test interface A<T> {
op foo<R = T, P = R>(params: P): R;
}
alias B = A<string>;
@test op MyOp is B.foo;
`,
);
const { MyOp } = (await testHost.compile("main.tsp")) as { MyOp: Operation };
const params = MyOp.parameters.properties.get("params");
ok(params, "Expected params to be defined");
strictEqual(params.type.kind, "Scalar");
strictEqual(params.type.name, "string");
strictEqual(MyOp.returnType.kind, "Scalar");
strictEqual(MyOp.returnType.name, "string");
});

it("can use parent parameters default in default", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
@test interface MyInterface<A, B = string> {
op foo<R = B, P = R>(params: P): R;
}
alias AliasedInterface = MyInterface<string>;
@test op MyOp is AliasedInterface.foo;
`,
);
const { MyOp } = (await testHost.compile("main.tsp")) as { MyOp: Operation };
const params = MyOp.parameters.properties.get("params");
ok(params, "Expected params to be defined");
strictEqual(params.type.kind, "Scalar");
strictEqual(params.type.name, "string");
strictEqual(MyOp.returnType.kind, "Scalar");
strictEqual(MyOp.returnType.name, "string");
});

it("can override default provided by parent parameters", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
@test interface A<T> {
op foo<U = T>(): U;
}
alias B = A<string>;
@test op MyOp is B.foo<bytes>;
`,
);
const { MyOp } = (await testHost.compile("main.tsp")) as { MyOp: Operation };
strictEqual(MyOp.returnType.kind, "Scalar");
strictEqual(MyOp.returnType.name, "bytes");
});

it("emit diagnostics if referencing itself", async () => {
testHost.addTypeSpecFile(
"main.tsp",
Expand Down
Loading