From 5931100c225b61f762178d0a5deb6c9304634575 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Sat, 23 Nov 2024 22:51:42 -0800 Subject: [PATCH] small step towards AttributVfd2 (#17089) --- compiler/src/dmd/expressionsem.d | 6 +++--- compiler/src/dmd/frontend.h | 21 +++++++------------ compiler/src/dmd/func.d | 31 ++++++++++++++++++--------- compiler/src/dmd/safe.d | 36 ++++++++++++++++---------------- 4 files changed, 50 insertions(+), 44 deletions(-) diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index d2b5ef43a184..7e319daa34ba 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -2057,7 +2057,7 @@ 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:" : @@ -2065,11 +2065,11 @@ public void errorSupplementalInferredAttr(FuncDeclaration fd, int maxDepth, bool 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() : ""); } } diff --git a/compiler/src/dmd/frontend.h b/compiler/src/dmd/frontend.h index 44f65921ea87..e7cdf7e9e065 100644 --- a/compiler/src/dmd/frontend.h +++ b/compiler/src/dmd/frontend.h @@ -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 diff --git a/compiler/src/dmd/func.d b/compiler/src/dmd/func.d index e9b9367fd32e..ad4231199e2c 100644 --- a/compiler/src/dmd/func.d +++ b/compiler/src/dmd/func.d @@ -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; + } } diff --git a/compiler/src/dmd/safe.d b/compiler/src/dmd/safe.d index 33b359e298f9..57b7c715b7cb 100644 --- a/compiler/src/dmd/safe.d +++ b/compiler/src/dmd/safe.d @@ -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() : ""); } } @@ -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) @@ -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)) @@ -458,7 +458,7 @@ 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; @@ -466,9 +466,9 @@ bool setUnsafe(Scope* sc, 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 } @@ -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) @@ -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; }