Skip to content
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

Typechecker fails when creating an object in array initialization #138

Closed
EricIO opened this issue Apr 24, 2015 · 7 comments
Closed

Typechecker fails when creating an object in array initialization #138

EricIO opened this issue Apr 24, 2015 · 7 comments

Comments

@EricIO
Copy link

EricIO commented Apr 24, 2015

The following minimal test case produces the error Cannot typecheck expression new Foo("bug")

class Foo
  name : string
  def init(name : string) : void {
    this.name = name
  }

class Main
  def main() : void {
    let
      arr = [new Foo("bug")]
    in
      print arr
  }

It does work if you create the object first and then initialize the array.

class Main
  def main() : void {
    let
      foo = new Foo("bug")
      arr = [foo]
    in
      print arr
  }

So the problem seems to only appear when you use new in the initialization.

@kaeluka
Copy link
Contributor

kaeluka commented Apr 24, 2015

if you turn the line: arr = [new Foo("bug")] into arr = [new Foo], it seems to work.

@TobiasWrigstad
Copy link
Contributor

That is not so strange. new Foo("bug") desugars into

let tmp = new Foo in { tmp.init("bug"); tmp }

and we don't have inference working for arbitrary statements, it seems. Or maybe inference is broken in combination with desugaring?

@TobiasWrigstad
Copy link
Contributor

So, I would update the ticket to say that it is not new that is the problem, but constructors.

@EliasC
Copy link
Contributor

EliasC commented Apr 24, 2015

My bet is on the desugaring step! The error-message happens because the constructor leaked through to the typechecker, suggesting that desugaring does not check inside arrays. Indeed, src/ir/AST/Util.hs (with which the desugarer is implemented) does not handle arrays at all. getChildren and putChildren need to be updated.

@kaeluka
Copy link
Contributor

kaeluka commented Apr 24, 2015

If this really is the bug and I'm not mistaken, it could have been caught by exhaustiveness checking: if getChildren and putChildren wouldn't have a catch-all clause at the end, the typechecker would emit a warning every time someone extends the AST but forgets to adapt these functions. This would also mean having to spell out some boring cases explicitly, but the benefits would make up for that, IMO.

@EliasC
Copy link
Contributor

EliasC commented Apr 24, 2015

@kaeluka: I agree. I wrote Util.hs and still obviously forgot to add things there, so adding the warning would be doing future implementors and bug-hunters a favor.

kikofernandez pushed a commit that referenced this issue Apr 27, 2015
This commit fixes issues #138 and #139, which were caused by missing
@kikofernandez
Copy link
Contributor

closed via PR #147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants