Bug 14170 – `this` compiles in a static context

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-02-12T00:11:30Z
Last change time
2024-12-13T18:40:19Z
Assigned to
No Owner
Creator
Mike Franklin
See also
https://issues.dlang.org/show_bug.cgi?id=12228, https://issues.dlang.org/show_bug.cgi?id=6579
Moved to GitHub: dmd#18946 →

Comments

Comment #0 by slavo5150 — 2015-02-12T00:11:30Z
This code compiles and executes: ----------------------------------- import std.stdio; struct StaticRegister { static private uint _value; @property static uint value() { return _value; } @property static void value(uint v) { _value = v; } static alias value this; // (1) static void test() { writeln(this.stringof); // (2) writeln(typeof(this).stringof); // (3) writeln(this.value); // (4) } } void main(string[] s) { // works due to `alias value this` StaticRegister = 1; StaticRegister.test(); } ----------------------------------- I suspect (1), (2), (3), and (4) should all generate compiler errors. `static alias value this` and `alias value this` seem to be semantically the same thing, but I can't be sure. I attempted to understand the meaning of `this` in a static context on the forum... http://forum.dlang.org/post/[email protected] http://forum.dlang.org/post/[email protected] ...but I was unable to elicit a definitive answer. One member of the community believes (3) is valid, and the rest are not. If that is the case, it needs to be documented in the language specification at http://dlang.org/expression.html#this. At the moment the spec is silent on the subject. Issue #380 is a D1 bug that was fixed to specifically allow `this` in a static context, but I'm not sure if it applies to D2.
Comment #1 by simen.kjaras — 2018-05-14T13:25:51Z
(1) is a case of DMD ignoring a redundant keyword - 'static' has no effect there, so it's just ignored. It's been mentioned that this is due to static blocks in aggregates, but I'm unsure of the exact details: struct S { static { // or just static: // lots of declarations alias value this; } } (3) is explicitly mentioned in https://dlang.org/spec/declaration.html#typeof: Special cases: typeof(this) will generate the type of what this would be in a non-static member function, even if not in a member function. (2) and (4) are bug 6579. Possibly a bit more than that, since they don't have an actual instance to work with, but that's the root.
Comment #2 by robert.schadek — 2024-12-13T18:40:19Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18946 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB