Skip to content

Commit

Permalink
Lkt lowering: properly catch type inheritance loops
Browse files Browse the repository at this point in the history
For #622
  • Loading branch information
pmderodat committed Jul 26, 2022
1 parent 2cb44f1 commit 460af0b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions langkit/lkt_lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from langkit.compiled_types import (
ASTNodeType, AbstractNodeData, Argument, CompiledType, CompiledTypeOrDefer,
CompiledTypeRepo, EnumNodeAlternative, EnumType, Field, StructType, T,
TypeRepo, UserField, resolve_type
TypeRepo, UserField
)
from langkit.diagnostics import (
DiagnosticError, Location, check_source_language, diagnostic_context, error
Expand Down Expand Up @@ -1354,8 +1354,9 @@ def lower_type_decl(self, decl: L.TypeDecl) -> CompiledType:
try:
t = self.compiled_types[decl]
except KeyError:
# The type is not lowered yet: let's do it
pass
# The type is not lowered yet: let's do it. Add the sentinel to
# reject type inheritance loop during recursion.
self.compiled_types[decl] = None
else:
if t is None:
error('Type inheritance loop detected')
Expand Down
15 changes: 15 additions & 0 deletions testsuite/tests/grammar/invalid_derivation_loop/test.lkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import lexer_example

@with_lexer(foo_lexer)
grammar foo_grammar {
@main_rule main_rule <- or(N1("example") | N2("null"))
}

@abstract class FooNode implements Node[FooNode] {
}

class N1 : N2 {
}

class N2 : N1 {
}
7 changes: 7 additions & 0 deletions testsuite/tests/grammar/invalid_derivation_loop/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
== test.lkt ==
test.lkt:11:1: error: Type inheritance loop detected
11 | class N1 : N2 {
| ^


Done
1 change: 1 addition & 0 deletions testsuite/tests/grammar/invalid_derivation_loop/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
driver: lkt_compile

0 comments on commit 460af0b

Please sign in to comment.