Skip to content

Add diagnostics for using auto as a type of variable and as function return type #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4894,6 +4894,21 @@ class parser
}
}
else if (auto t = type_id()) {
if (
t->get_token()
&& t->get_token()->to_string(true) == "auto"
)
{
auto name = std::string{"v"};
if (my_decl && my_decl->name()) {
name = my_decl->name()->to_string(true);
}
errors.emplace_back(
curr().position(),
"to define a function " + name + " with deduced return type, write '" + name + ": ( /* arguments */ ) -> _ = { /* function body */ }'"
);
return {};
}
n->returns = function_type_node::single_type_id{ std::move(t), passing_style::move };
assert(n->returns.index() == function_type_node::id);
}
Expand Down Expand Up @@ -5113,6 +5128,22 @@ class parser

// Or just a type-id, declaring a non-pointer object
else if (auto t = type_id()) {
if (
t->get_token()
&& t->get_token()->to_string(true) == "auto"
)
{
auto name = std::string{"v"};
if (n->name()) {
name = n->name()->to_string(true);
}
errors.emplace_back(
curr().position(),
"to define a variable " + name + " with deduced type, write '" + name + " := /* initializer */;'"
);
return {};
}

n->type = std::move(t);
assert (n->is_object());

Expand Down