eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
754 stars 68 forks source link

References declared in super types can not be resolved for subtypes #815

Closed l-ehlers closed 1 year ago

l-ehlers commented 1 year ago

References declared in super types can not be resolved for subtypes.

Langium version: 0.5.0 (and also tested in locally linked Langium from current main branch)

Steps To Reproduce

  1. Use minimal grammar pasted below (example.langium)
  2. langium:generate etc
  3. Try to use grammar with example from test.example
  4. See error below

Code example:

example.langium file (not working)

entry Model:
    examples += Example*
;

Example returns Example: 
    {SpecificExample} "example" name=ID "using" parent=[Example:ID]
;

interface Example {
    parent: @Example
}

interface SpecificExample extends Example {
}

example.langium file (working)

entry Model:
    examples += Example*
;

Example returns Example: 
    {SpecificExample} "example" name=ID "using" parent=[Example:ID]
;

interface Example {
}

interface SpecificExample extends Example {
    parent: @Example
}

test.example file

example A using B

example B using A

The current behavior

In the "not working" case: An error occurred while resolving reference to 'B': Error: SpecificExample:parent is not a valid reference id. type-derivation-test-language(linking-error)

Anticipated cause: in the ast.ts, getReferenceType looks like this:

getReferenceType(refInfo: ReferenceInfo): string {
        const referenceId = `${refInfo.container.$type}:${refInfo.property}`;
        switch (referenceId) {
            case 'Example:parent': {
                return Example;
            }
            default: {
                throw new Error(`${referenceId} is not a valid reference id.`);
            }
        }
    }

since SpecificExample:parent is not explicitly listed, it cannot be resolved

The expected behavior

Also references defined in super types should work in sub-types

spoenemann commented 1 year ago

I think this needs to be fixed in the generated AstReflection class.