Skip to content

Add self. on format!() macro failed #141136

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

Closed
A4-Tacks opened this issue May 17, 2025 · 1 comment · Fixed by #141213
Closed

Add self. on format!() macro failed #141136

A4-Tacks opened this issue May 17, 2025 · 1 comment · Fixed by #141213
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@A4-Tacks
Copy link
Contributor

A4-Tacks commented May 17, 2025

Code

struct Foo {
    x: i32
}
impl Foo {
    fn foo(&self) {
        let _ = format!("{x}");
    }
}

Current output

Checking test_ v0.1.0 (/home/lrne/Project/Rust/test_)
error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:6:27
  |
6 |         let _ = format!("{x}");
  |                           ^
  |
help: you might have meant to use the available field
  |
6 |         let _ = format!("{self.x}");
  |                           +++++

For more information about this error, try `rustc --explain E0425`.
error: could not compile `test_` (bin "test_") due to 1 previous error

Desired output

Checking test_ v0.1.0 (/home/lrne/Project/Rust/test_)
error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:6:27
  |
6 |         let _ = format!("{x}");
  |                           ^
  |
help: you might have meant to use the available field
  |
6 |         let _ = format!("{}", self.x);
  |                               ++++++

For more information about this error, try `rustc --explain E0425`.
error: could not compile `test_` (bin "test_") due to 1 previous error

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0-nightly (a15cce269 2025-04-17)
binary: rustc
commit-hash: a15cce2690e8fab72422515c9dc02c6fbc506733
commit-date: 2025-04-17
host: aarch64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

No response

@A4-Tacks A4-Tacks added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 17, 2025
@xizheyin
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in d30f047 May 21, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 21, 2025
Rollup merge of rust-lang#141213 - xizheyin:issue-141136, r=nnethercote

Suggest use "{}", self.x instead of {self.x} when resolve x as field of `self`

Fixes rust-lang#141136

Changes can be seen in the second commit: rust-lang@9de7fff

r? compiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants