Bug 22334 – TypeInfo is used in inexplicable places
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-09-24T09:10:48Z
Last change time
2023-01-03T13:27:21Z
Keywords
betterC
Assigned to
No Owner
Creator
SHOO
Comments
Comment #0 by zan77137 — 2021-09-24T09:10:48Z
This code doesn't work with `-betterC`:
-------------------------------------------
void _ctfeAppend(T)(ref T[] store, T dat)
{
static if (is(T U: const(U)))
{
U[] buf;
buf.length = 1;
buf[0] = dat;
store = cast(T[])buf;
}
}
void put(R, E)(R, E){}
struct Data{ string text;}
Data[] getData(string str)
{
if (__ctfe)
{
Data[] ret;
Data tmp;
tmp.text._ctfeAppend(str[0]);
ret._ctfeAppend(tmp);
return ret;
}
return null;
}
void foo(Data dat, DstRange)(DstRange dst)
{
put(dst, dat);
}
extern(C) void main()
{
static immutable dat = getData("a")[0];
char[] buf;
foo!dat(buf);
}
-------------------------------------------
Even though the above code does not use TypeInfo at first glance, the compiler says "Error: `TypeInfo` cannot be used with -betterC".
By the way, in CTFE, there should be no restriction on using TypeInfo, even if it is betterC.
The issue related to the relaxation of the CTFE+BetterC restrictions relates to the following:
https://issues.dlang.org/show_bug.cgi?id=18472
Comment #1 by maxsamukha — 2021-09-24T10:47:18Z
Assigning to an array's length may reallocate, and allocation still needs TypeInfo. You could try to get away with a lambda (not tested):
alias _ctfeAppend = (store, dat)
{
alias T = typeof(dat);
static if (is(T U: const(U)))
{
pragma(msg, U);
U[] buf;
buf.length = 1;
buf[0] = dat;
store = cast(T[])buf;
}
};
However, the lambda hack is unusable for anything slightly less trivial (no overloading, if-constraints, variadic parameters, etc.)
Comment #2 by zan77137 — 2021-09-28T12:16:19Z
(In reply to Max Samukha from comment #1)
> Assigning to an array's length may reallocate, and allocation still needs
> TypeInfo. You could try to get away with a lambda (not tested):
>
> alias _ctfeAppend = (store, dat)
> {
> alias T = typeof(dat);
>
> static if (is(T U: const(U)))
> {
> pragma(msg, U);
> U[] buf;
> buf.length = 1;
> buf[0] = dat;
> store = cast(T[])buf;
> }
> };
>
> However, the lambda hack is unusable for anything slightly less trivial (no
> overloading, if-constraints, variadic parameters, etc.)
Apparently, lambda does not solve the problem either...
And I doubt that reallocate is the cause, as the following code works:
-------------------------------------------
void _ctfeAppend(T)(ref T[] store, T dat)
{
static if (is(T U: const(U)))
{
U[] buf;
buf.length = 1;
buf[0] = dat;
store = cast(T[])buf;
}
}
string[] getData()
{
if (__ctfe)
{
string[] dat;
dat._ctfeAppend("aaa");
return dat;
}
return null;
}
extern(C) void main()
{
static immutable dat = getData()[0];
static assert(dat == "aaa");
}
-------------------------------------------
Comment #3 by dkorpel — 2023-01-03T13:27:21Z
The error now gives a location to the offending expression.
Allowing TypeInfo needing operations in CTFE only is covered by Issue 18472 like you mentioned.
*** This issue has been marked as a duplicate of issue 21477 ***