langium
langium copied to clipboard
Declared Types Incorrectly extend Inferred Types
Present in Langium version: 0.4.0
Currently, declared types can extend inferred types. This can lead to situations like the following:
// generates inferred type 'A'
A: 'a' a=ID;
// partially-declared type 'B', since it is founded on inferred type 'A', but is still only usable where declared types go (like in returns)
interface B extends A {
b: string
}
B returns B: 'b' b=ID;
In this case, we would expect the parser rule for B to have an error, since we're missing property a
. However, we get no such error.
...

This also generates the following AST types, showing that we're missing a link in the inheritance. This explains, in part, why we're not seeing an error for missing property 'a'.
export interface A extends AstNode {
a: string
}
export interface B extends AstNode {
b: string
}
If we do the same thing, but with declared types, we get an error as we would expect. ...

We should either:
- properly validate declared types extending inferred types
- prevent declared types from extending inferred types
The missing validation error affects an inferred type, but for those types our handling of properties is rather lax anyway (see also https://github.com/langium/langium/issues/562#issuecomment-1169798215). We get to the same situation with only inferred types:
A: 'a' a=ID;
B: 'b' b=ID;
C infers A: {infer B} 'c' b=ID;
So we could just decide to ignore the fuzziness here.
Another approach: consider this in the type inference, e.g. by making the a
property optional.