-
Notifications
You must be signed in to change notification settings - Fork 1.1k
#1589: Add error message for not emitting switch. #3556
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
#1589: Add error message for not emitting switch. #3556
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, and thank you for opening this PR! 🎉
All contributors have signed the CLA, thank you! ❤️
Have an awesome day! ☀️
|val number = (middle: @switch) match { | ||
| case 'a' => 1 | ||
| case 'm' => 13 | ||
| case last => 26 //a non-literal may prevent switch generation: this would not compile. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case _
is unreachable. You might want to either replace
case last => ...
with
case `last` => ...
or rename last
to Last
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
case class UnableToEmitSwitch()(implicit ctx: Context) | ||
extends Message(UnableToEmitSwitchID) { | ||
val kind = "Syntax" | ||
val msg = hl"""Could not emit switch for ${"@switch"} annotated match""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for triple quotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
| case 'm' => 13 | ||
| case Last => 26 //a non-literal may prevent switch generation: this would not compile. | ||
| case _ => 0 | ||
|}""".stripMargin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more complete example:
val ConstantB = 'B'
final val ConstantC = 'C'
def tokenMe(ch: Char) = (ch: @switch) match {
case '\t' | '\n' => 1
case 'A' => 2
case ConstantB => 3 // a non-literal may prevent switch generation: this would not compile
case ConstantC => 4 // a constant value is allowed
case _ => 5
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Updated.
|-The matched value is a known integer or types implicitly convertible to integer. | ||
|-The matched expression only matches literal values, without type checks, if statements, or extractors. | ||
|-The expression have its value available at compile time. | ||
|-There are more than two case statements.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If annotated with ${"@switch"}, the compiler will verify that the match has been compiled to a
tableswitch or lookupswitch and issue an error if it instead compiles into a series of conditional
expressions. Example usage:
$codeExample
The compiler will not apply the optimisation if:
- the matched value is not of type ${"Int"}, ${"Byte"}, ${"Short"} or ${"Char"}
- the matched value is not a constant literal
- there are less than three cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
Please change the commit message to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks @vitorsvieira!
Cool! 👍 |
Regarding #1589, this PR addresses "could not emit switch" (PatternMatcher.911).
Noticed that
ctx.warning
was not propagating the error messages during tests.@smarter Suggested to refactor
checkMessagesAfter
in a way that errors and warning messages are reported.Would it be something like below?
I'd be more than happy to contribute!