Skip to content

Improve error message for failure of generic subtyping of concrete types #4514

Description

@chalcolith

The following program:

class box B

class box A
  let bs: Array[B] = Array[B]
  
  new create(bs': Array[B box] box) =>
    bs.append(bs')
    
actor Main
  new create(env: Env) =>
    let bs: Array[B val] trn = [ recover B end ]
    let bs': Array[B val] val = consume bs
    let a = A(bs')
    env.out.print("done")

https://playground.ponylang.io/?gist=5cdad1a2e82f962b034e277e23d1a91f

Produces the errors:

main.pony:14:15: argument not assignable to parameter
    let a = A(bs')
              ^
    Info:
    main.pony:14:15: argument type is Array[B val] val
        let a = A(bs')
                  ^
    main.pony:7:14: parameter type requires Array[B box] box^
      new create(bs': Array[B box] box) =>
                 ^
    main.pony:7:25: B box is not a subtype of B val: box is not a subcap of val
      new create(bs': Array[B box] box) =>
                            ^
    main.pony:13:14: Array[B val] val has different type arguments than Array[B box] box^
        let bs': Array[B val] val = consume bs
                 ^

Where the compiler is not allowing us to pass an Array[B val] to a parameter of type Array[B box]. It would be nice to allow covariance for type parameters' capabilities so that we could do this.

The code in question seems to be

if(!is_eq_typeargs(sub, super, errorf, opt))
; there is some discussion on Zulip: https://ponylang.zulipchat.com/#narrow/stream/189985-beginner-help/topic/parameter.20subtyping

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions