-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8334722: Error emitted when switch expression does not yield any value lacks reasoning #19837
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
Conversation
|
👋 Welcome back Evemose! A progress list of the required criteria for merging this PR into |
|
❗ This change is not yet ready to be integrated. |
Webrevs
|
|
Sorry, but I am not quite sure what's the problem with the (existing) error, and if the new one is better. There are two reasons for that:
It does not say it is a compile-time error is that the switch expression's type cannot be determined (see below), but that it is an error if there are not result expression. And while trouble determining the expressions type are likely a part of the motivation for this assertion, it is not the only one, I think. b) for code like: the type of the switch expression is not determined by the result expressions, but by the target type. I.e. in this case, the type of the expression is |
Firstly, I would like to note that I don't view this as an issue, that's why I filed issue as enhancement, not a bug. Now with this said, I guess it would be appropriate to identify problem more formally. I would formulate it as something as following: "Switch expression have requirement to contain at least one branch that results in value yielded at least in one of possible execution execution scenarios, unlike the switch statements. The previous error, while provides explanation on how to fix this compilation error, does not explain why it is reported. It is confusing due to inconsistency between switch expression and statement". This issue is an enhancement and not a bug, because compiler does not have to "explain itself" if it at least explains a way to fix it. However, noone was ever hurt by more robust error reporting. Now to some statements in my problem description. Why I say it is confusing instead of it could be confusing. Well, because this confusion got even to jdk mailing lists. Also, I don't think personal experience is a good argument to provide, but I also encountered question regarding this error quite a bunch of times when I introduced someone to Java. As for new error message itself, I couldn't be 100% sure, but when this question got to amber mailing list, I spent some time looking through javac codebase and the only place where this error is reported is Attr class, which is responsible for attribution (which includes type determination). That's why I say that inability to attribute type to switch expression is the effective cause of this error. I'm not sure if specification designers put more meaning in this than I am, but the other thoughts, at least, didn't make it to javac codebase (of course if I haven't missed some other place where this issue is reported, although I doubt it). To yield part: im not sure if it is strictly correct, but I always thought of non-void one-expression lambdas as ones that imolecitly return the value. Moreover, if you look into decompiled sources, generated return statements is exactly what you will see. That's why I also have always thought that whether yield keyword is specified explicitly or not, value is always yielded. That's my reasoning behind it. If you find it insufficient or find improvement not significant enough to be integrated, I could close the issue, no problem in it, I don't think lost results of those enormous efforts to change one line in errors file are too significant to be upset about after all lol. |
|
[snip]
Technically speaking, the immediate reason for this error is the assertion in the JLS. And while saying "the error is reported because the type of the switch expression cannot be determined" may be a convenient informal explanation (and it was, I think, part of the consideration why the switch expressions without any result expressions are illegal), it is not technically correct. Please see below. [snip]
I probably didn't use the best example. Let me try with a new one: The type of the switch expression here is not determined by the In other words, there are (I think) multiple reasons why code like this is illegal: but inability to determine the type of the switch expression is not one of them. The type of this switch expression would still be The types of the expressions inside the |
Thats actually true! I didnt know about this detail honestly. Concidering this info, this new message will, in fact, be missleading. Therefore, I am closing this PR. |
Please review this trivial enhancement to informativity of error emiited when switch expression does not yield value in any branch lacks reasoning.
Issue body:
Currently, when switch expression does not yield any value, compiler emits following error: "switch expression does not have any result expressions". While this gives information on how to solve the issue, the exact reason of this error stays unclear, expecially because switch statements do not have same requirement. Recently, there even were question about this in one of jdk mailing lists.
The source of issue becomes obvious if we look where this error is reported: it is emmited in Attr class when javac is unable to assign type to switch expression. Therefore, I propose to change the emitted error text to following: "Unable to determine switch expression type: no branch that yields value found".
If anyone have ideas on how to furthernore improve this error message, please feel free to share.
Progress
Issue
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/19837/head:pull/19837$ git checkout pull/19837Update a local copy of the PR:
$ git checkout pull/19837$ git pull https://git.openjdk.org/jdk.git pull/19837/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 19837View PR using the GUI difftool:
$ git pr show -t 19837Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/19837.diff
Webrev
Link to Webrev Comment