Skip to content

Commit 0bac3bc

Browse files
authored
Add diagnostics for using auto as a type of variable and as function return type (#348)
* Add diagnostics for using auto as function return type While parsing following code: ```cpp f: () -> auto = 42; ``` Rise an error with the following diagnostics: ``` error: to define a function f with deduced return type, write 'f : ( /* arguments */ ) -> _ = { /* function body */ }' ``` * Add diagnostics for using auto as a type of variable While parsing the following code: ```cpp i : auto = 42; ``` Rise an error with the following diagnostics: ``` error: to define a variable i with deduced type, write 'i := /* initializer */;' ```
1 parent 56ce565 commit 0bac3bc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

source/parse.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4996,6 +4996,21 @@ class parser
49964996
}
49974997
}
49984998
else if (auto t = type_id()) {
4999+
if (
5000+
t->get_token()
5001+
&& t->get_token()->to_string(true) == "auto"
5002+
)
5003+
{
5004+
auto name = std::string{"v"};
5005+
if (my_decl && my_decl->name()) {
5006+
name = my_decl->name()->to_string(true);
5007+
}
5008+
errors.emplace_back(
5009+
curr().position(),
5010+
"to define a function " + name + " with deduced return type, write '" + name + ": ( /* arguments */ ) -> _ = { /* function body */ }'"
5011+
);
5012+
return {};
5013+
}
49995014
n->returns = function_type_node::single_type_id{ std::move(t), passing_style::move };
50005015
assert(n->returns.index() == function_type_node::id);
50015016
}
@@ -5215,6 +5230,22 @@ class parser
52155230

52165231
// Or just a type-id, declaring a non-pointer object
52175232
else if (auto t = type_id()) {
5233+
if (
5234+
t->get_token()
5235+
&& t->get_token()->to_string(true) == "auto"
5236+
)
5237+
{
5238+
auto name = std::string{"v"};
5239+
if (n->name()) {
5240+
name = n->name()->to_string(true);
5241+
}
5242+
errors.emplace_back(
5243+
curr().position(),
5244+
"to define a variable " + name + " with deduced type, write '" + name + " := /* initializer */;'"
5245+
);
5246+
return {};
5247+
}
5248+
52185249
n->type = std::move(t);
52195250
assert (n->is_object());
52205251

0 commit comments

Comments
 (0)