Bug 18097 – [REG2.077] Unittest function is undefined identifier

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-12-17T20:06:01Z
Last change time
2018-02-01T23:17:02Z
Keywords
industry
Assigned to
No Owner
Creator
johanengelen

Comments

Comment #0 by johanengelen — 2017-12-17T20:06:01Z
Pre 2.077, this code worked: ``` unittest // first unittest { } unittest // second unittest { auto a = &mixin(__traits(identifier, __traits(parent, { }))); auto b = &__traits(parent, { }); //auto c = &__unittestL3_2; // dlang 2.076 //auto c = &__unittest_dummy_d_3_2; //dlang 2.077 } ``` The purpose of the code is to obtain the address of the unittest function. Either `a` or `b` work, and directly naming the unittest function also works (`c`). With 2.077, we get the error: ``` dummy.d-mixin-5(5): Error: undefined identifier __unittest_dummy_d_3_1, did you mean function __unittest_dummy_d_3_1? ``` This regression was probably introduced by https://github.com/dlang/dmd/pull/6727 , where the symbol name of unittest function is set during semantic analysis probably too late for this identifier resolution? Note: the first unittest is needed to trigger the bug.
Comment #1 by atila.neves — 2017-12-22T20:14:00Z
It's probably due to using an "old" identifier. With the change you mentioned that introduced the bug, I had to set an identifier for the unittest functions before semantic analysis. I didn't understand why, but tests failed and all hell broke loose. I think I do now. Basically there's no scope when the `UnitTestDeclaration` is created, and then when semantic analysis happens the scope is there and a number counting the unittests is added to the name. The only reason for this is to account for the pathological case where a user defines two unittests on the same line, which is unlikely to happen but can. And that's the reason the 1st unittest has to be present for the bug to take effect - before semantic analysis the final digits after the last underscore are all `0`. After, they change. A (horrible, awful) workaround for now is to change auto c = &__unittest_dummy_d_3_2; //dlang 2.077 to auto c = &__unittest_dummy_d_3_0; //dlang 2.077 It's not the name of the symbol that gets put into the object file, but it's the name dmd thinks it has at that point.
Comment #2 by atila.neves — 2017-12-27T20:30:12Z
I have a potential fix but am waiting on another PR of mine since there will be merge conflicts.
Comment #3 by github-bugzilla — 2018-02-01T23:16:59Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/cab17a8744f920d083ff6afdfe5a3a06961ace56 Fix issue 18097 - unittest symbol names can be used before semantic pass https://github.com/dlang/dmd/commit/e2b75ef17e2640ea2123243b82491d11c07a9848 Merge pull request #7761 from atilaneves/fix_18097 Fix issue 18097 - unittest symbol names can be used before semantic merged-on-behalf-of: Iain Buclaw <[email protected]>