ooc-lang / rock

:ocean: self-hosted ooc compiler that generates c99
http://ooc-lang.org/
MIT License
404 stars 40 forks source link

Empty cases make rock crash #624

Closed nddrylliog closed 11 years ago

nddrylliog commented 11 years ago

If you have a match like this:

match foo {
  case bar: Baz =>
}

rock will fail with:

[OutOfBoundsException in ArrayList]: Trying to access an element at offset 0, but size is only 0!

jussi-kalliokoski commented 11 years ago
foo := 1;
match foo {
  case 1 =>
}

Builds and runs just fine. Not sure empty case is the problem. Btw, I have no idea what happens with that colon and class (: Baz) there, couldn't find documentation on that either?

$ rock --version
rock 0.9.6 codename loki, built on Wed Mar 13 22:57:47 2013
nddrylliog commented 11 years ago

@jussi-kalliokoski What happens with that colon and class is that the object is checked for its type, and cast to the appropriate target.

For example:

kill: func (entity: Entity) {
  match entity {
    case hero: Hero =>
      // I refuse to kill the hero!
      hero cheer()
    case enemy: Enemy =>
      enemy die()
  }
}

This is akin to Scala pattern matching, although much weaker/simpler.

What made my example blow up I think is that the match was used as a value (for example, as the return expression of a function.. or a variable decl.. or anything really).

So rock is looking for an expression in the case to assign to the temp value, and it fails.

Example:

a := match 1 {
  case 1 => // there's nothing there, blow up!
}

If it was correct, e.g.:

a := match Random randInt(1, 2) =>
  case 1 =>
    "blah"
  case 2 =>
    "blih"

Then it would compile down to something like this:

a: String
r := Random randInt(1, 2)
if (r == 1) {
  a = "blah"
} else if (r == 2) {
  a = "blih"
}

Except not really because of #615, but that's a separate issue :)

jussi-kalliokoski commented 11 years ago

Ahhh, ok, that makes sense, thanks. But not sure the example should even compile to anything, but the current behavior admittedly isn't desirable either. It would be good to get a descriptive error for this.

nddrylliog commented 11 years ago

Ah no the current example should err with 'no usable value in case inside a match used as expression' or some other less terrible error message. In any case it should definietly not crash rock.

alexnask commented 11 years ago

Will be on it tomorrow

nddrylliog commented 11 years ago

@shamanas Welcome back!

alexnask commented 11 years ago

@nddrylliog Happy to be here again! o/

alexnask commented 11 years ago

I can't seem to reproduce this on pacino (.9.7.x), I only get "Couldn't figure out type of match" error (which is tagged as a serious error that should be reported)

nddrylliog commented 11 years ago

Well.. then I don't know exactly what piece of code caused it, but in any case a check should be added when looking for a Scope's last element - if the Scope is empty => compile error.

nddrylliog commented 11 years ago

Complete test case:

Baz: class {}
foo := Baz new()

match foo {
  case bar: Baz =>
}
nddrylliog commented 11 years ago

Closed in 97x.