@safe function causes attribute inference since 2.072

jeckel jeckel12381236 at gmail.com
Mon Apr 13 19:11:58 UTC 2020


On Monday, 13 April 2020 at 15:17:24 UTC, Steven Schveighoffer 
wrote:
> 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

I'd say it's a feature. And I'd honestly prefer it to be expanded 
to regular functions as well. Rather than having to deal with the 
change all the defaults DIPs. I usually come across a function in 
phobos that is @nogc and pure, someone just forgot to annotate it 
with those attributes. If it was auto inferred this wouldn't be a 
problem, it knows whether it is @nogc and pure, it should just 
add those attributes itself.


More information about the Digitalmars-d mailing list