Skip to content

Commit

Permalink
use OutBuffer to build and pass around strings
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored and dlang-bot committed Nov 25, 2024
1 parent 4ba3411 commit 8e83403
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
8 changes: 4 additions & 4 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2972,14 +2972,14 @@ private bool functionParameters(const ref Loc loc, Scope* sc,

if (argumentList.names)
{
const(char)* msg = null;
auto resolvedArgs = tf.resolveNamedArgs(argumentList, &msg);
OutBuffer buf;
auto resolvedArgs = tf.resolveNamedArgs(argumentList, &buf);
if (!resolvedArgs)
{
// while errors are usually already caught by `tf.callMatch`,
// this can happen when calling `typeof(freefunc)`
if (msg)
error(loc, "%s", msg);
if (buf.length)
error(loc, "%s", buf.peekChars());
return true;
}
// note: the argument list should be mutated with named arguments / default arguments,
Expand Down
22 changes: 11 additions & 11 deletions compiler/src/dmd/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -2620,10 +2620,10 @@ extern (C++) final class TypeFunction : TypeNext
*
* Params:
* argumentList = array of function arguments
* pMessage = address to store error message, or `null`
* buf = if not null, append error message to it
* Returns: re-ordered argument list, or `null` on error
*/
extern(D) Expressions* resolveNamedArgs(ArgumentList argumentList, const(char)** pMessage)
extern(D) Expressions* resolveNamedArgs(ArgumentList argumentList, OutBuffer* buf)
{
Expression[] args = argumentList.arguments ? (*argumentList.arguments)[] : null;
Identifier[] names = argumentList.names ? (*argumentList.names)[] : null;
Expand All @@ -2647,8 +2647,8 @@ extern (C++) final class TypeFunction : TypeNext
const pi = findParameterIndex(name);
if (pi == -1)
{
if (pMessage)
*pMessage = getMatchError("no parameter named `%s`", name.toChars());
if (buf)
buf.writestring(getMatchError("no parameter named `%s`", name.toChars()));
return null;
}
ci = pi;
Expand All @@ -2658,8 +2658,8 @@ extern (C++) final class TypeFunction : TypeNext
if (!isVariadic)
{
// Without named args, let the caller diagnose argument overflow
if (hasNamedArgs && pMessage)
*pMessage = getMatchError("argument `%s` goes past end of parameter list", arg.toChars());
if (hasNamedArgs && buf)
buf.writestring(getMatchError("argument `%s` goes past end of parameter list", arg.toChars()));
return null;
}
while (ci >= newArgs.length)
Expand All @@ -2668,8 +2668,8 @@ extern (C++) final class TypeFunction : TypeNext

if ((*newArgs)[ci])
{
if (pMessage)
*pMessage = getMatchError("parameter `%s` assigned twice", parameterList[ci].toChars());
if (buf)
buf.writestring(getMatchError("parameter `%s` assigned twice", parameterList[ci].toChars()));
return null;
}
(*newArgs)[ci++] = arg;
Expand All @@ -2687,9 +2687,9 @@ extern (C++) final class TypeFunction : TypeNext
if (this.incomplete)
continue;

if (pMessage)
*pMessage = getMatchError("missing argument for parameter #%d: `%s`",
i + 1, parameterToChars(parameterList[i], this, false));
if (buf)
buf.writestring(getMatchError("missing argument for parameter #%d: `%s`",
i + 1, parameterToChars(parameterList[i], this, false)));
return null;
}
// strip trailing nulls from default arguments
Expand Down
30 changes: 19 additions & 11 deletions compiler/src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -753,13 +753,14 @@ extern (D) MATCH callMatch(TypeFunction tf, Type tthis, ArgumentList argumentLis
}
const(char)* failMessage;
const(char)** pMessage = errorHelper ? &failMessage : null;
auto resolvedArgs = tf.resolveNamedArgs(argumentList, pMessage);
OutBuffer buf;
auto resolvedArgs = tf.resolveNamedArgs(argumentList, errorHelper ? &buf : null);
Expression[] args;
if (!resolvedArgs)
{
if (failMessage)
if (buf.length)
{
errorHelper(failMessage);
errorHelper(buf.peekChars());
return MATCH.nomatch;
}

Expand Down Expand Up @@ -820,6 +821,11 @@ extern (D) MATCH callMatch(TypeFunction tf, Type tthis, ArgumentList argumentLis
if (!arg)
continue; // default argument
m = argumentMatchParameter(tf, p, arg, wildmatch, flag, sc, pMessage);
if (failMessage)
{
buf.reset();
buf.writestring(failMessage);
}
}
else if (p.defaultArg)
continue;
Expand All @@ -846,15 +852,17 @@ extern (D) MATCH callMatch(TypeFunction tf, Type tthis, ArgumentList argumentLis
errorHelper(failMessage);
return MATCH.nomatch;
}
if (pMessage && u >= args.length)
*pMessage = tf.getMatchError("missing argument for parameter #%d: `%s`",
u + 1, parameterToChars(p, tf, false));
// If an error happened previously, `pMessage` was already filled
else if (pMessage && !*pMessage)
*pMessage = tf.getParamError(args[u], p);

if (errorHelper)
errorHelper(*pMessage);
{
if (u >= args.length)
buf.writestring(tf.getMatchError("missing argument for parameter #%d: `%s`",
u + 1, parameterToChars(p, tf, false)));
// If an error happened previously, `pMessage` was already filled
else if (buf.length == 0)
buf.writestring(tf.getParamError(args[u], p));

errorHelper(buf.peekChars());
}
return MATCH.nomatch;
}
if (m < match)
Expand Down

0 comments on commit 8e83403

Please sign in to comment.