Skip to content

Suggest to bind self.x to x when field x may be in format string #141633

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

@xizheyin xizheyin commented May 27, 2025

Fixes #141350

I added the new test in the first commit, and committed the changes in the second one.

r? @fmease
cc @mejrs

xizheyin added 2 commits May 27, 2025 14:43
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
@rustbot
Copy link
Collaborator

rustbot commented May 27, 2025

fmease is not available for reviewing at the moment.

Please choose another assignee.

@rustbot
Copy link
Collaborator

rustbot commented May 27, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 May 27, 2025
Comment on lines +769 to +771
// Check if the field is used in a format string, such as `{x}`.
// Note that both `let y = {x}` and `"{x}"` can match this pattern.
let maybe_field_is_format_named_arg = source_map
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use maybe to imply that maybe x is in the format string, and add a comment here to clarify.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

I don't think this is an improvement, the help message being changed is just as correct as what you are suggesting, but feels more appropriate as it adds to the format macro that is where the error originates rather than adding another line entirely. You could attempt to improve this with a structured suggestion that the user add a , foo = self.foo argument to the format macro.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 27, 2025
@xizheyin
Copy link
Contributor Author

For structured suggestions, I tried, but I don't currently have a better way to get the HIR or ast of the format macro, in this diagnostic I can only get the span of x. There is so little information given here. Do you have a good idea?

@xizheyin
Copy link
Contributor Author

The original help message is wrong for the following example

struct Type { field: i32 }

impl Type {
    fn method(&self) { {field} }
}

So, a line of binding had to be added to ensure that the formatted string and this case in the example were satisfied.

@mejrs
Copy link
Contributor

mejrs commented May 27, 2025

I don't think this is an improvement, the help message being changed is just as correct as what you are suggesting, but feels more appropriate as it adds to the format macro that is where the error originates rather than adding another line entirely. You could attempt to improve this with a structured suggestion that the user add a , foo = self.foo argument to the format macro.

The problem is that there's not a good way to detect that we're in the format (or write, print etc) macro to begin with.

But format string parsing has a specialized error path for format!("{thing.field}") that suggests what you say. So I guess if we just revert #141213 then they'll eventually get there:

With that PR reverted, users will go from
format!("{x}") to format!("{self.x}") to format!("{}", self.x).

Not that elegant, but it works.

@mejrs
Copy link
Contributor

mejrs commented May 27, 2025

But if we suggest creating a new variable, the suggestion is always correct;

let x = &self.x;
format!("{x}");

@xizheyin
Copy link
Contributor Author

With that PR reverted, users will go from format!("{x}") to format!("{self.x}") to format!("{}", self.x).

I'm more curious if this is considered proper advice if it's suggested through a two-paragraph application?

But if we suggest creating a new variable, the suggestion is always correct;

let x = &self.x;
format!("{x}");

Yes, this prompts the user once and avoids having to apply the suggestion twice.

@mejrs
Copy link
Contributor

mejrs commented May 27, 2025

I'm more curious if this is considered proper advice if it's suggested through a two-paragraph application?

It's not, but you also have to weigh that against how accurate the suggestion is and how likely someone is to run into it to begin with. Going through code snippets and looking for braces is a hacky thing that risks being inaccurate.

@davidtwco
Copy link
Member

For structured suggestions, I tried, but I don't currently have a better way to get the HIR or ast of the format macro, in this diagnostic I can only get the span of x. There is so little information given here. Do you have a good idea?

Does Span::from_expansion help?

@xizheyin
Copy link
Contributor Author

I tried this and it was false. Probably because it's not what the macro expands out to, but is simply treated as a variable.

@xizheyin
Copy link
Contributor Author

This PR seems to be on hold for a while, maybe David doesn't have time on this PR for a while.
may r? @nnethercote

@rustbot rustbot assigned nnethercote and unassigned davidtwco Jun 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 10, 2025

nnethercote is not on the review rotation at the moment.
They may take a while to respond.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Diagnostic assumes that braced unresolved identifiers are formatting arguments
5 participants