rascal icon indicating copy to clipboard operation
rascal copied to clipboard

Check that users do not extend `Tree` data-type

Open jurgenvinju opened this issue 4 years ago • 1 comments

Although it looks like any normal data type, the Tree data-type from ParseTree is intimitetely connected to the core of the Rascal language semantics and its (optimized) implementation.

In other words, if someone extends Tree with new constructors, which they could naturally do, then the run-time system will crash in unforeseen ways (NullPointerException, ClassCastExceptions, UnsupportedOperation, etc.), or simply fail to do its job (failing matches or skipping branches in traversals for no apparent reasons).

This proposal is to extend the static checker with a feature that flags extensions of data Tree with new constructors as compile-time errors (a warning is too weak since there is really no way that this accidentally work). The checker would need to ignore the definitions in ParseTree since those are fundamentally required. Or, we could remove those definitions and hard-wire them in the bootstrap code, but I'm more for the documentation value they have there in ParseTree.

jurgenvinju avatar Aug 16 '21 10:08 jurgenvinju

code for later use in void dataDeclaration(Tags tags, Declaration current, list[Variant] variants, Collector c){

if ("<userType.name>" == "Tree" || "<userType.name>" == "\\Tree", variants != []) {
        str project = current@\loc.entity;
        str path = current@\loc.path;

        if (path != "rascal" || path != "/src/org/rascalmpl/library/ParseTree.rsc") {
            // TODO: turn into an error once this work ok
            c.report(warning(current, "The built-in Tree data type is not extensible with additional constructors."));
        }
    }

jurgenvinju avatar Aug 16 '21 14:08 jurgenvinju