Bug 16018 – fold size_t[] with int seed causes forward reference error
Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-05-11T23:21:24Z
Last change time
2019-12-06T19:05:46Z
Assigned to
No Owner
Creator
John Colvin
Comments
Comment #0 by john.loughran.colvin — 2016-05-11T23:21:24Z
% cat test.d
import std.algorithm;
void foo(size_t[] shape)
{
shape[].fold!"a*b"(1);
}
% dmd test.d
std/traits.d(551): Error: forward reference of variable parentPrefix
std/traits.d(461): Error: template instance std.traits.fqnSym!(binaryFun) error instantiating
std/algorithm/iteration.d(2680): instantiated from here: fullyQualifiedName!(binaryFun)
std/algorithm/iteration.d(2661): instantiated from here: reduceImpl!(false, ulong[], int)
std/algorithm/iteration.d(2645): instantiated from here: reducePreImpl!(ulong[], int)
std/algorithm/iteration.d(3024): instantiated from here: reduce!(int, ulong[])
test.d(4): instantiated from here: fold!(ulong[], int)
std/algorithm/iteration.d(2677): Error: static assert __error
std/algorithm/iteration.d(2661): instantiated from here: reduceImpl!(false, ulong[], int)
std/algorithm/iteration.d(2645): instantiated from here: reducePreImpl!(ulong[], int)
std/algorithm/iteration.d(3024): instantiated from here: reduce!(int, ulong[])
test.d(4): instantiated from here: fold!(ulong[], int)
works fine if you replace 1 with size_t(1)
Comment #1 by bugzilla — 2019-12-06T19:05:46Z
Meanwhile the example produces a static assert error:
Error: static assert: "Incompatible function/seed/element: binaryFun/int/ulong"
The type of the seed defines the type of the result, here: int, but the elements of the range are of type size_t, which is ulong on 64bit computers. As int * ulong results in a ulong, which cannot implicitly be casted to int, this results in an error.
The example works, when 1 is replaced by 1L or size_t.init