Skip to content

Commit

Permalink
[func.d] move errorSupplementalInferredAttr to expressionsem.d
Browse files Browse the repository at this point in the history
all but one of its uses are there, the other is in `canthrow.d`
  • Loading branch information
thewilsonator committed Oct 7, 2024
1 parent 20debb4 commit 725d880
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 65 deletions.
1 change: 1 addition & 0 deletions compiler/src/dmd/canthrow.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dmd.declaration;
import dmd.dsymbol;
import dmd.errorsink;
import dmd.expression;
import dmd.expressionsem : errorSupplementalInferredAttr;
import dmd.func;
import dmd.globals;
import dmd.init;
Expand Down
65 changes: 65 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,71 @@ void checkOverriddenDtor(FuncDeclaration f, Scope* sc, const ref Loc loc,
}
}

/// Print the reason why `fd` was inferred `@system` as a supplemental error
/// Params:
/// fd = function to check
/// maxDepth = up to how many functions deep to report errors
/// deprecation = print deprecations instead of errors
/// stc = storage class of attribute to check
public void errorSupplementalInferredAttr(FuncDeclaration fd, int maxDepth, bool deprecation, STC stc)
{
auto errorFunc = deprecation ? &deprecationSupplemental : &errorSupplemental;

AttributeViolation* s;
const(char)* attr;
if (stc & STC.safe)
{
s = fd.safetyViolation;
attr = "@safe";
}
else if (stc & STC.pure_)
{
s = fd.pureViolation;
attr = "pure";
}
else if (stc & STC.nothrow_)
{
s = fd.nothrowViolation;
attr = "nothrow";
}
else if (stc & STC.nogc)
{
s = fd.nogcViolation;
attr = "@nogc";
}

if (!s)
return;

if (s.fmtStr)
{
errorFunc(s.loc, deprecation ?
"which wouldn't be `%s` because of:" :
"which wasn't inferred `%s` because of:", attr);
if (stc == STC.nogc || stc == STC.pure_)
{
auto f = (cast(Dsymbol) s.arg0).isFuncDeclaration();
errorFunc(s.loc, s.fmtStr, f.kind(), f.toPrettyChars(), s.arg1 ? s.arg1.toChars() : "");
}
else
{
errorFunc(s.loc, s.fmtStr,
s.arg0 ? s.arg0.toChars() : "", s.arg1 ? s.arg1.toChars() : "", s.arg2 ? s.arg2.toChars() : "");
}
}
else if (auto sa = s.arg0.isDsymbol())
{
if (FuncDeclaration fd2 = sa.isFuncDeclaration())
{
if (maxDepth > 0)
{
errorFunc(s.loc, "which calls `%s`", fd2.toPrettyChars());
errorSupplementalInferredAttr(fd2, maxDepth - 1, deprecation, stc);
}
}
}
}

/*******************************************
* Accessing variable v.
* Check for purity and safety violations.
Expand Down
65 changes: 0 additions & 65 deletions compiler/src/dmd/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -2066,68 +2066,3 @@ struct AttributeViolation
/// ditto
RootObject arg2 = null;
}

/// Print the reason why `fd` was inferred `@system` as a supplemental error
/// Params:
/// fd = function to check
/// maxDepth = up to how many functions deep to report errors
/// deprecation = print deprecations instead of errors
/// stc = storage class of attribute to check
void errorSupplementalInferredAttr(FuncDeclaration fd, int maxDepth, bool deprecation, STC stc)
{
auto errorFunc = deprecation ? &deprecationSupplemental : &errorSupplemental;

AttributeViolation* s;
const(char)* attr;
if (stc & STC.safe)
{
s = fd.safetyViolation;
attr = "@safe";
}
else if (stc & STC.pure_)
{
s = fd.pureViolation;
attr = "pure";
}
else if (stc & STC.nothrow_)
{
s = fd.nothrowViolation;
attr = "nothrow";
}
else if (stc & STC.nogc)
{
s = fd.nogcViolation;
attr = "@nogc";
}

if (!s)
return;

if (s.fmtStr)
{
errorFunc(s.loc, deprecation ?
"which wouldn't be `%s` because of:" :
"which wasn't inferred `%s` because of:", attr);
if (stc == STC.nogc || stc == STC.pure_)
{
auto f = (cast(Dsymbol) s.arg0).isFuncDeclaration();
errorFunc(s.loc, s.fmtStr, f.kind(), f.toPrettyChars(), s.arg1 ? s.arg1.toChars() : "");
}
else
{
errorFunc(s.loc, s.fmtStr,
s.arg0 ? s.arg0.toChars() : "", s.arg1 ? s.arg1.toChars() : "", s.arg2 ? s.arg2.toChars() : "");
}
}
else if (auto sa = s.arg0.isDsymbol())
{
if (FuncDeclaration fd2 = sa.isFuncDeclaration())
{
if (maxDepth > 0)
{
errorFunc(s.loc, "which calls `%s`", fd2.toPrettyChars());
errorSupplementalInferredAttr(fd2, maxDepth - 1, deprecation, stc);
}
}
}
}

0 comments on commit 725d880

Please sign in to comment.