Skip to content

Check if format argument is identifier to avoid error err-emit #140286

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

Fixes #139104

When argument is not an identifier, it should not be considered a field access. I checked this and if not emit an invalid format string error. I think we could do with a little finer error handling, I'll open an issue to track this down later.

The first commit submits the ui test, the second commits the code and the changes to the test output.

r? compiler

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 25, 2025
@xizheyin xizheyin marked this pull request as draft April 25, 2025 10:13
if !self.consume('}') {
return;
// If the argument is an identifier, it may be a field access.
if arg.is_identifier() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I first determine if it's an identifier before considering if it's a field access, e.g. foo.0, foo.a

}
} else if matches!(arg.position, ArgumentNamed(_) | ArgumentIs(_)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only trigger this error when there is an explicit explicit location or a named argument.

This is because the case of ArgumentImplicitlyIs is more complex, e.g. “{ x”, and I think it should be prioritized to output the missing } over this error. For arguments that have a name or an explicit position, it's more appropriate to output invalid format specifier instead of missing }.

Also, the original logic does not emit an ERROR in ArgumentImplicitlyIs case, so I'm not making radical changes for now. It can be left for later enhancements.

@xizheyin xizheyin marked this pull request as ready for review April 25, 2025 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid format string with certain fill characters yields incorrect suggestion
3 participants