Skip to content

Commit

Permalink
Move global variables related to diagnostics into a separate struct
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Oct 15, 2024
1 parent b0bcf40 commit 44069a9
Show file tree
Hide file tree
Showing 56 changed files with 573 additions and 563 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ without control flow statements.
This was done successfully in src/dmd/escape.d, it wasn't easy, but
it was well worth it.

17. Try to eliminate reliance on `global.errors`, use `dmd.errorsink: ErrorSink` instead.
17. Try to eliminate reliance on `global.diag.errors`, use `dmd.errorsink: ErrorSink` instead.

18. For aggregates that expose public access to fields, think hard about why this is
necessary and if it can be done better. Merely replacing them with read/write properties
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/astbase.d
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@ struct ASTBase
extern (D) this()
{
super(Loc.initial, STMT.Error);
assert(global.gaggedErrors || global.errors);
assert(global.diag.gaggedErrors || global.diag.errors);

Check warning on line 2505 in compiler/src/dmd/astbase.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/astbase.d#L2505

Added line #L2505 was not covered by tests
}

override void accept(Visitor v)
Expand Down Expand Up @@ -6350,7 +6350,7 @@ struct ASTBase
if (errorexp is null)
errorexp = new ErrorExp();

if (global.errors == 0 && global.gaggedErrors == 0)
if (global.diag.errors == 0 && global.diag.gaggedErrors == 0)

Check warning on line 6353 in compiler/src/dmd/astbase.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/astbase.d#L6353

Added line #L6353 was not covered by tests
{
/* Unfortunately, errors can still leak out of gagged errors,
* and we need to set the error count to prevent bogus code
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dmd/blockexit.d
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ int blockExit(Statement s, FuncDeclaration func, ErrorSink eSink)

void visitMixin(MixinStatement s)
{
assert(global.errors);
assert(global.diag.errors);

Check warning on line 111 in compiler/src/dmd/blockexit.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/blockexit.d#L111

Added line #L111 was not covered by tests
result = BE.fallthru;
}

Expand Down Expand Up @@ -177,7 +177,7 @@ int blockExit(Statement s, FuncDeclaration func, ErrorSink eSink)

void visitWhile(WhileStatement s)
{
assert(global.errors);
assert(global.diag.errors);

Check warning on line 180 in compiler/src/dmd/blockexit.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/blockexit.d#L180

Added line #L180 was not covered by tests
result = BE.fallthru;
}

Expand Down Expand Up @@ -249,7 +249,7 @@ int blockExit(Statement s, FuncDeclaration func, ErrorSink eSink)

void visitForeachRange(ForeachRangeStatement s)
{
assert(global.errors);
assert(global.diag.errors);

Check warning on line 252 in compiler/src/dmd/blockexit.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/blockexit.d#L252

Added line #L252 was not covered by tests
result = BE.fallthru;
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/canthrow.d
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ CT canThrow(Expression e, FuncDeclaration func, ErrorSink eSink)
if (ce.inDebugStatement)
return;

if (global.errors && !ce.e1.type)
if (global.diag.errors && !ce.e1.type)
return; // error recovery

if (ce.f && ce.arguments.length > 0)
Expand Down
26 changes: 13 additions & 13 deletions compiler/src/dmd/clone.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ FuncDeclaration hasIdentityOpAssign(AggregateDeclaration ad, Scope* sc)
scope er = new NullExp(ad.loc, ad.type); // dummy rvalue
scope el = new IdentifierExp(ad.loc, Id.p); // dummy lvalue
el.type = ad.type;
const errors = global.startGagging(); // Do not report errors, even if the template opAssign fbody makes it.
const errors = global.diag.startGagging(); // Do not report errors, even if the template opAssign fbody makes it.
sc = sc.push();
sc.tinst = null;
sc.minst = null;
Expand All @@ -125,7 +125,7 @@ FuncDeclaration hasIdentityOpAssign(AggregateDeclaration ad, Scope* sc)
}

sc = sc.pop();
global.endGagging(errors);
global.diag.endGagging(errors);
if (!f)
return null;
if (f.errors)
Expand Down Expand Up @@ -385,7 +385,7 @@ FuncDeclaration buildOpAssign(StructDeclaration sd, Scope* sc)
sd.members.push(fop);
fop.addMember(sc, sd);
sd.hasIdentityAssign = true; // temporary mark identity assignable
const errors = global.startGagging(); // Do not report errors, even if the template opAssign fbody makes it.
const errors = global.diag.startGagging(); // Do not report errors, even if the template opAssign fbody makes it.
Scope* sc2 = sc.push();
sc2.stc = 0;
sc2.linkage = LINK.d;
Expand All @@ -395,7 +395,7 @@ FuncDeclaration buildOpAssign(StructDeclaration sd, Scope* sc)
//semantic3(fop, sc2); // isn't run here for lazy forward reference resolution.

sc2.pop();
if (global.endGagging(errors)) // if errors happened
if (global.diag.endGagging(errors)) // if errors happened
{
// Disable generated opAssign, because some members forbid identity assignment.
fop.storage_class |= STC.disable;
Expand Down Expand Up @@ -494,7 +494,7 @@ private FuncDeclaration hasIdentityOpEquals(AggregateDeclaration ad, Scope* sc)

bool hasIt(Type tthis)
{
const errors = global.startGagging(); // Do not report errors, even if the template opAssign fbody makes it
const errors = global.diag.startGagging(); // Do not report errors, even if the template opAssign fbody makes it
sc = sc.push();
sc.tinst = null;
sc.minst = null;
Expand All @@ -511,7 +511,7 @@ private FuncDeclaration hasIdentityOpEquals(AggregateDeclaration ad, Scope* sc)
f = rfc(el);

sc = sc.pop();
global.endGagging(errors);
global.diag.endGagging(errors);

return f !is null;
}
Expand Down Expand Up @@ -614,14 +614,14 @@ FuncDeclaration buildXopEquals(StructDeclaration sd, Scope* sc)
Expression e2 = new IdentifierExp(loc, Id.p);
Expression e = new EqualExp(EXP.equal, loc, e1, e2);
fop.fbody = new ReturnStatement(loc, e);
const errors = global.startGagging(); // Do not report errors
const errors = global.diag.startGagging(); // Do not report errors
Scope* sc2 = sc.push();
sc2.stc = 0;
sc2.linkage = LINK.d;
fop.dsymbolSemantic(sc2);
fop.semantic2(sc2);
sc2.pop();
if (global.endGagging(errors)) // if errors happened
if (global.diag.endGagging(errors)) // if errors happened
fop = sd.xerreq;
return fop;
}
Expand Down Expand Up @@ -738,14 +738,14 @@ FuncDeclaration buildXopCmp(StructDeclaration sd, Scope* sc)
Expression e2 = new IdentifierExp(loc, Id.p);
Expression e = new CallExp(loc, new DotIdExp(loc, e1, Id.cmp), e2);
fop.fbody = new ReturnStatement(loc, e);
const errors = global.startGagging(); // Do not report errors
const errors = global.diag.startGagging(); // Do not report errors
Scope* sc2 = sc.push();
sc2.stc = 0;
sc2.linkage = LINK.d;
fop.dsymbolSemantic(sc2);
fop.semantic2(sc2);
sc2.pop();
if (global.endGagging(errors)) // if errors happened
if (global.diag.endGagging(errors)) // if errors happened
fop = sd.xerrcmp;
return fop;
}
Expand Down Expand Up @@ -1617,7 +1617,7 @@ private Statement generateCopyCtorBody(StructDeclaration sd)
*/
bool needCopyCtor(StructDeclaration sd, out bool hasCpCtor)
{
if (global.errors)
if (global.diag.errors)
return false;

auto ctor = sd.search(sd.loc, Id.ctor);
Expand Down Expand Up @@ -1731,7 +1731,7 @@ bool buildCopyCtor(StructDeclaration sd, Scope* sc)
ccd.fbody = copyCtorBody;
sd.members.push(ccd);
ccd.addMember(sc, sd);
const errors = global.startGagging();
const errors = global.diag.startGagging();
Scope* sc2 = sc.push();
sc2.stc = 0;
sc2.linkage = LINK.d;
Expand All @@ -1740,7 +1740,7 @@ bool buildCopyCtor(StructDeclaration sd, Scope* sc)
ccd.semantic3(sc2);
//printf("ccd semantic: %s\n", ccd.type.toChars());
sc2.pop();
if (global.endGagging(errors) || sd.isUnionDeclaration())
if (global.diag.endGagging(errors) || sd.isUnionDeclaration())
{
ccd.storage_class |= STC.disable;
ccd.fbody = null;
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dmd/cond.d
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ extern (C++) final class StaticForeach : RootObject

// Run 'typeof' gagged to avoid duplicate errors and if it fails just create
// an empty foreach to expose them.
const olderrors = global.startGagging();
const olderrors = global.diag.startGagging();
ety = ety.typeSemantic(aloc, sc);
if (global.endGagging(olderrors))
if (global.diag.endGagging(olderrors))
s2.push(createForeach(aloc, pparams[1], null));
else
{
Expand Down Expand Up @@ -924,7 +924,7 @@ extern (C++) final class StaticIfCondition : Condition

int errorReturn()
{
if (!global.gag)
if (!global.diag.gag)
inc = Include.no; // so we don't see the error message again
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/constfold.d
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ UnionExp Equal(EXP op, const ref Loc loc, Type type, Expression e1, Expression e
StringExp es2 = e2.isStringExp();
if (es1.sz != es2.sz)
{
assert(global.errors);
assert(global.diag.errors);

Check warning on line 657 in compiler/src/dmd/constfold.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/constfold.d#L657

Added line #L657 was not covered by tests
cantExp(ue);
return ue;
}
Expand Down Expand Up @@ -1477,7 +1477,7 @@ UnionExp Cat(const ref Loc loc, Type type, Expression e1, Expression e2)
/* Can happen with:
* auto s = "foo"d ~ "bar"c;
*/
assert(global.errors);
assert(global.diag.errors);

Check warning on line 1480 in compiler/src/dmd/constfold.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/constfold.d#L1480

Added line #L1480 was not covered by tests
cantExp(ue);
assert(ue.exp().type);
return ue;
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/dcast.d
Original file line number Diff line number Diff line change
Expand Up @@ -2068,9 +2068,9 @@ Expression castTo(Expression e, Scope* sc, Type t, Type att = null)
* cast(to)e1.aliasthis
*/
auto exp = resolveAliasThis(sc, e);
const errors = global.startGagging();
const errors = global.diag.startGagging();
exp = castTo(exp, sc, t, att);
return global.endGagging(errors) ? null : exp;
return global.diag.endGagging(errors) ? null : exp;
}

bool hasAliasThis;
Expand Down
18 changes: 9 additions & 9 deletions compiler/src/dmd/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ extern (C++) final class AliasDeclaration : Declaration
Dsymbol err()
{
// Avoid breaking "recursive alias" state during errors gagged
if (global.gag)
if (global.diag.gag)
return this;
aliassym = new AliasDeclaration(loc, ident, Type.terror);
type = Type.terror;
Expand All @@ -632,15 +632,15 @@ extern (C++) final class AliasDeclaration : Declaration
if (inuse == 1 && type && _scope)
{
inuse = 2;
const olderrors = global.errors;
const olderrors = global.diag.errors;
Dsymbol s = type.toDsymbol(_scope);
//printf("[%s] type = %s, s = %p, this = %p\n", loc.toChars(), type.toChars(), s, this);
if (global.errors != olderrors)
if (global.diag.errors != olderrors)
return err();
if (s)
{
s = s.toAlias();
if (global.errors != olderrors)
if (global.diag.errors != olderrors)
return err();
aliassym = s;
inuse = 0;
Expand All @@ -650,7 +650,7 @@ extern (C++) final class AliasDeclaration : Declaration
Type t = type.typeSemantic(loc, _scope);
if (t.ty == Terror)
return err();
if (global.errors != olderrors)
if (global.diag.errors != olderrors)
return err();
//printf("t = %s\n", t.toChars());
inuse = 0;
Expand Down Expand Up @@ -1099,12 +1099,12 @@ extern (C++) class VarDeclaration : Declaration
assert(type && _init);

// Ungag errors when not speculative
const oldgag = global.gag;
if (global.gag)
const oldgag = global.diag.gag;
if (global.diag.gag)
{
Dsymbol sym = isMember();
if (sym && !sym.isSpeculative())
global.gag = 0;
global.diag.gag = 0;
}

if (_scope)
Expand All @@ -1118,7 +1118,7 @@ extern (C++) class VarDeclaration : Declaration
}

Expression e = _init.initializerToExpression(needFullType ? type : null);
global.gag = oldgag;
global.diag.gag = oldgag;
return e;
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/dinterpret.d
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,7 @@ public:
v.inuse++;
e = interpret(e, istate);
v.inuse--;
if (CTFEExp.isCantExp(e) && !global.gag && !ctfeGlobals.stackTraceCallsToSuppress)
if (CTFEExp.isCantExp(e) && !global.diag.gag && !ctfeGlobals.stackTraceCallsToSuppress)
errorSupplemental(loc, "while evaluating %s.init", v.toChars());
if (exceptionOrCantInterpret(e))
return e;
Expand Down Expand Up @@ -4992,7 +4992,7 @@ public:
result = paintTypeOntoLiteral(pue, e.type, result);
result.loc = e.loc;
}
else if (CTFEExp.isCantExp(result) && !global.gag)
else if (CTFEExp.isCantExp(result) && !global.diag.gag)
showCtfeBackTrace(e, fd); // Print a stack trace.
}

Expand Down
12 changes: 6 additions & 6 deletions compiler/src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ extern (C++) final class Module : Package
cast(int)name.length, name.ptr, cast(int)pkgfile.length, pkgfile.ptr);
}
}
if (!global.gag)
if (!global.diag.gag)
{
/* Print path
*/
Expand Down Expand Up @@ -929,7 +929,7 @@ extern (C++) final class Module : Package
error(md ? md.loc : loc, "%s `%s` from file %s conflicts with package name %s", kind, toPrettyChars, srcname, pkg.toChars());
}
else
assert(global.errors);
assert(global.diag.errors);

Check warning on line 932 in compiler/src/dmd/dmodule.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/dmodule.d#L932

Added line #L932 was not covered by tests
}
else
{
Expand Down Expand Up @@ -1085,7 +1085,7 @@ extern (C++) final class Module : Package
//printf("[%d] %s semantic2a\n", i, s.toPrettyChars());
s.semantic2(null);

if (global.errors)
if (global.diag.errors)
break;
}
a.setDim(0);
Expand All @@ -1102,7 +1102,7 @@ extern (C++) final class Module : Package
//printf("[%d] %s semantic3a\n", i, s.toPrettyChars());
s.semantic3(null);

if (global.errors)
if (global.diag.errors)
break;
}
a.setDim(0);
Expand Down Expand Up @@ -1260,14 +1260,14 @@ extern (C++) final class Module : Package
auto imp = new Import(Loc.initial, pkgids[], modid, null, true);
// Module.load will call fatal() if there's no module available.
// Gag the error here, pushing the error handling to the caller.
const errors = global.startGagging();
const errors = global.diag.startGagging();
imp.load(null);
if (imp.mod)
{
imp.mod.importAll(null);
imp.mod.dsymbolSemantic(null);
}
global.endGagging(errors);
global.diag.endGagging(errors);
mod = imp.mod;
return mod;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/dscope.d
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ extern (C++) struct Scope

extern (D) Dsymbol search_correct(Identifier ident)
{
if (global.gag)
if (global.diag.gag)
return null; // don't do it for speculative compiles; too time consuming

/************************************************
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dmd/dsymbol.d
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct Ungag

extern (C++) ~this() nothrow
{
global.gag = oldgag;
global.diag.gag = oldgag;
}
}

Expand Down Expand Up @@ -605,9 +605,9 @@ extern (C++) class Dsymbol : ASTNode

final Ungag ungagSpeculative() const
{
const oldgag = global.gag;
if (global.gag && !isSpeculative() && !toParent2().isFuncDeclaration())
global.gag = 0;
const oldgag = global.diag.gag;
if (global.diag.gag && !isSpeculative() && !toParent2().isFuncDeclaration())
global.diag.gag = 0;
return Ungag(oldgag);
}

Expand Down
Loading

0 comments on commit 44069a9

Please sign in to comment.