Bug 23279 – Segmentation fault on mixin template + using unknown type

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-08-01T02:13:01Z
Last change time
2023-07-15T15:30:02Z
Keywords
ice, pull
Assigned to
No Owner
Creator
Marcelo Silva Nascimento Mancini

Comments

Comment #0 by msnmancini — 2022-08-01T02:13:01Z
This is an example code of what is causing a segmentation fault: ```d template getParams (alias fn) { static if ( is(typeof(fn) params == __parameters) ) alias getParams = params; } template hasMethod(T, string method, Params...) { enum hasMethod = __traits(hasMember, T, method) && is(getParams!(__traits(getMember, T, method)) == Params); } mixin template TestMx() { static if(hasMethod!( typeof(this), "setIt", Tester)){} } class Tester { mixin TestMx; void setIt(Sth sth){} } ``` I don't know (or didn't test) on how to further reduce that, but it seems small enough. The error occurs when the mixin TestMx is called.
Comment #1 by msnmancini — 2022-08-01T02:17:36Z
This error occurs only on DMD with this test code. But in my project it does not work in both, and I do get "Error: unknown, please file report on issues.dlang.org" instead of a segmentation fault. But 99% chance they are related
Comment #2 by msnmancini — 2022-08-01T02:25:01Z
So, the "unknown error" message seems to be caused when that program is built with the unknown type available and it generates the binary for it then the program seems to abort. When I said that LDC doesn't suffer from this problem I was actually wrong in my statement, it builds and run with the unknown type in the code (probably because it strips something out)
Comment #3 by razvan.nitu1305 — 2022-08-04T12:50:09Z
Reduced: ``` class Tester { enum a = __traits(hasMember, Tester, "setIt"); void setIt(Sth sth){} } ```
Comment #4 by razvan.nitu1305 — 2022-08-04T12:51:38Z
It seems like `__traits(hasMember)` gets confused by the forward reference. Putting the `traits(hasMember)` it after the declaration of `setIt` does not result in an ice.
Comment #5 by b2.temp — 2023-01-02T13:47:32Z
The thing is that for `__traits(hasMember, Tester, "setIt")`, DMD gags the errors during semantic of `Tester.setIt`. ``` if (e.ident == Id.hasMember) { /* Take any errors as meaning it wasn't found */ ex = ex.trySemantic(scx); // try == gag return ex ? True() : False(); } ``` During that same step, the semantic pass for the bad function is also executed (since it's a forward ref) but the error for the bad identifier is just ignored and compilation continues. Finally DMD reaches the code in glue.d and then an assertion fails.
Comment #6 by dlang-bot — 2023-01-03T07:17:37Z
@SixthDot created dlang/dmd pull request #14778 "fix issue 23279 - `hasMember` should not gag sema of forward references" fixing this issue: - fix issue 23279 - `hasMember` should not gag sema of forward references `__traits(hasMember, e1, "e2")` works by looking if `e1.e2` is a valid expression, gagging the semantic errors of the whole expression, however that should just be done while `e2` is being analyzed. https://github.com/dlang/dmd/pull/14778
Comment #7 by dlang-bot — 2023-07-13T12:51:21Z
@RazvanN7 updated dlang/dmd pull request #15406 "Fix Issue 23951 - traits(getMember) does not follow alias this" fixing this issue: - Fix Issue 23279 - ICE when using traits(hasMember) with an erroneous member https://github.com/dlang/dmd/pull/15406
Comment #8 by dlang-bot — 2023-07-14T11:34:12Z
dlang/dmd pull request #15406 "Fix Issues 23951 and 23279 - traits(hasMember) does not follow alias this + ICE when using traits(hasMember) on an erroneous member" was merged into stable: - 1ad55f16b699d077c05034fe0678a5a05a73ed8a by RazvanN7: Fix Issue 23279 - ICE when using traits(hasMember) with an erroneous member https://github.com/dlang/dmd/pull/15406
Comment #9 by dlang-bot — 2023-07-15T15:30:02Z
dlang/dmd pull request #15417 "merge stable" was merged into master: - 452e170d2ad0c547dd1a4fe45e21af0cde87565c by Razvan Nitu: Fix Issues 23951 and 23279 - traits(hasMember) does not follow alias this + ICE when using traits(hasMember) on an erroneous member (#15406) * Fix Issue 23951 - traits(getMember) does not follow alias this * Fix Issue 23279 - ICE when using traits(hasMember) with an erroneous member https://github.com/dlang/dmd/pull/15417