Bug 23753 – this allowed in static member function

Status
NEW
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-02-28T23:02:02Z
Last change time
2024-12-13T19:27:30Z
Keywords
accepts-invalid, pull
Assigned to
No Owner
Creator
johanengelen
Moved to GitHub: dmd#20239 →

Comments

Comment #0 by johanengelen — 2023-02-28T23:02:02Z
Testcase: ``` void g(int); struct S { size_t member; static void foo(byte member) { g(this.member.sizeof); // <--- } } ``` The code accepted by the compiler, but shouldn't be: `this` has no meaning in a _static_ member function.
Comment #1 by razvan.nitu1305 — 2023-03-01T11:37:49Z
It seems that this is done intentionally [1], although I couldn't find anything in the spec. It seems that all compilers on run.dlang.io run this successfully. Is there a strong argument to not allow this (and justify the breakage) or should we simply amend the spec? I, personally, don't see any harm in allowing it. Note that this only works because you are using sizeof (which is instance agnostic). Any other uses of the concrete member will result in an error. [1] https://github.com/dlang/dmd/blob/master/compiler/src/dmd/expressionsem.d#L12735
Comment #2 by johanengelen — 2023-03-14T11:16:57Z
your link is to a moving target, please post the permalink (using git commit hash rather than master branch)
Comment #3 by johanengelen — 2023-03-14T11:20:53Z
(In reply to RazvanN from comment #1) > > Is there a strong argument to not allow this (and justify the breakage) or > should we simply amend the spec? Yes, the language should have a clear structured grammar, rather than accepting hacks that appear convenient. See here for similar removal of hacky constructs: https://dlang.org/changelog/2.100.0.html#super_type_gone
Comment #4 by razvan.nitu1305 — 2023-03-14T13:13:53Z
(In reply to johanengelen from comment #2) > your link is to a moving target, please post the permalink (using git commit > hash rather than master branch) Sorry about that: https://github.com/dlang/dmd/pull/7031/files#diff-1d23dd0cbd2bf1ed91655a6f65b94deae595ef0ea72fedb61c14d803ba0fbcceR8868
Comment #5 by dlang-bot — 2023-03-14T16:19:26Z
@RazvanN7 created dlang/dmd pull request #14986 "Fix Issue 23753 - this allowed in static member function" fixing this issue: - Fix Issue 23753 - this allowed in static member function https://github.com/dlang/dmd/pull/14986
Comment #6 by maxsamukha — 2023-03-14T19:06:06Z
(In reply to johanengelen from comment #3) > > Yes, the language should have a clear structured grammar, rather than > accepting hacks that appear convenient. See here for similar removal of > hacky constructs: https://dlang.org/changelog/2.100.0.html#super_type_gone I am not sure about that. There's been a long-standing tradition that allows accessing static members via instance references. struct S { static void bar() {} } void main() { S s; s.bar(); // works but could fail static void foo() { s.bar(); // fails but could work } }
Comment #7 by robert.schadek — 2024-12-13T19:27:30Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20239 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB