Skip to content

Provide hint for auto declaration in Cpp2 code #342

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 2 commits into from
Apr 9, 2023

Conversation

ntrel
Copy link
Contributor

@ntrel ntrel commented Apr 9, 2023

AFAIK, no Cpp2 declaration starts with an auto keyword, so forbid it.
Like the hints for namespace/class keywords.

Without this:

f: () = {
    ...
    auto x = 1; // causes ill-formed initializer error but line is first line of `f`, not this line
    auto [x,y] = (1,2); // passes cppfront but errors in C++1
}

With this, both auto declarations are caught with correct line numbers:

test.cpp2(8,10): error: unknown declaration (use `name: type = initializer` instead?)
test.cpp2(9,10): error: unknown declaration (use `name: type = initializer` instead?)

BTW I have emailed Herb requesting a CLA form.

Like the hints for `namespace`/`class` keywords.
@hsutter
Copy link
Owner

hsutter commented Apr 9, 2023

Thanks, this looks good! CLA is on the way.

Could you please update it to follow the style in the immediately following check for namespace? That is, use the next token (if there is one) in the error message, and make the error message follow that pattern... something like "to define an object " + name + ", write '" + name + " : /*type*/ = /*initializer*/;', or '" + name + " : /*type*/;' and initialize the object later". 🙏

Use it in error message.
@ntrel
Copy link
Contributor Author

ntrel commented Apr 9, 2023

Now done, it prints:

test.cpp2(8,10): error: to define a variable x of type T, write 'x: T = /* initializer */'
test.cpp2(9,10): error: to define a variable v of type T, write 'v: T = /* initializer */'

The simple case parses the identifier, the auto [x, y] case just defaults to v.

@hsutter
Copy link
Owner

hsutter commented Apr 9, 2023

Looks good, thanks!

@hsutter hsutter merged commit 089b785 into hsutter:main Apr 9, 2023
zaucy pushed a commit to zaucy/cppfront that referenced this pull request Dec 5, 2023
* Provide hint for `auto` declaration in Cpp2 code

Like the hints for `namespace`/`class` keywords.

* Try to parse name for auto declaration

Use it in error message.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants