Why can't pat_param be followed by a colon in macro_rules?

I wrote this macro only to discover that pat_param fragments (otherwise known as PatternNoTopAlts cannot be followed by :s. As far as I can tell, PatternNoTopAlts cannot be terminated by a :. I get that there are potential forward compatibility concerns, but it is also my understanding that StructPatternFields make the prospect of adding type annotations to patterns quite awkward, and I figured that the ability to match something as integral as a let statement would be a higher priority.

Are there any other uses of : in patterns being considered?

1 Like

Given that pat_param is in parameters (duh) which are followed by : always, this seems at least plausible? I don't think we'd want to do it for pat in general, but for pat_param maybe?

My suspicion is it's a holdover from when pat_param was split off from pat, which itself (at the time) needed to handle type ascription.

2 Likes

I would also like it if we didn't lift this restriction, because I'd like to reuse top-level pattern type ascription as the syntax to obtain access to anonymous enum variants:

fn foo() -> A | B { todo!() }

match foo() {
    x: A => {}
    x: B => {}
}
1 Like

Yes, absolutely. But for pat_param specifically, I don't see any argument for keeping the restriction given that it is literally designed to be followed by :.

2 Likes