Skip to content

Commit

Permalink
Improve error for failing opDispatch instantiation (#20902)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel authored Feb 20, 2025
1 parent f88520c commit 54a1424
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
22 changes: 19 additions & 3 deletions compiler/src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -3461,10 +3461,26 @@ Expression getProperty(Type t, Scope* scope_, Loc loc, Identifier ident, int fla
{
if (auto sym = dsym.isAggregateDeclaration())
{
if (auto fd = search_function(sym, Id.opDispatch))
errorSupplemental(loc, "potentially malformed `opDispatch`. Use an explicit instantiation to get a better error message");
else if (!sym.members)
if (!sym.members)
{
errorSupplemental(sym.loc, "`%s %s` is opaque and has no members.", sym.kind, mt.toPrettyChars(true));
return ErrorExp.get();
}

if (auto fd = search_function(sym, Id.opDispatch))
{
if (auto td = fd.isTemplateDeclaration())
{
e = mt.defaultInitLiteral(loc);
auto se = new StringExp(e.loc, ident.toString());
auto tiargs = new Objects();
tiargs.push(se);
auto dti = new DotTemplateInstanceExp(e.loc, e, Id.opDispatch, tiargs);
dti.ti.tempdecl = td;
dti.dotTemplateSemanticProp(scope_, DotExpFlag.none);
return ErrorExp.get();
}
}
}
errorSupplemental(dsym.loc, "%s `%s` defined here",
dsym.kind, dsym.toChars());
Expand Down
8 changes: 3 additions & 5 deletions compiler/test/fail_compilation/diag14145.d
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag14145.d(16): Error: no property `i` for `_` of type `diag14145.main.Capture!(i)`
fail_compilation/diag14145.d(16): potentially malformed `opDispatch`. Use an explicit instantiation to get a better error message
fail_compilation/diag14145.d(26): struct `Capture` defined here
fail_compilation/diag14145.d(35): Error: expression `*this.ptr` of type `shared(int)` is not implicitly convertible to return type `ref int`
fail_compilation/diag14145.d(17): Error: template instance `diag14145.main.Capture!(i).Capture.opDispatch!"i"` error instantiating
fail_compilation/diag14145.d(14): Error: no property `i` for `_` of type `diag14145.main.Capture!(i)`
fail_compilation/diag14145.d(33): Error: expression `*this.ptr` of type `shared(int)` is not implicitly convertible to return type `ref int`
fail_compilation/diag14145.d(14): Error: template instance `diag14145.main.Capture!(i).Capture.opDispatch!"i"` error instantiating
---
*/

Expand Down
8 changes: 4 additions & 4 deletions compiler/test/fail_compilation/fail18970.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
TEST_OUTPUT:
---
fail_compilation/fail18970.d(26): Error: no property `y` for `S()` of type `fail18970.S`
fail_compilation/fail18970.d(26): potentially malformed `opDispatch`. Use an explicit instantiation to get a better error message
fail_compilation/fail18970.d(15): struct `S` defined here
fail_compilation/fail18970.d(19): Error: undefined identifier `x`
fail_compilation/fail18970.d(26): Error: template instance `fail18970.S.opDispatch!"y"` error instantiating
fail_compilation/fail18970.d(33): Error: no property `yyy` for `this` of type `fail18970.S2`
fail_compilation/fail18970.d(33): potentially malformed `opDispatch`. Use an explicit instantiation to get a better error message
fail_compilation/fail18970.d(29): struct `S2` defined here
fail_compilation/fail18970.d(38): Error: undefined identifier `x`
fail_compilation/fail18970.d(33): Error: template instance `fail18970.S2.opDispatch!"yyy"` error instantiating
---
*/

Expand Down
10 changes: 4 additions & 6 deletions compiler/test/fail_compilation/fail22054.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail22054.d(23): Error: no property `what` for type `fail22054.exception`
fail_compilation/fail22054.d(18): `class fail22054.exception` is opaque and has no members.
fail_compilation/fail22054.d(18): class `exception` defined here
fail_compilation/fail22054.d(24): Error: no property `what` for type `fail22054.exception2`
fail_compilation/fail22054.d(19): `struct fail22054.exception2` is opaque and has no members.
fail_compilation/fail22054.d(19): struct `exception2` defined here
fail_compilation/fail22054.d(21): Error: no property `what` for type `fail22054.exception`
fail_compilation/fail22054.d(16): `class fail22054.exception` is opaque and has no members.
fail_compilation/fail22054.d(22): Error: no property `what` for type `fail22054.exception2`
fail_compilation/fail22054.d(17): `struct fail22054.exception2` is opaque and has no members.
---
*/

Expand Down
4 changes: 2 additions & 2 deletions compiler/test/fail_compilation/ice10938.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
TEST_OUTPUT:
---
fail_compilation/ice10938.d(14): Error: no property `opts` for `this` of type `ice10938.C`
fail_compilation/ice10938.d(14): potentially malformed `opDispatch`. Use an explicit instantiation to get a better error message
fail_compilation/ice10938.d(10): class `C` defined here
fail_compilation/ice10938.d(19): Error: forward reference to inferred return type of function call `this.opDispatch()`
fail_compilation/ice10938.d(14): Error: template instance `ice10938.C.opDispatch!"opts"` error instantiating
---
*/

Expand Down
4 changes: 2 additions & 2 deletions compiler/test/fail_compilation/test16188.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* TEST_OUTPUT:
---
fail_compilation/test16188.d(101): Error: no property `name` for `Where()` of type `test16188.Where`
fail_compilation/test16188.d(101): potentially malformed `opDispatch`. Use an explicit instantiation to get a better error message
fail_compilation/test16188.d(103): struct `Where` defined here
fail_compilation/test16188.d(107): Error: undefined identifier `getMember`
fail_compilation/test16188.d(101): Error: template instance `test16188.Where.opDispatch!"name"` error instantiating
---
*/

Expand Down

0 comments on commit 54a1424

Please sign in to comment.