Bug 10008 – Inconsistent inference of template and non-template function attributes

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-29T06:12:00Z
Last change time
2015-06-09T01:31:21Z
Assigned to
nobody
Creator
maxim

Comments

Comment #0 by maxim — 2013-04-29T06:12:26Z
struct S() { void foo(){} } struct Z { void foo(){} } void main() @safe { auto x = &Z.foo; auto y = &S!().foo; pragma(msg, typeof(x)); pragma(msg, typeof(y)); x(); y(); } Member function of templated struct is deduced to be pure nothrow @safe while non-templated struct function is not. Either both should be rejected or none of them.
Comment #1 by k.hara.pg — 2013-04-29T07:10:35Z
This is a new D feature from 2.063. For the *instantiated* non-virtual member functions, compiler now infers their function attributes as same as normal template functions. This is useful template based structures, such as std.algorithm and std.range. Now, most of ranges and algorithms would work in safe/pure/nothrow functions. import std.algorithm, std.range; void main() @safe pure nothrow { auto r = [1,2,3].map!(a => a*2).retro; }
Comment #2 by maxim — 2013-04-29T10:10:56Z
OK, this explains why function from templated struct is deduced to have all attributes. But this tells nothing about another case. I understand that Z.foo is not required to be @safe/pure/nothrow because it isn't marked as such, but on the other hand S.foo() is also not marked and clearly there is contradiction here.
Comment #3 by k.hara.pg — 2013-04-29T17:26:09Z
(In reply to comment #2) > OK, this explains why function from templated struct is deduced to have all > attributes. But this tells nothing about another case. I understand that Z.foo > is not required to be @safe/pure/nothrow because it isn't marked as such, but > on the other hand S.foo() is also not marked and clearly there is contradiction > here. Short answer is that it is just same as the difference between: void foo() {} and void bar()() {} Both don't have attributes explicitly, but bar would be inferred to @safe/pure/nothrow when it is instantiated. --- There is an essential difference between template functions and non-template ones. For the former, the actual function attributes of instantiated code is not pre-deterministic. Ultimately, it would depend on the arguments which is given on the template parameters. Attribute inference for the instantiated functions is necessary for making function attributes more convenient. On the other hand, the functions out of the template declaration should be "as is". You can hide the function body in the library file for the separate compilation, and just only declaring function signatures in di file. Therefore, we cannot/should not apply attribute inference for normal functions. The contradiction you feel comes from the essential difference that I described in above. Thanks.
Comment #4 by maxim — 2013-04-29T19:25:37Z
I see, mark it as RESOLVED INVALID.