From a4dc6247ade19eac03e0643d56f0877d39aaec6e Mon Sep 17 00:00:00 2001 From: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> Date: Thu, 24 Apr 2025 14:19:45 -0700 Subject: Fix attempt to construct an abstract AST node (#6905) Work on #6892 The function `tryInferLoopMaxIterations` in `slang-check-stmt.cpp` was doing: auto litExpr = m_astBuilder->create(); but the declaration of `LiteralExpr` in `slang-ast-expr.h` had been marked as abstract, during the switch to using the `slang-fiddle` tool to generate code: FIDDLE(abstract) class LiteralExpr : public Expr { ... } In this case, the intention of the AST design is that `LiteralExpr` should be kept abstract, and only the concrete subclasses should ever be instantiated. Because of some historical design choices, the `ASTNodeType` enumeration includes both the concrete and abstract AST classes, so the code ended up constructing a `LiteralExpr` that had the tag `ASTNodeType::LiteralExpr`. Then attempts to use the `ASTNodeDispatcher` code on such a node caused crashes, because the dispatcher code only included `case` statements for the non-abstract `ASTNodeType`s. The quick fix here was to change the `tryInferLoopMaxIterations` function to instead do: auto litExpr = m_astBuilder->create(); A test case was added to help catch any future regressions on this specific issue. A more long-term fix should involve introducing code that statically and/or dynamically prohibits the creation of instances of AST node classes that have been marked abstract. --- tests/bugs/serialization/gh-6892.slang | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/bugs/serialization/gh-6892.slang (limited to 'tests/bugs') diff --git a/tests/bugs/serialization/gh-6892.slang b/tests/bugs/serialization/gh-6892.slang new file mode 100644 index 000000000..1235c292c --- /dev/null +++ b/tests/bugs/serialization/gh-6892.slang @@ -0,0 +1,9 @@ +//TEST:COMPILE: tests/bugs/serialization/gh-6892.slang -o tests/bugs/serialization/gh-6892.slang-module + +// assertion failure when compiling to a slang module + +void f() +{ + for (int i = 0; i < 1; ++i) + ; +} -- cgit v1.2.3