Spurious "no property" error shown alongside opDispatch instantiation failure
Description
When a call to opDispatch fails to compile because of an error inside
the opDispatch template body (e.g. a static assert), dmd reports two
errors: a misleading "no property X" error, followed by the real error
from inside opDispatch.
The "no property" message is inaccurate — the property does exist and is found via opDispatch; it just fails to compile. This can confuse users into thinking they misspelled the member name, when the real problem is unrelated.
Code:
struct Proxy
{
auto opDispatch(string name)()
{
static assert(name != "forbidden", "cannot access forbidden member");
return 42;
}
}
void main()
{
Proxy p;
auto x = p.forbidden();
}
Output:
test.d(13): Error: no property `forbidden` for `p` of type `test.Proxy`
auto x = p.forbidden();
^
test.d(5): Error: static assert: "cannot access forbidden member"
static assert(name != "forbidden", "cannot access forbidden member");
^
test.d(13): instantiated from here: `opDispatch!"forbidden"`
auto x = p.forbidden();
^
Expected output:
Just the static assert error (and instantiation context), without the spurious "no property" line — since the property is not missing, it failed to compile.
Notes:
- Reproduces identically whether Proxy is a struct or a class.
- Only occurs when the opDispatch instantiation itself fails to compile; a successful call through opDispatch produces no error, as expected.
- This looks related to but distinct from Bugzilla 19181 ("Semantic errors in opDispatch argument lead to 'no property X'", fixed in 2.083.0), which was specifically about errors in the opDispatch template argument. This case is about an error inside the opDispatch body.
Tested on dmd 2.112.1.
Root cause found, but the fix isn't straightforward.
The spurious "no property" error comes from Type.noMember() in compiler/src/dmd/typesem.d (around line 6707), specifically this opDispatch-handling block:
const errors = gagError ? global.startGagging() : 0;
e = dti.dotTemplateSemanticProp(sc, DotExpFlag.none);
if (gagError && global.endGagging(errors))
e = null;
return returnExp(e);
When opDispatch is attempted under a gagged scope (gagError == true)
and its instantiation fails (e.g. due to a failing static assert
inside the opDispatch body), the result is discarded and replaced
with null — indistinguishable from "opDispatch doesn't apply to
this identifier at all". The caller (searchUFCS's call site in
expressionsem.d) then falls through to UFCS lookup, which also
fails, eventually reaching Type.getProperty() and printing the
spurious "no property" error. Only afterwards does a second,
ungagged evaluation path print the real error (the static assert
failure), producing the duplicate/misleading output.
I tried two fixes:
-
Re-running dotTemplateSemanticProp() ungagged on the same AST node when the gagged attempt fails, to get the real error and propagate ErrorExp (which the caller already handles correctly via
if (ey.op == EXP.error) return ey;). This crashes, because the first (gagged) run already mutates the expression node's semantic state in place; re-entering semantic on it is invalid. -
Same idea but rebuilding fresh AST nodes (including e.syntaxCopy()) for the retry, to avoid reusing already-processed nodes. This still fails — this time hitting dmd's internal "unknown, please file report" safety message from ErrorExp.get(). This happens because dmd caches/memoizes template instantiations: the second attempt resolves to the same (failed) instantiation as the first without re-running semantic or re-emitting any message, so the resulting ErrorExp has no accompanying error message at all.
In short: reliably distinguishing "opDispatch doesn't apply" from
"opDispatch applies but fails to instantiate" requires either (a) a
way to gag an attempt and still recover what error it would have
produced without re-running semantic (which dmd doesn't currently
have a general mechanism for — this looks related to the "store
gagged errors" mechanism discussed in #18626 / PR #15544), or (b)
tracking why the gagged attempt returned null (found-but-failed
vs. not-found) through an explicit signal instead of collapsing
both cases to null.
I don't think I can land a safe fix for this myself right now without a wider change to the gagging infrastructure. Leaving this documented here in case someone more familiar with that part of the compiler wants to pick it up.