Skip to content

Commit

Permalink
small step towards AttributVfd2 (#17089)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored Nov 24, 2024
1 parent dfbc380 commit 5931100
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
6 changes: 3 additions & 3 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2057,19 +2057,19 @@ public void errorSupplementalInferredAttr(FuncDeclaration fd, int maxDepth, bool
if (!s)
return;

if (s.fmtStr)
if (s.format)
{
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() : "");
errorFunc(s.loc, s.format, f.kind(), f.toPrettyChars(), s.arg1 ? s.arg1.toChars() : "");
}
else
{
errorFunc(s.loc, s.fmtStr,
errorFunc(s.loc, s.format,
s.arg0 ? s.arg0.toChars() : "", s.arg1 ? s.arg1.toChars() : "", s.arg2 ? s.arg2.toChars() : "");
}
}
Expand Down
21 changes: 8 additions & 13 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3548,25 +3548,20 @@ class XorExp final : public BinExp
struct AttributeViolation final
{
Loc loc;
const char* fmtStr;
FuncDeclaration* fd;
const char* format;
RootObject* arg0;
RootObject* arg1;
RootObject* arg2;
AttributeViolation() :
loc(Loc(0u, 0u, 0u)),
fmtStr(nullptr),
arg0(nullptr),
arg1(nullptr),
arg2(nullptr)
loc(),
fd(),
format(),
arg0(),
arg1(),
arg2()
{
}
AttributeViolation(Loc loc, const char* fmtStr = nullptr, RootObject* arg0 = nullptr, RootObject* arg1 = nullptr, RootObject* arg2 = nullptr) :
loc(loc),
fmtStr(fmtStr),
arg0(arg0),
arg1(arg1),
arg2(arg2)
{}
};

enum class ILS : uint8_t
Expand Down
31 changes: 21 additions & 10 deletions compiler/src/dmd/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -1871,14 +1871,25 @@ extern (C++) final class NewDeclaration : FuncDeclaration
/// The `FunctionDeclaration` is then stored in `arg0` and `fmtStr` must be `null`.
struct AttributeViolation
{
/// location of error
Loc loc = Loc.init;
/// printf-style format string
const(char)* fmtStr = null;
/// Arguments for up to two `%s` format specifiers in format string
RootObject arg0 = null;
/// ditto
RootObject arg1 = null;
/// ditto
RootObject arg2 = null;
Loc loc; /// location of error

FuncDeclaration fd; /// function is the focus of the violation shared int x;

// -- OR --}

const(char)* format; /// printf-style format string
RootObject arg0; /// Arguments for up to two `%s` format specifiers in format string
RootObject arg1; /// ditto
RootObject arg2; /// ditto
// this(ref Loc loc, FuncDeclaration fd) { this.loc = loc; this.fd = fd; }

this(ref Loc loc, const(char)* format, RootObject arg0 = null, RootObject arg1 = null, RootObject arg2 = null)
{
//assert(format);
this.loc = loc;
this.format = format;
this.arg0 = arg0;
this.arg1 = arg1;
this.arg2 = arg2;
}
}
36 changes: 18 additions & 18 deletions compiler/src/dmd/safe.d
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,23 @@ bool isTrusted(FuncDeclaration fd)
* fd = function we're gonna rat on
* gag = suppress error message (used in escape.d)
* loc = location of error
* fmt = printf-style format string
* format = printf-style format string
* arg0 = (optional) argument for first %s format specifier
* arg1 = (optional) argument for second %s format specifier
* arg2 = (optional) argument for third %s format specifier
*/
extern (D) void reportSafeError(FuncDeclaration fd, bool gag, Loc loc,
const(char)* fmt = null, RootObject arg0 = null, RootObject arg1 = null, RootObject arg2 = null)
const(char)* format = null, RootObject arg0 = null, RootObject arg1 = null, RootObject arg2 = null)
{
if (fd.type.toTypeFunction().trust == TRUST.system) // function was just inferred to be @system
{
if (fmt || arg0)
fd.safetyViolation = new AttributeViolation(loc, fmt, arg0, arg1, arg2);
if (format || arg0)
fd.safetyViolation = new AttributeViolation(loc, format, arg0, arg1, arg2);
}
else if (fd.isSafe())
{
if (!gag && fmt)
.error(loc, fmt, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
if (!gag && format)
.error(loc, format, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
}
}

Expand Down Expand Up @@ -417,14 +417,14 @@ extern (D) bool setUnsafeCall(FuncDeclaration fd, FuncDeclaration f)
* sc = scope that the unsafe statement / expression is in
* gag = surpress error message (used in escape.d)
* loc = location of error
* fmt = printf-style format string
* format = printf-style format string
* arg0 = (optional) argument for first %s format specifier
* arg1 = (optional) argument for second %s format specifier
* arg2 = (optional) argument for third %s format specifier
* Returns: whether there's a safe error
*/
bool setUnsafe(Scope* sc,
bool gag = false, Loc loc = Loc.init, const(char)* fmt = null,
bool gag = false, Loc loc = Loc.init, const(char)* format = null,
RootObject arg0 = null, RootObject arg1 = null, RootObject arg2 = null)
{
if (sc.intypeof)
Expand All @@ -439,7 +439,7 @@ bool setUnsafe(Scope* sc,
{
if (sc.varDecl.storage_class & STC.safe)
{
.error(loc, fmt, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
.error(loc, format, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
return true;
}
else if (!(sc.varDecl.storage_class & STC.trusted))
Expand All @@ -458,17 +458,17 @@ bool setUnsafe(Scope* sc,
{
// Message wil be gagged, but still call error() to update global.errors and for
// -verrors=spec
.error(loc, fmt, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
.error(loc, format, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
return true;
}
return false;
}

if (setFunctionToUnsafe(sc.func))
{
if (fmt || arg0)
if (format || arg0)
{
reportSafeError(sc.func, gag, loc, fmt, arg0, arg1, arg2);
reportSafeError(sc.func, gag, loc, format, arg0, arg1, arg2);
}
return sc.func.isSafe(); // it is only an error if in an @safe function
}
Expand All @@ -490,23 +490,23 @@ bool setUnsafe(Scope* sc,
* fs = feature state from the preview flag
* gag = surpress error message
* loc = location of error
* msg = printf-style format string
* format = printf-style format string
* arg0 = (optional) argument for first %s format specifier
* arg1 = (optional) argument for second %s format specifier
* arg2 = (optional) argument for third %s format specifier
* Returns: whether an actual safe error (not deprecation) occured
*/
bool setUnsafePreview(Scope* sc, FeatureState fs, bool gag, Loc loc, const(char)* msg,
bool setUnsafePreview(Scope* sc, FeatureState fs, bool gag, Loc loc, const(char)* format,
RootObject arg0 = null, RootObject arg1 = null, RootObject arg2 = null)
{
//printf("setUnsafePreview() fs:%d %s\n", fs, msg);
//printf("setUnsafePreview() fs:%d %s\n", fs, format);
with (FeatureState) final switch (fs)
{
case disabled:
return false;

case enabled:
return sc.setUnsafe(gag, loc, msg, arg0, arg1, arg2);
return sc.setUnsafe(gag, loc, format, arg0, arg1, arg2);

case default_:
if (!sc.func)
Expand All @@ -515,13 +515,13 @@ bool setUnsafePreview(Scope* sc, FeatureState fs, bool gag, Loc loc, const(char)
{
if (!gag && !sc.isDeprecated())
{
deprecation(loc, msg, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
deprecation(loc, format, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
}
}
else if (!sc.func.safetyViolation)
{
import dmd.func : AttributeViolation;
sc.func.safetyViolation = new AttributeViolation(loc, msg, arg0, arg1, arg2);
sc.func.safetyViolation = new AttributeViolation(loc, format, arg0, arg1, arg2);
}
return false;
}
Expand Down

0 comments on commit 5931100

Please sign in to comment.