fix(format.read): formattedRead incorrectly static asserts with Tuple and compile-time format string

This method supports std.typecons.Tuple, however, when passing the
format string at compile-time, the format validation does not correctly
account for the std.typecons.Tuple's types and instead validates the
std.typecons.Tuple itself leading to a static assert.

This change now adds support for this feature when used with
compile-time format strings.

Fix Issue #23600

Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
This commit is contained in:
João Lourenço 2023-01-04 21:36:46 +00:00
parent 19b890230d
commit c4192e855e
No known key found for this signature in database
GPG key ID: E12EDDC07E2F4EE4

View file

@ -303,8 +303,23 @@ uint formattedRead(alias fmt, Range, Args...)(auto ref Range r, auto ref Args ar
if (isSomeString!(typeof(fmt))) if (isSomeString!(typeof(fmt)))
{ {
import std.format : checkFormatException; import std.format : checkFormatException;
import std.meta : staticMap;
import std.typecons : Tuple;
alias e = checkFormatException!(fmt, Args);
// formattedRead supports std.typecons.Tuple
// however, checkFormatException does not
// this means that all std.typecons.Tuple's types in Args must be unwrapped
// and passed to checkFormatException
template Flatten(T)
{
static if (is(T : Tuple!Args, Args...))
alias Flatten = Args;
else
alias Flatten = T;
}
alias e = checkFormatException!(fmt, staticMap!(Flatten, Args));
static assert(!e, e); static assert(!e, e);
return .formattedRead(r, fmt, args); return .formattedRead(r, fmt, args);
} }