Inferred attributes errors in template function.

vit vit at vit.vit
Fri May 27 08:39:08 UTC 2022


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`
}
```


More information about the Digitalmars-d-learn mailing list