Inferred attributes errors in template function.
Tejas
notrealemail at gmail.com
Fri May 27 13:40:35 UTC 2022
On Friday, 27 May 2022 at 08:39:08 UTC, vit wrote:
> Hello, I have this problem:
>
> ```d
> static int i;
>
> void bar(T)(){
> static if(is(T == int))
> (()@system => 1)();
> static if(is(T == float))
> i = 42;
>
> }
> void foo(T)(){
> bar!T();
> }
>
> void main()@safe pure{
> foo!long();
> foo!float(); //Error: `pure` function `D main` cannot call
> impure function `onlineapp.foo!float.foo`
> foo!int(); //Error: `@safe` function `D main` cannot call
> `@system` function `onlineapp.foo!int.foo`
> }
> ```
>
> When template function foo is called and its inferred
> attributes are not compatible with attributes of main, errors
> are not really useful. Compiler print that foo!float is not
> pure or foo!int is not @safe but doesn't tell why. Is in dmd
> some flag that print errors similarly to this?:
>
> ```d
> void main()@safe pure{
> foo!long();
> foo!float();
> //Error: `pure` function `D main` cannot call impure
> function `onlineapp.foo!float.foo`
> //Error: potentially `pure` function
> `onlineapp.foo!float.foo` cannot call impure function
> `onlineapp.bar!float.bar`
> //Error: potentially `pure` function
> `onlineapp.bar!float.bar` cannot access mutable static data `i`
> foo!int();
> //Error: `@safe` function `D main` cannot call `@system`
> function `onlineapp.foo!int.foo`
> //Error: potentially `@safe` function
> `onlineapp.foo!int.foo` cannot call `@system` function
> `onlineapp.bar!int.bar`
> //Error: potentially `@safe` function
> `onlineapp.bar!int.bar` cannot call `@system` delegate
> `onlineapp.bar!int.bar.__lambda1`
> }
> ```
Use `-verrors=context` for dmd
```d
static int i;
void bar(T)(){
static if(is(T == int))
(()@system => 1)();
static if(is(T == float))
i = 42;
}
void foo(T)(){
bar!T();
}
void main()@safe pure{
foo!long();
foo!float(); /+ onlineapp.d(16): Error: `pure` function `D main`
cannot call impure function `onlineapp.foo!float.foo`
foo!float();
^ +/
foo!int(); /+onlineapp.d(18): Error: `@safe` function `D main`
cannot call `@system` function `onlineapp.foo!int.foo`
foo!int();
^ +/
}
```
More information about the Digitalmars-d-learn
mailing list