Skip to content

Commit

Permalink
Template instantiated with ErrorType will get the arg changed to unkn…
Browse files Browse the repository at this point in the history
…own (#2874)

fix #2722

---------

Co-authored-by: Mark Cowlishaw <markcowl@microsoft.com>
  • Loading branch information
timotheeguerin and markcowl committed Feb 5, 2024
1 parent 1d6d1ce commit cc2723a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-starfishes-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typespec/compiler": patch
---

Template instantiated with ErrorType will get the arg changed to unknown
6 changes: 5 additions & 1 deletion packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,11 +1090,15 @@ export function createChecker(program: Program): Checker {
commit(param, effectiveType);
continue;
}
} else if (isErrorType(type)) {
// If we got an error type we don't want to keep passing it through so we reduce to unknown
// Similar to the above where if the type is not assignable to the constraint we reduce to the constraint
commit(param, unknownType);
continue;
}

commit(param, type);
}

return finalMap;
}

Expand Down
27 changes: 26 additions & 1 deletion packages/compiler/test/checker/templates.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { deepStrictEqual, fail, strictEqual } from "assert";
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 { isUnknownType } from "../../src/index.js";
import {
BasicTestRunner,
TestHost,
Expand Down Expand Up @@ -244,6 +245,30 @@ describe("compiler: templates", () => {
});
});

it("an error type should revert to unknown", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
model Test<T> {
@test prop: T;
}
model Bar {
a: Test<notExists>;
}
`
);
const [{ prop }, diagnostics] = await testHost.compileAndDiagnose("main.tsp");
// Only one error
expectDiagnostics(diagnostics, {
code: "unknown-identifier",
message: "Unknown identifier notExists",
});

strictEqual(prop.kind, "ModelProperty");
ok(isUnknownType(prop.type), "Prop type should be unknown");
});

it("operation should still be able to be used(no extra diagnostic)", async () => {
const { pos, source } = extractCursor(`
op Action<T extends {}>(): T;
Expand Down

0 comments on commit cc2723a

Please sign in to comment.