Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Fix: Issue with templated operations in templated interface would get cached only by keying on the operation template args.",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
21 changes: 7 additions & 14 deletions packages/compiler/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,18 +1146,13 @@ export function createChecker(program: Program): Checker {
);
}
}
const cached = symbolLinks.instantiations?.get(args);
const mapper = createTypeMapper(params, args, parentMapper);
const cached = symbolLinks.instantiations?.get(mapper.args);
if (cached) {
return cached;
}
if (instantiateTempalates) {
return instantiateTemplate(
symbolLinks.instantiations,
templateNode,
params,
args,
parentMapper
);
return instantiateTemplate(symbolLinks.instantiations, templateNode, params, mapper);
} else {
return errorType;
}
Expand All @@ -1175,13 +1170,11 @@ export function createChecker(program: Program): Checker {
instantiations: TypeInstantiationMap,
templateNode: TemplateableNode,
params: TemplateParameter[],
args: Type[],
parentMapper: TypeMapper | undefined
mapper: TypeMapper
): Type {
const mapper = createTypeMapper(params, args, parentMapper);
const type = getTypeForNode(templateNode, mapper);
if (!instantiations.get(args)) {
instantiations.set(args, type);
if (!instantiations.get(mapper.args)) {
instantiations.set(mapper.args, type);
}
if (type.kind === "Model") {
type.templateNode = templateNode;
Expand Down Expand Up @@ -5041,7 +5034,7 @@ function createTypeMapper(

return {
partial: false,
args,
args: [...(parentMapper?.args ?? []), ...args],
getMappedType: (type: TemplateParameter) => {
return map.get(type) ?? type;
},
Expand Down
25 changes: 24 additions & 1 deletion packages/compiler/test/checker/interface.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepStrictEqual, ok, strictEqual } from "assert";
import { deepStrictEqual, notStrictEqual, ok, strictEqual } from "assert";
import { isTemplateDeclaration } from "../../core/type-utils.js";
import { Interface, Model, Operation, Type } from "../../core/types.js";
import {
Expand Down Expand Up @@ -368,6 +368,29 @@ describe("compiler: interfaces", () => {
strictEqual(a.type, b.type);
});

it("templated interface with different args but templated operations with the same arg shouldn't be the same", async () => {
const { Index } = (await runner.compile(`
@test interface Foo<A> {
@test bar<B>(input: A): B;
}

alias MyFoo8 = Foo<int8>;
alias MyFoo16 = Foo<int16>;
@test model Index {
a: MyFoo8.bar<string>;
b: MyFoo16.bar<string>;
}
`)) as {
Index: Model;
};
const a = Index.properties.get("a");
const b = Index.properties.get("b");
ok(a);
ok(b);

notStrictEqual(a.type, b.type);
});

it("can extend an interface with templated operations", async () => {
const { Foo, myBar: bar } = (await runner.compile(`
interface Base<A> {
Expand Down