Skip to content

Commit bf25346

Browse files
dkorpelthewilsonator
authored andcommitted
Add suggested fix to error message when casting struct to bool
1 parent f02b9d6 commit bf25346

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

compiler/src/dmd/expressionsem.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16843,8 +16843,12 @@ Expression toBoolean(Expression exp, Scope* sc)
1684316843
if (!t.isBoolean())
1684416844
{
1684516845
if (tb != Type.terror)
16846+
{
1684616847
error(exp.loc, "expression `%s` of type `%s` does not have a boolean value",
1684716848
exp.toChars(), t.toChars());
16849+
if (auto ts = tb.isTypeStruct())
16850+
errorSupplemental(ts.sym.loc, "perhaps add Cast Operator Overloading with `bool opCast(T : bool)() => ...`");
16851+
}
1684816852
return ErrorExp.get();
1684916853
}
1685016854
return e;
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
/*
22
TEST_OUTPUT:
33
---
4-
fail_compilation/diag13320.d(14): Error: operator `++` not supported for `f` of type `Foo`
5-
fail_compilation/diag13320.d(9): perhaps implement `auto opUnary(string op : "++")() {}` or `auto opOpAssign(string op : "+")(int) {}`
4+
fail_compilation/diag13320.d(20): Error: operator `++` not supported for `f` of type `Foo`
5+
fail_compilation/diag13320.d(14): perhaps implement `auto opUnary(string op : "++")() {}` or `auto opOpAssign(string op : "+")(int) {}`
6+
fail_compilation/diag13320.d(21): Error: expression `f` of type `Foo` does not have a boolean value
7+
fail_compilation/diag13320.d(14): perhaps add Cast Operator Overloading with `bool opCast(T : bool)() => ...`
8+
fail_compilation/diag13320.d(22): Error: expression `Foo()` of type `E` does not have a boolean value
9+
fail_compilation/diag13320.d(14): perhaps add Cast Operator Overloading with `bool opCast(T : bool)() => ...`
610
---
711
*/
812

13+
914
struct Foo {}
15+
enum E : Foo { a = Foo.init }
1016

1117
void main()
1218
{
1319
Foo f;
1420
++f;
21+
if (f) {}
22+
assert(E.a);
1523
}

0 commit comments

Comments
 (0)