Is std.variant.visit not @nogc?

Adam D. Ruppe destructionator at gmail.com
Mon Apr 9 03:41:17 UTC 2018


On Monday, 9 April 2018 at 03:20:58 UTC, helxi wrote:
> Is std.variant.visit not @nogc?

These error messages REALLY need to be fixed.

visit, being a template, is @nogc or not based on the arguments 
passed to it as well as its own body, so while the error messages 
point to visit itself, these are frequently actually caused the 
predicate your pass.

....well, in this case, it is actually visit itself.

phobos/std/variant.d(2464): Error: @nogc function 
std.variant.visitImpl!(true, VariantN!(8u, int, string), function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visitImpl cannot call non- at nogc 
constructor std.variant.VariantException.this
phobos/std/variant.d(2469): Error: @nogc function 
std.variant.visitImpl!(true, VariantN!(8u, int, string), function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visitImpl cannot call non- at nogc 
function std.variant.VariantN!(8u, int, 
string).VariantN.peek!int.peek
phobos/std/variant.d(2469): Error: @nogc function 
std.variant.visitImpl!(true, VariantN!(8u, int, string), function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visitImpl cannot call non- at nogc 
function std.variant.VariantN!(8u, int, 
string).VariantN.peek!string.peek
phobos/std/variant.d(2173): Error: template instance 
`std.variant.visitImpl!(true, VariantN!(8u, int, string), 
function (string s) => printf("%s\x0a", cast(immutable(char)*)s), 
function (int n) => printf("%i\x0a", n))` error instantiating


Ugh, so unreadable even on this level, but at least the actual 
information is there:

std.variant.VariantException.this is not marked @nogc (but it 
prolly could be)

VariantN.peek is not @nogc because it calls Object.opEquals... 
which is broken af, sadly, but can probably be fixed for this 
case by marking TypeInfo.opEquals nogc.


I think the peek one is going to be the harder one to work around 
since any reimplementation of peek is probably going to still 
call it... though MAYBE you can use `!is` instead of `!=`... and 
any reimplementation of visit needs to check types.


But if you wanna try to work around it, I would copy the 
visitImpl and visit functions out of std.variant and do some 
adjustments, then call your version instead (which will be fairly 
easy btw since they are already UFCS).



FYI: the way I got these error messages was to go into the Phobos 
source and add @nogc to the lowest level template in the 
instantiation chain. Then just recompile your program - no need 
to recompile Phobos itself since they are templates.

I wish the error messages would just do this for you (simulate 
@nogc at the second-highest level) to keep you from having to 
edit it yourself just to know what it is.



More information about the Digitalmars-d-learn mailing list