Skip to content
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

[compiler] Named instantiation of template arguments #2546

Merged

Conversation

witemple-msft
Copy link
Member

@witemple-msft witemple-msft commented Oct 6, 2023

Implementation of named template arguments as described in #2340.

I added a new syntax node (TemplateArgument), and a cover grammar for this node:

<Expression> ('=' <Expression>)?

Rather than ExpressionNode, the type of a template argument is now TemplateArgumentNode.

If the = is parsed, we assert in the parser that the first Expression must be a "bare identifier" (i.e. a TypeReference with target: Identifier) and unwrap the identifier to produce a clean AST with `name?: Identifier

Template arguments are evaluated in the order that they were declared, not in the order they are specified in the instantiation. The new template argument checker replaces the previous one, and it performs normalization of argument order and typechecking of template arguments all in one pass.

TODO:

  • Review templates across core libraries to ensure that template arguments have good names.
  • Documentation of new behavior in website.
  • Semantic/tmlanguage colorization of tokens.
  • Validate that the AST printer produces the correct text for the new template argument nodes and that the output is well-formatted.
  • Completion of template argument names in argument position.
  • Hover context on template argument names in instantiation.

Closes #2340

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2023

Changes in this PR will be published to the following url to try(check status of TypeSpec Pull Request Try It pipeline for publish status):
Playground: https://cadlplayground.z22.web.core.windows.net/prs/2546/

Website: https://tspwebsitepr.z22.web.core.windows.net/prs/2546/

@witemple-msft witemple-msft self-assigned this Oct 6, 2023
@witemple-msft witemple-msft changed the title [draft/compiler] Named instantiation of template arguments [compiler] Named instantiation of template arguments Oct 10, 2023
@garethj-msft
Copy link
Member

Validated in the playground that this allows the required customization for Microsoft Graph - thank you!

packages/compiler/src/server/completion.ts Outdated Show resolved Hide resolved
packages/compiler/src/server/completion.ts Outdated Show resolved Hide resolved
@@ -60,7 +60,7 @@ describe("compiler: templates", () => {
const diagnostics = await testHost.diagnose("main.tsp");
strictEqual(diagnostics.length, 1);
strictEqual(diagnostics[0].code, "invalid-template-args");
strictEqual(diagnostics[0].message, "Too few template arguments provided.");
strictEqual(diagnostics[0].message, "Template argument 'T' is required and not specified.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that message stay the same? If we are missing positional parameters? Feels like otherwise this error leads you to believe we are only missing template argument T while we might be missing more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I struggled with this a little bit. You'll at least get one error per uninstantiated parameter name. When I thought about it, it just didn't seem like "too few" was really the right error anymore, for example:

Given alias Foo<T, U = string>, Foo<U = "bar">... well now I don't exactly have "too few" arguments. I have enough arguments; they're just the wrong ones and T is unbound.

I could also specify a list of args with bogus names in a template with different names, and then the problem wouldn't be that I have too few of them either:

Given alias Foo<T, U, V>, Foo<X = string, Y = numeric, Z = boolean>... I have enough args, they're just all wrong, so "too few" is again not quite the right error anymore.

I eventually landed on just raising one Missing required template argument error per missing arg, but I'm open to alternatives, just don't think "too few" is the one anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hhm I guess that's what you get in csharp when missing an argument on a function
image

model A<T, U, V extends string = string> { a: T, b: U, c: V }

@test model B {
foo: ~~~A<boolean, V = "bar", string>~~~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the error here be on string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two errors, one on the whole typeref (Template argument 'U' is required and not specified.) and one on string (positional cannot follow named). I calculate the pos/end of the error on string from the outer type reference.

Diagnostic[],
];

const foo = B.properties.get("foo")!.type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: does it matter to test the content of this template if there was an error creating it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is just making sure I didn't break anything in the logic about how to instantiate unspecified types. We instantiate them to their constraint (or unknown in this case as the implied constraint of an unconstrained parameter).

packages/compiler/test/checker/templates.test.ts Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update the naming convention doc to say how to name template param (pascal case without T?)

@garethj-msft
Copy link
Member

@allenjzhang Gentle nudge on getting this into the build. It's been open quite a while.

@witemple-msft
Copy link
Member Author

@garethj-msft Just to give you a concrete ETA: we've got a couple of open PRs blocking this one -- just renames of template args that we've been discussing. I'm aiming to have this merged next week for release in January.

Copy link
Member

@timotheeguerin timotheeguerin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just need the style-guide update and a nitpick on the formatter printer.

@azure-sdk
Copy link
Collaborator

You can try these changes at https://cadlplayground.z22.web.core.windows.net/prs/2546/

Check the website changes at https://tspwebsitepr.z22.web.core.windows.net/prs/2546/

@witemple-msft witemple-msft merged commit cb92f49 into microsoft:main Dec 18, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[compiler] Named instantiation of template arguments
6 participants