Skip to content

Commit

Permalink
Improve error message in the case of MyInterface x = foo; #1522
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Oct 7, 2024
1 parent 80d016e commit 19a96ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Slicing arrays, slices and bytes at compile time #1466.
- Better error for `int a[4] = ...`. #1518
- Better error for `int Foo(int a)` declarations #1516
- Improve error message in the case of `MyInterface x = foo;` #1522

### Fixes
- `Unsupported int[*] $x = { 1, 2, 3, 4 }` #1489.
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/sema_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,21 @@ static bool report_cast_error(CastContext *cc, bool may_cast_explicit)
}
else
{
if (to->type_kind == TYPE_INTERFACE)
{
if (expr->type->canonical->type_kind != TYPE_POINTER)
{
RETURN_CAST_ERROR(expr,
"You can only convert pointers to an interface like %s. "
"Try passing the address of the expression instead.",
type_quoted_error_string(to));
}
}
else if (to->type_kind == TYPE_ANY && expr->type->canonical->type_kind != TYPE_POINTER)
{
RETURN_CAST_ERROR(expr, "You can only convert pointers to 'any'. "
"Try passing the address of the expression instead.");
}
RETURN_CAST_ERROR(expr,
"It is not possible to cast %s to %s.",
type_quoted_error_string(type_no_optional(expr->type)), type_quoted_error_string(to));
Expand Down

0 comments on commit 19a96ac

Please sign in to comment.