Skip to content

Incorrect '&'-reference removal #140166

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
adi-joshi opened this issue Apr 22, 2025 · 1 comment
Open

Incorrect '&'-reference removal #140166

adi-joshi opened this issue Apr 22, 2025 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@adi-joshi
Copy link

adi-joshi commented Apr 22, 2025

Code

fn print_board(ttt_array: &[[char; 3]; 3]) {
    println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
}

Current output

error[E0277]: `&Chars<'_>` is not an iterator
  --> src/main.rs:34:44
   |
34 |     println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
   |                                            ^^^^^^^^ `&Chars<'_>` is not an iterator
   |
   = help: the trait `Iterator` is not implemented for `&Chars<'_>`
   = help: the trait `Iterator` is implemented for `Chars<'_>`
   = note: `Iterator` is implemented for `&mut Chars<'_>`, but not for `&Chars<'_>`
   = note: required for `&Chars<'_>` to implement `IntoIterator`
note: required by a bound in `flat_map`
  --> /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/core/src/iter/traits/iterator.rs:1430:5

error[E0277]: `&Chars<'_>` is not an iterator
  --> src/main.rs:34:22
   |
34 |     println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
   |               ----   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&Chars<'_>` is not an iterator
   |               |
   |               required by a bound introduced by this call
   |
   = help: the trait `Iterator` is not implemented for `&Chars<'_>`
   = note: `Iterator` is implemented for `&mut Chars<'_>`, but not for `&Chars<'_>`
   = note: required for `&Chars<'_>` to implement `IntoIterator`
   = note: required for `FlatMap<std::slice::Iter<'_, [char; 3]>, &Chars<'_>, {closure@src/main.rs:34:53: 34:56}>` to implement `Debug`
note: required by a bound in `core::fmt::rt::Argument::<'_>::new_debug`
  --> /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/core/src/fmt/rt.rs:117:5
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider removing the leading `&`-reference
   |
34 -     println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
34 +     println!("{:?}", tt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
   |

For more information about this error, try `rustc --explain E0277`.

Desired output

Rationale and extra context

The proposed change by the compiler is to remove the leading &-reference by removing the first character of the expression (which is not an & in this case).

I believe the &-reference in this case is from inside the lambda expression (the flat_map(|x| &x.into...) part).

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Anything else?

No response

@adi-joshi adi-joshi 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 Apr 22, 2025
@adi-joshi adi-joshi changed the title Incorrect '&' character removal Incorrect '&'-reference removal Apr 22, 2025
@theemathas
Copy link
Contributor

A more self-contained reproducer:

trait Trait {}

struct Chars;
impl Trait for Chars {}

struct FlatMap<T>(T);
impl<T: Trait> std::fmt::Debug for FlatMap<T> {
    fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        unimplemented!()
    }
}

fn lol() {
    format_args!("{:?}", FlatMap(&Chars));
}
error[E0277]: the trait bound `&Chars: Trait` is not satisfied
   --> src/lib.rs:14:26
    |
14  |     format_args!("{:?}", FlatMap(&Chars));
    |                   ----   ^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `&Chars`
    |                   |
    |                   required by a bound introduced by this call
    |
note: required for `FlatMap<&Chars>` to implement `Debug`
   --> src/lib.rs:7:16
    |
7   | impl<T: Trait> std::fmt::Debug for FlatMap<T> {
    |         -----  ^^^^^^^^^^^^^^^     ^^^^^^^^^^
    |         |
    |         unsatisfied trait bound introduced here
note: required by a bound in `core::fmt::rt::Argument::<'_>::new_debug`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/rt.rs:117:25
    |
117 |     pub fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
    |                         ^^^^^ required by this bound in `Argument::<'_>::new_debug`
    = note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider removing the leading `&`-reference
    |
14  -     format_args!("{:?}", FlatMap(&Chars));
14  +     format_args!("{:?}", latMap(&Chars));
    |

For more information about this error, try `rustc --explain E0277`.

@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 22, 2025
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 A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants