@safe function causes attribute inference since 2.072
Steven Schveighoffer
schveiguy at gmail.com
Mon Apr 13 15:17:24 UTC 2020
I'm trying to fix a bug in phobos, and I was perplexed when the type I
defined inferred constructor attributes in my unittest. It looks like this:
@safe unittest
{
static struct A
{
int x;
this(scope ref return const A other) {}
}
}
I was surprised that the copy constructor is further inferred to be
pure, @nogc, nothrow.
I actually wanted it to be not inferred because that is part of the bug
I'm fixing. After much head scratching, I discovered that it's the @safe
tag on the unit test.
Observe:
void foo1()
{
static struct S { void bar() {} }
pragma(msg, typeof(S.bar)); // void()
}
@safe void foo2()
{
static struct S { void bar() {} }
pragma(msg, typeof(S.bar)); // pure nothrow @nogc @safe void()
}
It has been this way since 2.072 (prior to that, foo2.S.bar is just
@safe void()). I don't see a changelog that shows this to be intended.
Should this be a bug? I think at least the fact that inference only
happens on structs inside @safe functions doesn't make sense (why not
pure, @nogc nothrow functions).
But if we "fix" the bug, we risk breaking a lot of code.
-Steve
More information about the Digitalmars-d
mailing list