Skip to content

Fix issue 20261 - lambda function error message is confusing - #23835

Open
usefahmed07 wants to merge 1 commit into
dlang:masterfrom
usefahmed07:fix-issue-20261
Open

usefahmed07 wants to merge 1 commit into
dlang:masterfrom
usefahmed07:fix-issue-20261

Conversation

@usefahmed07

Copy link
Copy Markdown
Contributor

Fixes #20261

When a function literal's parameter types are inferred (e.g. 'x => c'
with no explicit types), it gets compiled as a template internally.
The error for accessing an outer variable then exposed the compiler-
generated identifier (e.g. __lambda3(__T1)) via toPrettyChars(), and
mislabeled it as a plain 'function' when it was actually a template
declaration at that point.

Now, when the inaccessible-frame error is reported for such a
templated function literal, the message says 'lambda function'
instead of surfacing the internal name.

Fixes https://issues.dlang.org/show_bug.cgi?id=23847
@kinke

kinke commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

I don't think this is an improvement, let alone totally inconsistent if only done here, or selectively for a few other messages.

@usefahmed07
usefahmed07 deleted the fix-issue-20261 branch September 12, 2026 18:10
@usefahmed07
usefahmed07 restored the fix-issue-20261 branch September 12, 2026 18:11
@usefahmed07 usefahmed07 reopened this Sep 12, 2026
@usefahmed07

Copy link
Copy Markdown
Contributor Author

Contributor

Thanks for the feedback. Just to understand better — do you think the original __lambda3(__T1) name was fine as-is, or is there a different way you'd want it changed (keeping the location/type info but dropping just the compiler-generated parts)?

@kinke

kinke commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

My main problem is the inconsistency - either we print all lambdas in a generic fashion without the internal identifier, with its line+column suffix, everywhere (for all diagnostics, and implemented only once), or we keep things as is - which I'd prefer, since the line number can be handy at times (it's the line where the lambda starts).

The original #20261 is about the internal counter suffix that we used back then, instead of that line+column suffix nowadays. So I think it's invalid nowadays, even though it's certainly confusing that for that specific error message, the template declaration seems to be printed, not the template instance, where the T1 type parameter would be bound to a specific type. Printing the full lambda as suggested in that original issue is IMO not an option, since a lambda can stretch over many lines.

@kinke

kinke commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Looking again at the original code in the issue, I doubt that printing the template instance is easily feasible here - inferring the type for untyped parameter x requires the assignment to the function pointer (expecting an int param), but the error might likely occur earlier, while checking the lambda body and coming across the invalid use of the captured local.

But feel free to try to improve that. :)

@usefahmed07

Copy link
Copy Markdown
Contributor Author

Looking again at the original code in the issue, I doubt that printing the template instance is easily feasible here - inferring the type for untyped parameter x requires the assignment to the function pointer (expecting an int param), but the error might likely occur earlier, while checking the lambda body and coming across the invalid use of the captured local.

But feel free to try to improve that. :)

Tried it — it actually works. Added a debug printf and confirmed the TemplateInstance's tiargs are already resolved by the time the error fires.

For example, with:

int function(int, int) fp = (x, y) => x + y + c;

Current output shows the unbound template declaration:

__lambda_L4_C33(__T1, __T2)(x, y)

Switching toPrettyChars() to use the instance instead of the declaration gives:

__lambda_L4_C33!(int, int)

Location is kept, and the placeholder types (__T1, __T2) become the actual resolved ones (int, int).

I'll push this to this PR instead of the generic "lambda function" approach, unless you'd prefer something different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error with regards to lambda function is confusing

2 participants