Bug 23105 – `__trait(getMember)` and `mixin()` of the same code as a string behave differently

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-05-12T09:30:03Z
Last change time
2022-06-25T00:31:54Z
Keywords
accepts-invalid, pull
Assigned to
No Owner
Creator
Basile-z

Comments

Comment #0 by b2.temp — 2022-05-12T09:30:03Z
``` module self; enum getMember = `__traits(getMember, sym, m)`; template test(alias sym) { static foreach (m; __traits(allMembers, sym)) { static if (is(__traits(getMember, sym, m)==module)) static assert(0); // pass static if (is(mixin(getMember)==module)) static assert(0); // fails } } void main() { alias _ = test!(self); } ``` the first static assert should fail the condition is the same as the second.
Comment #1 by b2.temp — 2022-05-12T09:33:41Z
note: the module is the automatic import of object.d so this test case cant be tested with -betterC.
Comment #2 by b2.temp — 2022-05-12T14:20:42Z
somewhat better. Static evaluation is known to have problems with shortcut && and ||: ``` module self; enum getMember = `__traits(getMember, sym, m)`; template test(alias sym) { static foreach (m; __traits(allMembers, sym)) { static if (m == "object") static if (is(__traits(getMember, sym, "object")==module)) static assert(0); // pass static if (m == "object") static if (is(mixin(getMember)==module)) static assert(0, m); // fails } } void main() { alias _ = test!(self); } ```
Comment #3 by dlang-bot — 2022-05-13T05:14:37Z
@BorisCarvajal created dlang/dmd pull request #14119 "Fix Issue 23105 - `__trait(getMember)` and `mixin()` of the same code as a string behave differently" fixing this issue: - Fix Issue 23105 - `__trait(getMember)` and `mixin()` of the same code as a string behave differently The fix is to remove the `SCOPE.alias_` dependency when resolving TypeTraits on certain conditions, it's not necessary, the trait resolves to type or symbol naturally. Changes: - Remove `SCOPE.alias_` as it is no longer used. - Remove a huge `if` with an early error from `TypeTraits` typeSemantic. - Cache not only symbols but also types in TypeTraits (TypeTraits.sym -> TypeTraits.obj). - Move `TypeTraits.typeSemantic` logic to `TypeTraits.resolve` since `typeSemantic` should only care about giving a type and `resolve` about maybe something else (like TypeMixin logic). https://github.com/dlang/dmd/pull/14119
Comment #4 by dlang-bot — 2022-05-13T06:49:31Z
dlang/dmd pull request #14119 "Fix Issue 23105 - `__trait(getMember)` and `mixin()` of the same code as a string behave differently" was merged into master: - acee742790929cf7fb3ea9817254708baa06f3e1 by Boris Carvajal: Fix Issue 23105 - `__trait(getMember)` and `mixin()` of the same code as a string behave differently The fix is to remove the `SCOPE.alias_` dependency when resolving TypeTraits on certain conditions, it's not necessary, the trait resolves to type or symbol naturally. Changes: - Remove `SCOPE.alias_` as it is no longer used. - Remove a huge `if` with an early error from `TypeTraits` typeSemantic. - Cache not only symbols but also types in TypeTraits (TypeTraits.sym -> TypeTraits.obj). - Move `TypeTraits.typeSemantic` logic to `TypeTraits.resolve` since `typeSemantic` should only care about giving a type and `resolve` about maybe something else (like TypeMixin logic). https://github.com/dlang/dmd/pull/14119
Comment #5 by dlang-bot — 2022-05-13T08:32:21Z
dlang/dmd pull request #14120 "Tiny follow up for pull #14119 (Issue 23105)" was merged into master: - 79d9c5b9114474ab8690f75b25a3dbbd0e7d3b9c by Boris Carvajal: Tiny follow up for pull #14119 (Issue 23105) Cache TypeTraits semantic on error too. https://github.com/dlang/dmd/pull/14120
Comment #6 by boris2.9 — 2022-06-25T00:31:54Z
*** Issue 23211 has been marked as a duplicate of this issue. ***