ponyc icon indicating copy to clipboard operation
ponyc copied to clipboard

Incorrect array inference

Open chalcolith opened this issue 3 years ago • 3 comments

When compiling the following code:

type Foo is (Bar box | Baz box | Bool)

class Bar
  embed _items: Array[Foo] = _items.create()

  new create(items: ReadSeq[Foo]) =>
    for item in items.values() do
      _items.push(item)
    end

class Baz

actor Main
  new create(env: Env) =>
    let bar = Bar([
      true
      Bar([ false ])
    ])
    env.out.print("done")

we get the errors:

Error:
main.pony:17:10: array element not a subtype of specified array type
      Bar([ false ])
         ^
    Info:
    main.pony:6:29: array type: (this->Bar box | this->Baz box | Bool val)
      new create(items: ReadSeq[Foo]) =>
                                ^
    main.pony:6:3: element type: Bar ref^
      new create(items: ReadSeq[Foo]) =>
      ^
    main.pony:6:3: Bar ref^ is not a subtype of Bar val: ref^ is not a subcap of val
      new create(items: ReadSeq[Foo]) =>
      ^
    main.pony:6:3: Bar ref^ is not a subtype of Baz box^
      new create(items: ReadSeq[Foo]) =>
      ^
    main.pony:6:3: Bar ref^ is not a subtype of Bool val^
      new create(items: ReadSeq[Foo]) =>
      ^
    main.pony:6:3: Bar ref^ is not a subtype of any element of (this->Bar box^ | this->Baz box^ | Bool val^)
      new create(items: ReadSeq[Foo]) =>
      ^

Notice the the third info section erroneously says Bar ref^ is not a subtype of Bar val, where it should be checking against Bar box^, since type Foo is (Bar box | Baz box | Bool). Indeed in the next section it correctly checks against Baz box^.

chalcolith avatar Dec 20 '22 23:12 chalcolith

As Jason pointed out in today's sync call, it looks like the array inference for ReadSeq is missing the this-> that it needs for that check.

jemc avatar Jan 03 '23 19:01 jemc

Other way around, it's inadvertently adding a this->

jasoncarr0 avatar Jan 03 '23 20:01 jasoncarr0

Thanks for clarifying :smile:

jemc avatar Jan 03 '23 20:01 jemc