aboutsummaryrefslogtreecommitdiff
path: root/libphobos/src/std
diff options
context:
space:
mode:
authorIain Buclaw <ibuclaw@gdcproject.org>2025-01-05 14:40:13 +0100
committerIain Buclaw <ibuclaw@gdcproject.org>2025-01-05 14:40:13 +0100
commitf5351b38a8aff438b41cae0d133fd38d56d8cd1f (patch)
tree6c3ebcd181efe8e57c3e6fe4af8f595996b713ff /libphobos/src/std
parenta676a516701789730aa482bcef4adcb683ba0140 (diff)
downloadgcc-f5351b38a8aff438b41cae0d133fd38d56d8cd1f.zip
gcc-f5351b38a8aff438b41cae0d133fd38d56d8cd1f.tar.gz
gcc-f5351b38a8aff438b41cae0d133fd38d56d8cd1f.tar.bz2
d: Merge upstream dmd, druntime 66b93fc24a, phobos 0c28620c3
Synchronizing with the upstream release of v2.109.1. D front-end changes: - Import dmd v2.109.1. - Copying from `const(void)[]' to `void[]' is now disallowed with `-fpreview=fiximmutableconv'. - Import expressions are now treated as hex strings. - Using boolean values other than 0 or 1 in `@safe' code is now deprecated. D runtime changes: - Import dmd v2.109.1. Phobos changes: - Import dmd v2.109.1. gcc/d/ChangeLog: * dmd/MERGE: Merge upstream dmd 66b93fc24a. * dmd/VERSION: Bump version to v2.109.1. * d-builtins.cc (build_frontend_type): Update for new front-end interface. (matches_builtin_type): Likewise. * d-codegen.cc (identity_compare_p): Likewise. (call_side_effect_free_p): Likewise. * d-convert.cc (convert_expr): Likewise. (check_valist_conversion): Likewise. * d-lang.cc (d_types_compatible_p): Likewise. * d-target.cc (Target::isVectorTypeSupported): Likewise. (Target::isReturnOnStack): Likewise. (Target::preferPassByRef): Likewise. * decl.cc (class DeclVisitor): Likewise. * expr.cc (class ExprVisitor): Likewise. * typeinfo.cc (class TypeInfoVisitor): Likewise. * types.cc (class TypeVisitor): Likewise. libphobos/ChangeLog: * libdruntime/MERGE: Merge upstream druntime 66b93fc24a. * src/MERGE: Merge upstream phobos 0c28620c3. * src/Makefile.am (PHOBOS_DSOURCES): Add std/internal/test/sumtype_example_overloads.d. * src/Makefile.in: Regenerate.
Diffstat (limited to 'libphobos/src/std')
-rw-r--r--libphobos/src/std/format/spec.d6
-rw-r--r--libphobos/src/std/internal/test/sumtype_example_overloads.d17
-rw-r--r--libphobos/src/std/math/exponential.d2
-rw-r--r--libphobos/src/std/net/curl.d4
-rw-r--r--libphobos/src/std/range/package.d6
-rw-r--r--libphobos/src/std/string.d56
-rw-r--r--libphobos/src/std/sumtype.d82
-rw-r--r--libphobos/src/std/utf.d8
8 files changed, 98 insertions, 83 deletions
diff --git a/libphobos/src/std/format/spec.d b/libphobos/src/std/format/spec.d
index b129686..e5564c9 100644
--- a/libphobos/src/std/format/spec.d
+++ b/libphobos/src/std/format/spec.d
@@ -681,7 +681,7 @@ if (is(Unqual!Char == Char))
auto fmt = "Number: %6.4e\nString: %s";
auto f = FormatSpec!char(fmt);
- assert(f.writeUpToNextSpec(a) == true);
+ assert(f.writeUpToNextSpec(a));
assert(a.data == "Number: ");
assert(f.trailing == "\nString: %s");
@@ -689,13 +689,13 @@ if (is(Unqual!Char == Char))
assert(f.width == 6);
assert(f.precision == 4);
- assert(f.writeUpToNextSpec(a) == true);
+ assert(f.writeUpToNextSpec(a));
assert(a.data == "Number: \nString: ");
assert(f.trailing == "");
assert(f.spec == 's');
- assert(f.writeUpToNextSpec(a) == false);
+ assert(!f.writeUpToNextSpec(a));
assert(a.data == "Number: \nString: ");
}
diff --git a/libphobos/src/std/internal/test/sumtype_example_overloads.d b/libphobos/src/std/internal/test/sumtype_example_overloads.d
new file mode 100644
index 0000000..235659d
--- /dev/null
+++ b/libphobos/src/std/internal/test/sumtype_example_overloads.d
@@ -0,0 +1,17 @@
+/++
+For testing only.
+
+Overload set used in std.sumtype example. Needs its own internal module so that
+it can be available for `make publictests` without polluting the public API.
++/
+module std.internal.test.sumtype_example_overloads;
+
+import std.sumtype;
+
+@safe
+{
+ string handle(int) { return "got an int"; }
+ string handle(string) { return "got a string"; }
+ string handle(double) { return "got a double"; }
+ alias handle = match!handle;
+}
diff --git a/libphobos/src/std/math/exponential.d b/libphobos/src/std/math/exponential.d
index 5e90f0d..8fcd88f 100644
--- a/libphobos/src/std/math/exponential.d
+++ b/libphobos/src/std/math/exponential.d
@@ -256,7 +256,7 @@ if (isFloatingPoint!(F) && isIntegral!(G))
* If x is 0 and n is negative, the result is the same as the result of a
* division by zero.
*/
-typeof(Unqual!(F).init * Unqual!(G).init) pow(F, G)(F x, G n) @nogc @trusted pure nothrow
+typeof(Unqual!(F).init * Unqual!(G).init) pow(F, G)(F x, G n) @nogc @safe pure nothrow
if (isIntegral!(F) && isIntegral!(G))
{
import std.traits : isSigned;
diff --git a/libphobos/src/std/net/curl.d b/libphobos/src/std/net/curl.d
index 3f82301..07905fc 100644
--- a/libphobos/src/std/net/curl.d
+++ b/libphobos/src/std/net/curl.d
@@ -1063,7 +1063,7 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien
{
size_t minLen = min(buf.length, remainingData.length);
if (minLen == 0) return 0;
- buf[0 .. minLen] = remainingData[0 .. minLen];
+ buf[0 .. minLen] = cast(void[]) remainingData[0 .. minLen];
remainingData = remainingData[minLen..$];
return minLen;
};
@@ -1202,7 +1202,7 @@ private auto _basicFTP(T)(const(char)[] url, const(void)[] sendData, FTP client)
{
size_t minLen = min(buf.length, sendData.length);
if (minLen == 0) return 0;
- buf[0 .. minLen] = sendData[0 .. minLen];
+ buf[0 .. minLen] = cast(void[]) sendData[0 .. minLen];
sendData = sendData[minLen..$];
return minLen;
};
diff --git a/libphobos/src/std/range/package.d b/libphobos/src/std/range/package.d
index 995bf1e..b6fddf7 100644
--- a/libphobos/src/std/range/package.d
+++ b/libphobos/src/std/range/package.d
@@ -13539,10 +13539,10 @@ if (isInputRange!R && isIntegral!(ElementType!R))
{
size_t bitsNum = IntegralType.sizeof * 8;
- auto first = cast(IntegralType)(1);
+ auto first = IntegralType(1);
// 2 ^ (bitsNum - 1)
- auto second = cast(IntegralType)(cast(IntegralType)(1) << (bitsNum - 2));
+ auto second = cast(IntegralType)(IntegralType(1) << (bitsNum - 2));
IntegralType[] a = [first, second];
auto bw = Bitwise!(IntegralType[])(a);
@@ -13571,7 +13571,7 @@ if (isInputRange!R && isIntegral!(ElementType!R))
auto bw2 = bw[0 .. $ - 5];
auto bw3 = bw2[];
- assert(bw2.length == (bw.length - 5));
+ assert(bw2.length == bw.length - 5);
assert(bw2.length == bw3.length);
bw2.popFront();
assert(bw2.length != bw3.length);
diff --git a/libphobos/src/std/string.d b/libphobos/src/std/string.d
index b350d6b..21e1ca3 100644
--- a/libphobos/src/std/string.d
+++ b/libphobos/src/std/string.d
@@ -6331,42 +6331,42 @@ if (isSomeString!S ||
assertCTFEable!(
{
// Test the isNumeric(in string) function
- assert(isNumeric("1") == true );
- assert(isNumeric("1.0") == true );
- assert(isNumeric("1e-1") == true );
- assert(isNumeric("12345xxxx890") == false );
- assert(isNumeric("567L") == true );
- assert(isNumeric("23UL") == true );
- assert(isNumeric("-123..56f") == false );
- assert(isNumeric("12.3.5.6") == false );
- assert(isNumeric(" 12.356") == false );
- assert(isNumeric("123 5.6") == false );
- assert(isNumeric("1233E-1+1.0e-1i") == true );
-
- assert(isNumeric("123.00E-5+1234.45E-12Li") == true);
- assert(isNumeric("123.00e-5+1234.45E-12iL") == false);
- assert(isNumeric("123.00e-5+1234.45e-12uL") == false);
- assert(isNumeric("123.00E-5+1234.45e-12lu") == false);
-
- assert(isNumeric("123fi") == true);
- assert(isNumeric("123li") == true);
- assert(isNumeric("--123L") == false);
- assert(isNumeric("+123.5UL") == false);
- assert(isNumeric("123f") == true);
- assert(isNumeric("123.u") == false);
+ assert(isNumeric("1"));
+ assert(isNumeric("1.0"));
+ assert(isNumeric("1e-1"));
+ assert(!isNumeric("12345xxxx890"));
+ assert(isNumeric("567L"));
+ assert(isNumeric("23UL"));
+ assert(!isNumeric("-123..56f"));
+ assert(!isNumeric("12.3.5.6"));
+ assert(!isNumeric(" 12.356"));
+ assert(!isNumeric("123 5.6"));
+ assert(isNumeric("1233E-1+1.0e-1i"));
+
+ assert(isNumeric("123.00E-5+1234.45E-12Li"));
+ assert(!isNumeric("123.00e-5+1234.45E-12iL"));
+ assert(!isNumeric("123.00e-5+1234.45e-12uL"));
+ assert(!isNumeric("123.00E-5+1234.45e-12lu"));
+
+ assert(isNumeric("123fi"));
+ assert(isNumeric("123li"));
+ assert(!isNumeric("--123L"));
+ assert(!isNumeric("+123.5UL"));
+ assert(isNumeric("123f"));
+ assert(!isNumeric("123.u"));
// @@@BUG@@ to!string(float) is not CTFEable.
// Related: formatValue(T) if (is(FloatingPointTypeOf!T))
if (!__ctfe)
{
- assert(isNumeric(to!string(real.nan)) == true);
- assert(isNumeric(to!string(-real.infinity)) == true);
+ assert(isNumeric(to!string(real.nan)));
+ assert(isNumeric(to!string(-real.infinity)));
}
string s = "$250.99-";
- assert(isNumeric(s[1 .. s.length - 2]) == true);
- assert(isNumeric(s) == false);
- assert(isNumeric(s[0 .. s.length - 1]) == false);
+ assert(isNumeric(s[1 .. $ - 2]));
+ assert(!isNumeric(s));
+ assert(!isNumeric(s[0 .. $ - 1]));
});
assert(!isNumeric("-"));
diff --git a/libphobos/src/std/sumtype.d b/libphobos/src/std/sumtype.d
index 80ce73d..69c2a49 100644
--- a/libphobos/src/std/sumtype.d
+++ b/libphobos/src/std/sumtype.d
@@ -11,6 +11,15 @@ include:
* No dependency on runtime type information (`TypeInfo`).
* Compatibility with BetterC.
+$(H3 List of examples)
+
+* [Basic usage](#basic-usage)
+* [Matching with an overload set](#matching-with-an-overload-set)
+* [Recursive SumTypes](#recursive-sumtypes)
+* [Memory corruption](#memory-corruption) (why assignment can be `@system`)
+* [Avoiding unintentional matches](#avoiding-unintentional-matches)
+* [Multiple dispatch](#multiple-dispatch)
+
License: Boost License 1.0
Authors: Paul Backus
Source: $(PHOBOSSRC std/sumtype.d)
@@ -77,52 +86,38 @@ version (D_BetterC) {} else
assert(!isFahrenheit(t3));
}
-/** $(DIVID introspection-based-matching, $(H3 Introspection-based matching))
+/** $(DIVID matching-with-an-overload-set, $(H3 Matching with an overload set))
+ *
+ * Instead of writing `match` handlers inline as lambdas, you can write them as
+ * overloads of a function. An `alias` can be used to create an additional
+ * overload for the `SumType` itself.
+ *
+ * For example, with this overload set:
*
- * In the `length` and `horiz` functions below, the handlers for `match` do not
- * specify the types of their arguments. Instead, matching is done based on how
- * the argument is used in the body of the handler: any type with `x` and `y`
- * properties will be matched by the `rect` handlers, and any type with `r` and
- * `theta` properties will be matched by the `polar` handlers.
+ * ---
+ * string handle(int n) { return "got an int"; }
+ * string handle(string s) { return "got a string"; }
+ * string handle(double d) { return "got a double"; }
+ * alias handle = match!handle;
+ * ---
+ *
+ * Usage would look like this:
*/
version (D_BetterC) {} else
@safe unittest
{
- import std.math.operations : isClose;
- import std.math.trigonometry : cos;
- import std.math.constants : PI;
- import std.math.algebraic : sqrt;
-
- struct Rectangular { double x, y; }
- struct Polar { double r, theta; }
- alias Vector = SumType!(Rectangular, Polar);
-
- double length(Vector v)
- {
- return v.match!(
- rect => sqrt(rect.x^^2 + rect.y^^2),
- polar => polar.r
- );
- }
-
- double horiz(Vector v)
- {
- return v.match!(
- rect => rect.x,
- polar => polar.r * cos(polar.theta)
- );
- }
+ alias ExampleSumType = SumType!(int, string, double);
- Vector u = Rectangular(1, 1);
- Vector v = Polar(1, PI/4);
+ ExampleSumType a = 123;
+ ExampleSumType b = "hello";
+ ExampleSumType c = 3.14;
- assert(length(u).isClose(sqrt(2.0)));
- assert(length(v).isClose(1));
- assert(horiz(u).isClose(1));
- assert(horiz(v).isClose(sqrt(0.5)));
+ assert(a.handle == "got an int");
+ assert(b.handle == "got a string");
+ assert(c.handle == "got a double");
}
-/** $(DIVID arithmetic-expression-evaluator, $(H3 Arithmetic expression evaluator))
+/** $(DIVID recursive-sumtypes, $(H3 Recursive SumTypes))
*
* This example makes use of the special placeholder type `This` to define a
* [recursive data type](https://en.wikipedia.org/wiki/Recursive_data_type): an
@@ -227,6 +222,10 @@ version (D_BetterC) {} else
assert(pprint(*myExpr) == "(a + (2 * b))");
}
+// For the "Matching with an overload set" example above
+// Needs public import to work with `make publictests`
+version (unittest) public import std.internal.test.sumtype_example_overloads;
+
import std.format.spec : FormatSpec, singleSpec;
import std.meta : AliasSeq, Filter, IndexOf = staticIndexOf, Map = staticMap;
import std.meta : NoDuplicates;
@@ -266,8 +265,7 @@ private enum isInout(T) = is(T == inout);
*
* The special type `This` can be used as a placeholder to create
* self-referential types, just like with `Algebraic`. See the
- * ["Arithmetic expression evaluator" example](#arithmetic-expression-evaluator) for
- * usage.
+ * ["Recursive SumTypes" example](#recursive-sumtypes) for usage.
*
* A `SumType` is initialized by default to hold the `.init` value of its
* first member type, just like a regular union. The version identifier
@@ -1619,9 +1617,9 @@ enum bool isSumType(T) = is(T : SumType!Args, Args...);
* overloads are considered as potential matches.
*
* Templated handlers are also accepted, and will match any type for which they
- * can be [implicitly instantiated](https://dlang.org/glossary.html#ifti). See
- * ["Introspection-based matching"](#introspection-based-matching) for an
- * example of templated handler usage.
+ * can be [implicitly instantiated](https://dlang.org/glossary.html#ifti).
+ * (Remember that a $(DDSUBLINK spec/expression,function_literals, function literal)
+ * without an explicit argument type is considered a template.)
*
* If multiple [SumType]s are passed to match, their values are passed to the
* handlers as separate arguments, and matching is done for each possible
diff --git a/libphobos/src/std/utf.d b/libphobos/src/std/utf.d
index c0cd386..f0d5d4d 100644
--- a/libphobos/src/std/utf.d
+++ b/libphobos/src/std/utf.d
@@ -2528,8 +2528,8 @@ size_t encode(UseReplacementDchar useReplacementDchar = No.useReplacementDchar)(
encode(buf, '\u0000'); assert(buf[0] == '\u0000');
encode(buf, '\uD7FF'); assert(buf[0] == '\uD7FF');
encode(buf, '\uE000'); assert(buf[0] == '\uE000');
- encode(buf, 0xFFFE ); assert(buf[0] == 0xFFFE);
- encode(buf, 0xFFFF ); assert(buf[0] == 0xFFFF);
+ encode(buf, 0xFFFE); assert(buf[0] == 0xFFFE);
+ encode(buf, 0xFFFF); assert(buf[0] == 0xFFFF);
encode(buf, '\U0010FFFF'); assert(buf[0] == '\U0010FFFF');
assertThrown!UTFException(encode(buf, cast(dchar) 0xD800));
@@ -2749,8 +2749,8 @@ void encode(UseReplacementDchar useReplacementDchar = No.useReplacementDchar)(
encode(buf, '\u0000'); assert(buf[0] == '\u0000');
encode(buf, '\uD7FF'); assert(buf[1] == '\uD7FF');
encode(buf, '\uE000'); assert(buf[2] == '\uE000');
- encode(buf, 0xFFFE ); assert(buf[3] == 0xFFFE);
- encode(buf, 0xFFFF ); assert(buf[4] == 0xFFFF);
+ encode(buf, 0xFFFE); assert(buf[3] == 0xFFFE);
+ encode(buf, 0xFFFF); assert(buf[4] == 0xFFFF);
encode(buf, '\U0010FFFF'); assert(buf[5] == '\U0010FFFF');
assertThrown!UTFException(encode(buf, cast(dchar) 0xD800));