From 2e94ea1a0dea79154c357628da7b5c90aa3da474 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 28 Sep 2024 23:53:31 +0200 Subject: [PATCH] Improved error messages on `Foo a = foo { 1 };` #1496 --- releasenotes.md | 1 + src/compiler/parse_stmt.c | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/releasenotes.md b/releasenotes.md index d639f4091..dd4c016dc 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -63,6 +63,7 @@ - Safer seed of rand() for WASM without libc. - Bad error message aliasing an ident with a path. #1481. - Error when slicing a struct with an inline array #1488. +- Improved error messages on `Foo a = foo { 1 };` #1496 ### Stdlib changes - Additional init functions for hashmap. diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index 32bb0651c..60ba8c505 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -15,9 +15,14 @@ static Ast *parse_decl_stmt_after_type(ParseContext *c, TypeInfo *type) ast->span = type->span; ast->ast_kind = AST_DECLARE_STMT; ASSIGN_DECL_OR_RET(ast->declare_stmt, parse_local_decl_after_type(c, type), poisoned_ast); + Decl *decl = ast->declare_stmt; + if (tok_is(c, TOKEN_LBRACE) && decl->var.init_expr && decl->var.init_expr->expr_kind == EXPR_IDENTIFIER) + { + print_error_at(decl->var.init_expr->span, "An identifier would not usually be followed by a '{'. Did you intend write the name of a type here?"); + return poisoned_ast; + } if (tok_is(c, TOKEN_EOS)) return ast; - Decl *decl = ast->declare_stmt; if (decl->attributes || decl->var.init_expr) { if (tok_is(c, TOKEN_COMMA) && peek(c) == TOKEN_IDENT)