Thread overview
betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC
Oct 17, 2018
test
Oct 17, 2018
test
Oct 17, 2018
Paul Backus
Oct 18, 2018
learnfirst1
Oct 18, 2018
test
October 17, 2018
test1:
--------------------
module test1;
import test2;
enum X = getR(1,3);
void main(string[] args){}

test2:
--------------------
module test2;
struct R {
        int i;
}
R[] getR(int a, int b){
        R[] r;
        r       ~= R(a);
        r       ~= R(b);
        return r;
}


to build without betterC:  ldmd2 ./test.d -I.
to build with betterC: ldmd2 ./test.d -I.  -betterC


If build without betterC, all work fine.  if with betterC:

test2.d(3): Error: TypeInfo cannot be used with -betterC



October 17, 2018
On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote:
> test2.d(3): Error: TypeInfo cannot be used with -betterC


the first problem is the error message is not clear and can be improved.


And my question is how to workaround this to make it work with betterC.
October 17, 2018
On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote:
>
> test1:
> --------------------
> module test1;
> import test2;
> enum X = getR(1,3);
> void main(string[] args){}
>
> test2:
> --------------------
> module test2;
> struct R {
>         int i;
> }
> R[] getR(int a, int b){
>         R[] r;
>         r       ~= R(a);
>         r       ~= R(b);
>         return r;
> }

You can't append to an array in betterC code, because making space for the new elements requires allocating memory, and that uses the GC.

In theory, since you're only using the GC during CTFE, it shouldn't be a problem, but the D compiler doesn't *know* that's what you're doing, so it has to assume the function could be called at runtime--which would be an error.

If you're willing to give up on computing `X` at compile time, you could rewrite getR to allocate manually, with `malloc`:

R[] getR(int a, int b)
{
    import core.stdc.stdlib: malloc;

    R* rp = cast(R*) malloc(2 * R.sizeof);
    R[] r = rp[0 .. 2];
    r[0] = R(a);
    r[1] = R(b);
    return r;
}

October 18, 2018
On Wednesday, 17 October 2018 at 16:50:36 UTC, Paul Backus wrote:
> You can't append to an array in betterC code, because making space for the new elements requires allocating memory, and that uses the GC.
>
> In theory, since you're only using the GC during CTFE, it shouldn't be a problem, but the D compiler doesn't *know* that's what you're doing, so it has to assume the function could be called at runtime--which would be an error.


The test2 is not build as source code,  like you can use std.* in butterC ctfe function even it use GC.

for example you can use this function in betterC if it not build as source code(include from include path):

	string add_prefix(string exp) {
	    string reg = "^" ~ exp;
	    return reg ;
	}


the dynamic array append working for basic type but not for struct.

so I guess this is a dmd front bug.

October 18, 2018
On Wednesday, 17 October 2018 at 16:50:36 UTC, Paul Backus wrote:
> On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote:
>>

simple example:   you can not use functionAttributes from betterC