From 11b98347a77bd87351a713556c714075eda3f4b1 Mon Sep 17 00:00:00 2001 From: dkorpel Date: Tue, 1 Jun 2021 15:02:34 +0200 Subject: [PATCH] use `static foreach` for correct `scope` inference --- std/conv.d | 21 +++++++++++++++++++-- std/range/package.d | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/std/conv.d b/std/conv.d index 0fc5e1d3841..33174c1ec4d 100644 --- a/std/conv.d +++ b/std/conv.d @@ -454,6 +454,23 @@ template to(T) assert(text(null) == "null"); } +// Test `scope` inference of parameters of `text` +@safe unittest +{ + static struct S + { + int* x; // make S a type with pointers + string toString() const scope + { + static int g = 0; // force toString to be impure for: + g++; // https://issues.dlang.org/show_bug.cgi?id=20150 + return "S"; + } + } + scope S s; + assert(text("a", s) == "aS"); +} + // Tests for issue 11390 @safe pure /+nothrow+/ unittest { @@ -4778,8 +4795,8 @@ private S textImpl(S, U...)(U args) // assume that on average, parameters will have less // than 20 elements app.reserve(U.length * 20); - - foreach (arg; args) + // Must be static foreach because of https://issues.dlang.org/show_bug.cgi?id=21209 + static foreach (arg; args) { static if ( isSomeChar!(typeof(arg)) || isSomeString!(typeof(arg)) || diff --git a/std/range/package.d b/std/range/package.d index ba46758df5d..4161b4f89e3 100644 --- a/std/range/package.d +++ b/std/range/package.d @@ -932,7 +932,8 @@ if (Ranges.length > 0 && public: this(R input) { - foreach (i, v; input) + // Must be static foreach because of https://issues.dlang.org/show_bug.cgi?id=21209 + static foreach (i, v; input) { source[i] = v; }