brownplt / pyret-lang

The Pyret language.
Other
1.07k stars 110 forks source link

Type checker: Object type does not have field. #1398

Open jswrenn opened 6 years ago

jswrenn commented 6 years ago

This program:

data Foo:
  | foo(bar :: Number)
with:
  method baz(self :: Foo) -> Number:
    self.bar
  end
end

...produces the error:

The type checker rejected your program because the object type Foo does not have a field named bar

jpolitz commented 6 years ago

Thanks for reporting. That's frustrating and kind of silly. The type-checker hasn't yet figured out the intersection of shared fields when type-checking variants' methods, so it doesn't know that all Foos have a bar yet.

If you need a workaround, this will type-check if you omit the Foo annotation; the type-checker has special handling for self in when checking methods of a datatype. Maybe it should just be a well-formedness error to annotate self.

Note also that later in the program, one can write:

fun f(x :: Foo): x.bar end

later, it will type-check. This is a problem somewhat specific to the body of a variant.