AstGen: improve error message for missing parameter name

Closes #13393
This commit is contained in:
Veikka Tuominen 2022-12-02 15:39:40 +02:00
parent 665eba93c1
commit 86e6acb37b
2 changed files with 42 additions and 0 deletions

View File

@ -3696,6 +3696,29 @@ fn fnDecl(
if (param.anytype_ellipsis3) |tok| {
return astgen.failTok(tok, "missing parameter name", .{});
} else {
ambiguous: {
if (tree.nodes.items(.tag)[param.type_expr] != .identifier) break :ambiguous;
const main_token = tree.nodes.items(.main_token)[param.type_expr];
const identifier_str = tree.tokenSlice(main_token);
if (isPrimitive(identifier_str)) break :ambiguous;
return astgen.failNodeNotes(
param.type_expr,
"missing parameter name or type",
.{},
&[_]u32{
try astgen.errNoteNode(
param.type_expr,
"if this is a name, annotate its type '{s}: T'",
.{identifier_str},
),
try astgen.errNoteNode(
param.type_expr,
"if this is a type, give it a name '<name>: {s}'",
.{identifier_str},
),
},
);
}
return astgen.failNode(param.type_expr, "missing parameter name", .{});
}
} else 0;

View File

@ -0,0 +1,19 @@
fn f2(u64) u64 {
return x;
}
fn f3(*x) u64 {
return x;
}
fn f1(x) u64 {
return x;
}
// error
// backend=stage2
// target=native
//
// :1:7: error: missing parameter name
// :4:7: error: missing parameter name
// :7:7: error: missing parameter name or type
// :7:7: note: if this is a name, annotate its type 'x: T'
// :7:7: note: if this is a type, give it a name '<name>: x'