Skip to content

Commit 38329ba

Browse files
Merge pull request #4192 from jmdavis/issue_4191
Fix issue #4191: The spec should warn against using mixin(__FUNCTION__)
2 parents d9ca981 + 7e44db1 commit 38329ba

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

spec/expression.dd

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,6 +3676,27 @@ function: 'test.main', pretty function: 'int test.main(string[] args)',
36763676
file full path: '/example/test.d'
36773677
)
36783678

3679+
$(PANEL $(P $(RED Warning): Do not use $(D mixin(__FUNCTION__)) to get the
3680+
symbol for the current function. This seems to be a common
3681+
thing for programmers to attempt when trying to get the symbol
3682+
for the current function in order to do introspection on it,
3683+
since D does not currently have a direct way to get that
3684+
symbol. However, using $(D mixin(__FUNCTION__)) means that the
3685+
symbol for the function will be looked up by name, which means
3686+
that it's subject to the various rules that go with symbol
3687+
lookup, which can cause various problems. One such problem
3688+
would that if a function is overloaded, the result will be the
3689+
first overload whether that's the current function or not.)
3690+
3691+
$(P Given that D doesn't currently have a way to directly get the
3692+
symbol for the current function, the best way to do it is to
3693+
get the parent symbol of a symbol within the function, since
3694+
that avoids any issues surrounding symbol lookup rules. An
3695+
example of that which doesn't rely on any other symbols is
3696+
$(D __traits(parent {})). It declares an anonymous, nested
3697+
function, whose parent is then the current function. So,
3698+
getting its parent gets the symbol for the current function.))
3699+
36793700
$(H2 $(LNAME2 associativity, Associativity and Commutativity))
36803701

36813702
$(P An implementation may rearrange the evaluation of expressions

0 commit comments

Comments
 (0)