Aliases to mutable thread-local data not allowed

Steven Schveighoffer schveiguy at gmail.com
Tue Mar 10 15:27:04 UTC 2020


On 3/10/20 7:09 AM, mark wrote:
> I just tried:
> 
>              auto jobs = tids.length;
>              while (jobs) {
>                  receive(
>                      (Deb deb) { debForName[deb.name] = cast(Deb)deb; },
>                      (DoneMessage m) { jobs--; }
>                  );
>              }
> 
> to cast away the immutable from the sender, but that doesn't work either:

You aren't accepting an immutable(Deb), but a Deb. So you aren't 
actually casting away anything.

But this seems more like it would be a runtime error (std.concurrency 
can't find any function that handles the immutable(Deb) e.g.).

Still, the correct thing here is to handle immutable(Deb). However, I'd 
strongly caution you against casting away immutable, that can lead to 
undefined behavior in D. Better to just store it as immutable to begin with.

> src/model.d(85,30): Error: template std.concurrency.spawn cannot deduce 
> function from argument types !()(void delegate(Tid parentTid, string 
> filename), Tid, string), candidates are:
> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(460,5):        
> spawn(F, T...)(F fn, T args)
>    with F = void delegate(Tid, string),
>         T = (Tid, string)
>    must satisfy the following constraint:
>         isSpawnable!(F, T)
> src/model.d(86,13): Warning: statement is not reachable
> src/model.d(87,13): Warning: statement is not reachable
> src/model.d(86,13): Warning: statement is not reachable
> src/model.d(87,13): Warning: statement is not reachable

Hard to see what the problem is here, it looks like you have a function 
that has unreachable statements, which is why the spawn isn't compiling 
(the function you are passing it can't compile).

> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/variant.d(701,26): 
> Error: cannot implicitly convert expression rhs of type immutable(Deb) 
> to Deb
> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/variant.d(603,17): 
> Error: template instance 
> std.variant.VariantN!32LU.VariantN.opAssign!(immutable(Deb)) error 
> instantiating
> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(126,22):        
> instantiated from here: __ctor!(immutable(Deb))
> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(656,23):        
> instantiated from here: __ctor!(immutable(Deb))
> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(647,10):        
> instantiated from here: _send!(immutable(Deb))
> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(626,10):        
> instantiated from here: _send!(immutable(Deb))
> src/model.d(115,21):        instantiated from here: send!(immutable(Deb))
> /home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/ldc2 failed with exit code 1.

This looks like an error in std.concurrency (or std.variant), and I'm 
not sure what is the problem here. It seems std.variant cannot accept an 
immutable, which makes no sense as immutable data is a huge usecase of 
std.concurrency.

Can you narrow this down to a small example that can be submitted as a 
bug report?

-Steve


More information about the Digitalmars-d-learn mailing list