scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Nest patterns in pattern match guards

Open dwijnand opened this issue 3 years ago • 1 comments

Fixes https://github.com/lampepfl/dotty-feature-requests/issues/301

dwijnand avatar May 22 '22 17:05 dwijnand

So the implementation I've got here is rewriting the nested pattern match during parser into its RHS with an added default case that uses a special "continue" method invocation. Then, during pattern match translation (in its plan emission) (1) that invocation gets dropped and (2) the inner return jumps are written to jump to the outer resultLabel.

I didn't initially have it rewrite in the parser, trying to preserve the true nature of the syntax. I had it rewrite in typer, because I needed the subtrees to type correct, specifically the match's type needed to come from its RHS and the guard need to be <: Boolean. But that meant having essentially the same identifying logic (Match/InfixOp) in both parser and typer and have typer rewrite the code anyways. I would have thought that the truest way to do it would have been to keep the syntax representation, typed, and let pattern matcher translation handle it. But there's quite the overhead in introducing a new tree and there seems to be quite the precedent of untyped trees that get rewritten in Typer via desugar, like Throw and the loop trees like ForYield, ForDo. If desired I can go back to delaying the rewrite to Typer.

dwijnand avatar May 23 '22 09:05 dwijnand