Initialising Context Pointer during Semantic Analysis

Timon Gehr timon.gehr at gmx.ch
Sat Nov 26 18:12:44 UTC 2022


On 11/25/22 17:10, Teodor Dutu wrote:
> 
> After discussing with my mentors, we have come up with 2 possible 
> approaches:

Probably the right approach is to lower the call to a local function, 
e.g., with local template instantiation.

This is a somewhat hacky way to achieve that without changing the 
compiler frontend:

auto make(T,alias dummy)(){
     return T(); // can do your emplace magic here
}

auto foo(){
     int x=123;
     struct S{
         int bar(){ return x; }
     }
     void[0] context;
     return make!(S,context)();
}


void main(){
     import std.stdio;
     writeln(foo().bar()); // 123
}


TBH, I think local template instantiation should work even without that 
dummy parameter and with an arbitrary number of contexts.


General remark: I think the whole point of this project is to get rid of 
compiler magic, so instead of introducing compiler magic and 
magic-UB-that-works-anyway, maybe propose general language features to 
cover the missing use cases. Not only compiler developers may want to 
hook into builtin behavior.


More information about the Digitalmars-d mailing list