Bug 476290 - Strange switch expression allowed
Summary: Strange switch expression allowed
Status: UNCONFIRMED
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.8.4   Edit
Hardware: PC Windows 7
: P3 minor (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-09-01 06:41 EDT by Bernhard Stadler CLA
Modified: 2016-06-10 05:57 EDT (History)
1 user (show)

See Also:


Attachments
Generated code (2.52 KB, text/x-java-source)
2015-09-01 06:41 EDT, Bernhard Stadler CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Bernhard Stadler CLA 2015-09-01 06:41:30 EDT
Created attachment 256278 [details]
Generated code

Xtend 2.8.4 seems to allow strange switch expressions like the following:

  def getFoo(Object obj) {
    switch (obj) { String: "foo", Integer: "bar" : "???" default: "!!!" }
  }

Please notice the comma after "foo" and the colon after "bar".

The commas can even be repeated, so this is grammatically correct:

  def getFoo2(Object obj) {
    switch (obj) { ,,,,,,: "???" }
  }

Also, this seems to coincidentally allow a nice shorthand:

  def getBar(Object obj) {
    switch (obj) { String: "bar" : "???" }
  }

I like the idea of allowing commas for switch case separation, and I can't think of any ambiguities involving fall-through commas.

  def getBar2(Object obj) {
    switch (obj) { String: "bar", Integer: "foo", default: "???" }
  }


Which seems to produce code equivalent to:

  def getBar3(Object obj) {
    switch (obj) { String: "bar" default: "???" }
  }

I attached the generated code.
Comment 1 Bernhard Stadler CLA 2015-09-01 07:08:34 EDT
Sorry, getBar3() is equivalent to getBar() of course, not to getBar2().
Comment 2 Sven Efftinge CLA 2015-09-01 09:01:01 EDT
we should invalidate XCasePart without 'typeGuard' or 'case'.
Comment 3 Sven Efftinge CLA 2015-09-01 09:08:20 EDT
This should do it:
XCasePart:
	{XCasePart}
	(typeGuard=JvmTypeReference ('case' case=XExpression)?
	 |'case' case=XExpression)
		(':' then=XExpression | fallThrough?=',') ;