Skip to content

Commit d9bc84b

Browse files
Fix segfault in DComputeSemanticAnalyser::visit(VarDeclaration)
Guard against decl->type being null when semantic analysis encounters VarDeclarations from failed template instantiations in imported modules. When a @compute module imports a template via -I and uses __traits(compiles, ...) to test it, the recursive walker may visit VarDeclaration nodes whose type field is null. The unconditional dereference of decl->type on the check for Taarray/Tclass causes a segmentation fault. Fixes #5027.
1 parent 6143d88 commit d9bc84b

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

gen/semantic-dcompute.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ struct DComputeSemanticAnalyser : public StoppableVisitor {
9292
return;
9393
}
9494

95+
if (!decl->type)
96+
return;
97+
9598
if (decl->type->ty == TY::Taarray) {
9699
error(decl->loc, "associative arrays not allowed in `@compute` code");
97100
stop = true;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Regression test: importing a module with a recursive template via -I and using
2+
// __traits(compiles, ...) on it should not crash the compiler during dcompute
3+
// semantic analysis. Previously, the VarDeclaration's type field could be null
4+
// for error'd template instantiations, causing a segfault in
5+
// DComputeSemanticAnalyser::visit(VarDeclaration*).
6+
7+
// REQUIRES: target_NVPTX
8+
// RUN: %ldc -mdcompute-targets=cuda-350 -o- -I%S/inputs %s
9+
10+
@compute(CompileFor.deviceOnly) module tests.compilable.dcompute_template_import;
11+
import ldc.dcompute;
12+
import dcompute_testmod;
13+
14+
static assert(!__traits(compiles, { imported!"dcompute_testmod".hasIndirections!(int[]); }));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
template hasIndirections(T)
2+
{
3+
static if (is(T == enum))
4+
enum hasIndirections = hasIndirections;
5+
}

0 commit comments

Comments
 (0)