How to fix "typesafe variadic function parameter"?

2022-05-24 Thread Andrey Zherikov via Digitalmars-d-learn

How to fix this?
```d
struct S
{
string[] s;
}

auto foo(string[] s...)   // Error: typesafe variadic function 
parameter `s` of type `string[]` cannot be marked `return`

{
return S(s);
}

void main()
{
import std.stdio: writeln;

writeln(foo("Hello D"));
}
```

This successfully compiles with dmd 2.099.1 but fails with 2.100.0

I tried to add `return`/`ref` but no luck.


Re: How to fix "typesafe variadic function parameter"?

2022-05-24 Thread Adam Ruppe via Digitalmars-d-learn

On Tuesday, 24 May 2022 at 22:46:55 UTC, Andrey Zherikov wrote:

return S(s);


return S(s.dup);


The variadic lives in a temporary array that expires at the end 
of the function. So copying it out to the GC lets it live on.


Your code was wrong on 2.099 too, but the compiler didn't tell 
you.




Re: How to fix "typesafe variadic function parameter"?

2022-05-24 Thread Andrey Zherikov via Digitalmars-d-learn

On Tuesday, 24 May 2022 at 22:51:50 UTC, Adam Ruppe wrote:

On Tuesday, 24 May 2022 at 22:46:55 UTC, Andrey Zherikov wrote:

return S(s);


return S(s.dup);


The variadic lives in a temporary array that expires at the end 
of the function. So copying it out to the GC lets it live on.


Your code was wrong on 2.099 too, but the compiler didn't tell 
you.


That works, thank you!

Can this error message be improved some way so this fix becomes 
obvious?


Re: How to fix "typesafe variadic function parameter"?

2022-05-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/24/22 6:54 PM, Andrey Zherikov wrote:

On Tuesday, 24 May 2022 at 22:51:50 UTC, Adam Ruppe wrote:

On Tuesday, 24 May 2022 at 22:46:55 UTC, Andrey Zherikov wrote:

return S(s);


return S(s.dup);


The variadic lives in a temporary array that expires at the end of the 
function. So copying it out to the GC lets it live on.


Your code was wrong on 2.099 too, but the compiler didn't tell you.


That works, thank you!

Can this error message be improved some way so this fix becomes obvious?


What is happening is that you have an `auto` return function, which 
infers attributes.


The parameter is being returned, which means it infers `return` on the 
parameter. Then decides that it's not legal.


The compiler error message should be more specific about inferred 
attribute errors and why they occurred, because they are super-confusing.


I believe there are some recent gains in this space. But it's not 100% yet.

-Steve