Issue 11907 - No undeclared identifier error in lambda alias argument
Summary: No undeclared identifier error in lambda alias argument
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: diagnostic
: 23663 (view as issue list)
Depends on:
Blocks:
 
Reported: 2014-01-12 03:02 UTC by Peter Alexander
Modified: 2023-02-01 21:58 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Peter Alexander 2014-01-12 03:02:46 UTC
void f(alias a)() if (is(typeof(a()))) {}

void main()
{
    f!(x => blarg);
}


This gives the error:

foo.d(5): Error: template instance f!((x) => blarg) does not match template declaration f(alias a)() if (is(typeof(a())))

Ideally, the error would be:

foo.d(5): Error: undeclared identifier 'blarg'


This comes up all the time when using predicates on Phobos functions when you forget an import or just have a typo.
Comment 1 basile-z 2020-07-04 17:36:22 UTC
the diagnostic is better nowdays:

---
/tmp/temp_7F0C787490B0.d(5,5): Error: template instance `temp_7F0C787490B0.main.f!((x) => blarg)` does not match template declaration `f(alias a)()`
  with `a = __lambda1`
  must satisfy the following constraint:
`       is(typeof(a()))`
---

good enough to close ?
Comment 2 Nick Treleaven 2023-02-01 21:41:16 UTC
*** Issue 23663 has been marked as a duplicate of this issue. ***
Comment 3 Nick Treleaven 2023-02-01 21:52:48 UTC
Note: issue also applies when constraint is `__traits(compiles, a())`.

> Ideally, the error would be: foo.d(5): Error: undeclared identifier 'blarg'

The problem is that the lambda is a template, and templates aren't semantically analyzed until instantiated. If the constraint wasn't there then the body would likely instantiate the lambda somewhere and produce the desired error. In this case the lambda is trivial and some kind of analysis without types could detect the error, but not in the general case where the lambda doesn't compile for other reasons, e.g. `x => x.typo`. As a workaround, the constraint expression could be moved to a `static if` test in the body.