eclipse-langium / langium

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

Array properties are sometimes undefined instead of empty arrays #1372

Closed drhagen closed 5 months ago

drhagen commented 5 months ago

Langium version: 2.1.0 Package name: hello-world

Steps To Reproduce

  1. Create a Hello World project with yo langium
  2. Replace hello-world.langium with the following:
grammar HelloWorld

entry Model:
    'header' SEP
    ((persons+=Person))*
;

Person:
    'person' name=ID;

SEP:
    {infer SEP} ';';

hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;
  1. Replace hello-world-validator.ts with a basic standard checkUniqueDefs implementation:
import type { ValidationAcceptor, ValidationChecks } from 'langium';
import type { HelloWorldAstType, Model } from './generated/ast.js';
import type { HelloWorldServices } from './hello-world-module.js';

/**
 * Register custom validation checks.
 */
export function registerValidationChecks(services: HelloWorldServices) {
    const registry = services.validation.ValidationRegistry;
    const validator = services.validation.HelloWorldValidator;
    const checks: ValidationChecks<HelloWorldAstType> = {
        Model: validator.checkUniqueDefs
    };
    registry.register(checks, validator);
}

/**
 * Implementation of custom validations.
 */
export class HelloWorldValidator {
    checkUniqueDefs(model: Model, accept: ValidationAcceptor): void {
        const reported = new Set();
        model.persons.forEach(d => {
            if (reported.has(d.name)) {
                accept('error', `non-unique name '${d.name}'.`, { node: d, property: 'name' });
            }
            reported.add(d.name);
        });
    }
}
  1. Attempt to validate the following file:
header;
  1. See the error in the console
An error occurred during validation: TypeError: Cannot read properties of undefined (reading 'forEach')
    at HelloWorldValidator.checkUniqueDefs (/home/david/hello-world/out/language/main.cjs:33902:19)
    at /home/david/hello-world/out/language/main.cjs:14698:21
    at /home/david/hello-world/out/language/main.cjs:17481:15
    at async Promise.all (index 0)
    at async DefaultDocumentValidator.validateAst (/home/david/hello-world/out/language/main.cjs:17477:5)
    at async DefaultDocumentValidator.validateDocument (/home/david/hello-world/out/language/main.cjs:17394:27)
    at async DefaultDocumentBuilder.validate (/home/david/hello-world/out/language/main.cjs:32812:25)
    at async DefaultDocumentBuilder.runCancelable (/home/david/hello-world/out/language/main.cjs:32774:7)
    at async DefaultDocumentBuilder.buildDocuments (/home/david/hello-world/out/language/main.cjs:32749:5)
    at async DefaultDocumentBuilder.update (/home/david/hello-world/out/language/main.cjs:32706:5)
TypeError: Cannot read properties of undefined (reading 'forEach')
    at HelloWorldValidator.checkUniqueDefs (/home/david/hello-world/out/language/main.cjs:33902:19)
    at /home/david/hello-world/out/language/main.cjs:14698:21
    at /home/david/hello-world/out/language/main.cjs:17481:15
    at async Promise.all (index 0)
    at async DefaultDocumentValidator.validateAst (/home/david/hello-world/out/language/main.cjs:17477:5)
    at async DefaultDocumentValidator.validateDocument (/home/david/hello-world/out/language/main.cjs:17394:27)
    at async DefaultDocumentBuilder.validate (/home/david/hello-world/out/language/main.cjs:32812:25)
    at async DefaultDocumentBuilder.runCancelable (/home/david/hello-world/out/language/main.cjs:32774:7)
    at async DefaultDocumentBuilder.buildDocuments (/home/david/hello-world/out/language/main.cjs:32749:5)
    at async DefaultDocumentBuilder.update (/home/david/hello-world/out/language/main.cjs:32706:5)

The cause of this error is that model.persons is undefined rather than [] as required by the attribute type. This may be related to to #1358 because the error does not occur if the SEP after the header is any of the following things:

msujew commented 5 months ago

This is a consequence of https://github.com/eclipse-langium/langium/issues/1371. I'll close this in favor of the other one.