Fix Issue 23873 - [ICE] segfault on imported static if ; else auto x (#15168)

This commit is contained in:
Razvan Nitu 2023-05-03 11:08:05 +03:00 committed by GitHub
parent 6d0a9a5607
commit c2f8f32de5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 4 deletions

View file

@ -26,6 +26,7 @@ import dmd.location;
import dmd.mtype;
import dmd.visitor;
import core.stdc.stdio;
/***********************************************************
*/
extern (C++) final class Import : Dsymbol
@ -232,7 +233,20 @@ extern (C++) final class Import : Dsymbol
* most likely because of parsing errors.
* Therefore we cannot trust the resulting AST.
*/
if (load(sc)) return;
if (load(sc))
{
// https://issues.dlang.org/show_bug.cgi?id=23873
// For imports that are not at module or function level,
// e.g. aggregate level, the import symbol is added to the
// symbol table and later semantic is performed on it.
// This leads to semantic analysis on an malformed AST
// which causes all kinds of segfaults.
// The fix is to note that the module has errors and avoid
// semantic analysis on it.
if(mod)
mod.errors = true;
return;
}
if (!mod) return; // Failed

View file

@ -1366,9 +1366,9 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
{
static if (LOG)
{
printf("Import::semantic('%s') %s\n", toPrettyChars(), id.toChars());
printf("Import::semantic('%s') %s\n", imp.toPrettyChars(), imp.id.toChars());
scope(exit)
printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
printf("-Import::semantic('%s'), pkg = %p\n", imp.toChars(), imp.pkg);
}
if (imp.semanticRun > PASS.initial)
return;
@ -1434,7 +1434,9 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
imp.addPackageAccess(scopesym);
}
imp.mod.dsymbolSemantic(null);
// if a module has errors it means that parsing has failed.
if (!imp.mod.errors)
imp.mod.dsymbolSemantic(null);
if (imp.mod.needmoduleinfo)
{

View file

@ -0,0 +1,2 @@
static if ;
else auto x

View file

@ -0,0 +1,14 @@
// https://issues.dlang.org/show_bug.cgi?id=23873
/*
TEST_OUTPUT:
---
fail_compilation/imports/import23873.d(1): Error: (expression) expected following `static if`
fail_compilation/imports/import23873.d(1): Error: declaration expected following attribute, not `;`
fail_compilation/imports/import23873.d(3): Error: no identifier for declarator `x`
---
*/
struct Foo
{
import imports.import23873;
}