dmd icon indicating copy to clipboard operation
dmd copied to clipboard

Lambda instantiation traces expose compiler-generated identifier (__lambda_LxxCyy!T)

Open usefahmed07 opened this issue 2 months ago • 4 comments

Description

Following up on #20261 (Fixed in #23835): the confusing compiler-generated identifier for lambdas can still leak through in a different place — the "instantiated from here" supplemental error line that's printed when a template instantiation fails.

Example

void main()
{
    immutable int c;
    int function(int) outer = (x) {
        int function(int) inner = (y) => y + c;
        return x;
    };
}

Current output:

Error: lambda function cannot access variable `c` in frame of function `D main`
        `c` declared here
        instantiated from here: `__lambda_L4_C31!int`

The main error line was already fixed by #23835, but the supplemental "instantiated from here" line (printed by TemplateInstance.printInstantiationTrace in compiler/src/dmd/dtemplate.d) still exposes the internal identifier __lambda_L4_C31!int.

Expected

The supplemental line should avoid exposing the compiler-generated name, similar to how the main error message was fixed, e.g.:

instantiated from here: `lambda function`

Notes

This affects all three code paths in printInstantiationTrace that print an instantiation trace entry (the normal case, the collapsed-recursion case, and the "too many instantiations" case), since all three call toChars()/toErrMsg() on the TemplateInstance, which for a lambda-generated template resolves to the internal identifier.

usefahmed07 avatar Sep 12 '26 13:09 usefahmed07

How would a generic lambda function be any better than __lambda_L4_C31!int? The latter at least shows what it was instantiated with (template params), and a line number.

kinke avatar Sep 12 '26 17:09 kinke

How would a generic lambda function be any better than __lambda_L4_C31!int? The latter at least shows what it was instantiated with (template params), and a line number.

Fair point — you're right, this isn't the same as #20261. There the name was just a counter + a generic placeholder with no real info. Here __lambda_L4_C31!int actually shows the location and the real type, so it's useful as-is.

What would you suggest instead? Happy to adjust or close if you think it's not needed.

usefahmed07 avatar Sep 12 '26 17:09 usefahmed07

I'd tend towards closing this.

kinke avatar Sep 12 '26 18:09 kinke

I'd tend towards closing this.

Understood, thanks for the feedback and the explanation — closing it.

usefahmed07 avatar Sep 12 '26 18:09 usefahmed07