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

Add support for var patterns #5069

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

geoffromer
Copy link
Contributor

No description provided.

@github-actions github-actions bot requested a review from zygoloid March 4, 2025 22:47
Copy link
Contributor

@zygoloid zygoloid left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great!

// CHECK:STDOUT: %x.param_patt: %empty_tuple.type = value_param_pattern %x.patt, runtime_param0
// CHECK:STDOUT: %y.patt: %empty_tuple.type = binding_pattern y
// CHECK:STDOUT: %y.param_patt: %empty_tuple.type = ref_param_pattern %y.patt, runtime_param1
// CHECK:STDOUT: %.loc4_13: %empty_tuple.type = var_pattern %y.param_patt
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should compute a "pattern category", and list it here like we do for expression categories, eg

Suggested change
// CHECK:STDOUT: %.loc4_13: %empty_tuple.type = var_pattern %y.param_patt
// CHECK:STDOUT: %.loc4_13: ref %empty_tuple.type = var_pattern %y.param_patt

That said, if that information is not useful for check, I suppose we'd be computing it only for the purpose of formatted SemIR, and we can get the same info by looking at the right-hand side of the = :-)

(Just thinking out loud here, no change requested.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I've been thinking that we should at least be able to talk about "pattern categories", whether or not we need to explicitly represent them in semir.

I've also been thinking about an issue that seems closely related: it kind of seems like we're missing an expression category, for an expression that names an output parameter (i.e., that names a pattern that binds to an initializing expression). We currently treat it as a reference expression (see the TODO at the end of GetExprCategory), but I suspect we're only getting away with that because we don't yet distinguish between objects and storage (e.g. we model initialization as assignment).

// CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {} {}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%x.param_patt: %empty_tuple.type, %.loc4_13: %empty_tuple.type);
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice for there to be some mention here that %.loc4_13 is a var parameter. (Does not need to be in this PR.) I'd probably format it before the type, to match ref instructions:

fn @F(%x.param_patt: %empty_tuple.type, %.loc4_13: ref %empty_tuple.type);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we should do the same thing with the return slot? For example, in toolchain/check/testdata/class/init.carbon we have

fn @Make(%n.param_patt: %i32, %next.param_patt: %ptr.e71) -> %return.param_patt: %Class {

But maybe that should be:

fn @Make(%n.param_patt: %i32, %next.param_patt: %ptr.e71) -> out %return.param_patt: %Class {

Or even

fn @Make(%n.param_patt: %i32, %next.param_patt: %ptr.e71, out %return.param_patt: %Class) {

// CHECK:STDERR: fn F[var u: ()]();
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
// CHECK:STDERR: fail_implicit.carbon:[[@LINE+4]]:6: error: found `var` pattern in implicit parameter list [VarInImplicitParams]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we will want to allow fn F[var self: Self]() eventually.

Might be worth adding a test for that with a TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely worth adding a test, but it seems like if I remove the new diagnostic, it... just works?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, nice! I assume we get the "var declaration cannot declare a compile-time binding" error for [var N:! i32] or similar? Do we have a test for that? (Aside: I suppose that error should say "var pattern" rather than "var declaration" now.)

geoffromer and others added 2 commits March 5, 2025 14:18
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants