Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Wrong help message for Vec::resize_with calls that constrain the Vec to a wrong type #116155

Closed

Description

@narpfel

Code

struct S { xs: Vec<usize> }

fn main() {
    let mut xs = Vec::new();
    xs.resize_with(42, || true);
    S { xs };
}

Current output

error[E0308]: mismatched types
 --> <source>:6:9
  |
5 |     xs.resize_with(42, || true);
  |     --                 ------- this argument has type `[closure@<source>:5:24: 5:26]`...
  |     |
  |     ... which causes `xs` to have type `Vec<bool>`
6 |     S { xs };
  |         ^^ expected `Vec<usize>`, found `Vec<bool>`
  |
  = note: expected struct `Vec<usize>`
             found struct `Vec<bool>`
help: use parentheses to call this closure
  |
5 |     xs.resize_with(42, (|| true)());
  |                        +       +++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Compiler returned: 1

Desired output

error[E0308]: mismatched types
 --> <source>:6:9
  |
5 |     xs.resize_with(42, || true);
  |     --                 ------- this argument has type `[closure@<source>:5:24: 5:26]`...
  |     |
  |     ... which causes `xs` to have type `Vec<bool>`
6 |     S { xs };
  |         ^^ expected `Vec<usize>`, found `Vec<bool>`
  |
  = note: expected struct `Vec<usize>`
             found struct `Vec<bool>`
help: this expression should have type `usize`
  |
5 |     xs.resize_with(42, || true);
  |                           ~~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Compiler returned: 1

Rationale and extra context

The same help message is also shown for function calls and other cases which later lead to type errors, such as returning xs from a function with return type Vec<usize>.

Present in 1.72.0 stable and nightly-2023-09-24:

$ rustc -vV
rustc 1.74.0-nightly (37390d656 2023-09-24)
binary: rustc
commit-hash: 37390d65636dd67e263753a3c04fbc60dcc4348e
commit-date: 2023-09-24
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0

Other cases

No response

Anything else?

Godbolt link

Activity

  1. added
    A-diagnostics
    T-compiler
    on Sep 25, 2023
  2. added
    needs-triage
    on Sep 25, 2023
  3. added
    D-invalid-suggestion
    and removed
    needs-triage
    on Sep 25, 2023
  4. asquared31415 commented on Sep 25, 2023

    @asquared31415
    Contributor

    Reduced to not rely on Vec:

    struct S<T>(T);
    
    impl<T> S<T> {
        fn new() -> Self {
            loop {}
        }
    
        fn constrain<F: Fn() -> T>(&self, _f: F) {}
    }
    
    fn main() {
        let s = S::new();
        let c = || true;
        s.constrain(c);
        let _: S<usize> = s;
    }
    error[E0308]: mismatched types
      --> src/main.rs:15:23
       |
    14 |     s.constrain(c);
       |     -           - this argument has type `[closure@src/main.rs:13:13: 13:15]`...
       |     |
       |     ... which causes `s` to have type `S<bool>`
    15 |     let _: S<usize> = s;
       |            --------   ^ expected `S<usize>`, found `S<bool>`
       |            |
       |            expected due to this
       |
       = note: expected struct `S<usize>`
                  found struct `S<bool>`
    help: use parentheses to call this closure
       |
    14 |     s.constrain(c());
       |                  ++
    
  5. added a commit that references this issue on Oct 3, 2023
  6. added a commit that references this issue on Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsD-invalid-suggestionT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions