Repository navigation
fix(compiler): preserve templated scalar initializer types #12510
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
Open
Sylvester Kaczmarek (sylvesterkaczmarek)
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
sylvesterkaczmarek:fix/12506-templated-scalar-initializers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
94aa9d4
fix(compiler): preserve templated scalar initializer types
sylvesterkaczmarek 21ae810
Merge current TypeSpec main and preserve scalar initializer regression
sylvesterkaczmarek c1b12d7
fix(compiler): preserve warnings for late-bound scalar initializers
sylvesterkaczmarek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
packages/compiler/test/checker/values/templated-scalar-values.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| 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<string>; | ||
| const namespaceRef: TT = TT.from("a"); | ||
| scalar Test<T> { init from(a: string); } | ||
| `); | ||
| }); | ||
|
|
||
| it.each([false, true])( | ||
| "constructs an aliased scalar instantiation (declaration first: %s)", | ||
| async (declarationFirst) => { | ||
| const declaration = "scalar Test<T> { init from(a: string); }"; | ||
| const { TT, namespaceRef } = await Tester.compile(t.code` | ||
| ${declarationFirst ? declaration : ""} | ||
| alias ${t.scalar("TT")} = Test<string>; | ||
| 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<T> { 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<T> { 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<T = string> { init from(a: T); } | ||
| alias ${t.scalar("Instance")} = Test<string>; | ||
| 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<T> { init from(a: T); } | ||
| alias ${t.scalar("Instance")} = Test<string>; | ||
| 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<T> extends Base<T>; alias Instance = Derived<string>;", | ||
| "scalar Derived extends Base<string>; 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<T> { 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<string>", "123", "string"], | ||
| ["Test<int32>", '"a"', "int32"], | ||
| ["Derived<string>", "123", "string"], | ||
| ])("rejects an invalid argument to %s", async (instance, argument, parameterType) => { | ||
| const diagnostics = await Tester.diagnose(` | ||
| scalar Test<T> { init from(a: T); } | ||
| scalar Derived<T> extends Test<T>; | ||
| alias Strings = Test<string>; | ||
| alias Numbers = Test<int32>; | ||
| 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); | ||
| }); | ||
|
|
||
| it("preserves deprecation diagnostics for late-bound templated constructors", async () => { | ||
| const diagnostics = await Tester.diagnose(` | ||
| scalar Test<T> { | ||
| #deprecated "Use a different initializer" | ||
| init from(a: T); | ||
| } | ||
| alias Instance = Test<string>; | ||
| const value: Instance = Instance.from("a"); | ||
| `); | ||
|
|
||
| expectDiagnostics(diagnostics, [ | ||
| { code: "deprecated", severity: "warning", message: "Deprecated: Use a different initializer" }, | ||
| ]); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.