Skip to content
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

Consider suggesting &mut TypeParam when given &TypeParam and that would fix the user's issue #81421

Closed
estebank opened this issue Jan 26, 2021 · 1 comment · Fixed by #81990
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 26, 2021

When given:

fn communicate_with_server<T: Read + Write>(mut stream: T) {
    let initial_message = format!("hello world");
    let mut buffer: Vec<u8> = Vec::new();
    let _bytes_written = stream.write_all(initial_message.as_bytes());
    let _flush = stream.flush();

    loop {
        let mut stream_reader = BufReader::new(&stream);
        stream_reader.read_until(b'\n', &mut buffer).expect("Reading into buffer failed");
    }
}

We currently emit:

error[E0277]: the trait bound `&T: std::io::Read` is not satisfied
  --> src/main.rs:13:48
   |
13 |         let mut stream_reader = BufReader::new(&stream);
   |                                                -^^^^^^
   |                                                |
   |                                                the trait `std::io::Read` is not implemented for `&T`
   |                                                help: consider removing the leading `&`-reference
   |
   = note: required by `BufReader::<R>::new`

error[E0599]: no method named `read_until` found for struct `BufReader<&T>` in the current scope
  --> src/main.rs:14:23
   |
14 |           stream_reader.read_until(b'\n', &mut buffer).expect("Reading into buffer failed");
   |                         ^^^^^^^^^^ method not found in `BufReader<&T>`
   |
   = note: the method `read_until` exists but the following trait bounds were not satisfied:
           `&T: std::io::Read`
           which is required by `BufReader<&T>: BufRead`

What is likely desired instead is to use &mut Stream.

Seen in the wild at https://www.reddit.com/r/rust/comments/l5osgu/why_does_a_reference_to_a_generic_parameter_not/

@estebank estebank added A-type-system Area: Type system 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. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jan 26, 2021
@estebank estebank changed the title Consider suggesting &mut TypoParam when given &TypeParam and that would fix the user's issue Consider suggesting &mut TypeParam when given &TypeParam and that would fix the user's issue Jan 26, 2021
@hkmatsumoto
Copy link
Member

@rustbot claim

JohnTitor added a commit to JohnTitor/rust that referenced this issue Feb 12, 2021
…estebank

Make suggestion of changing mutability of arguments broader

Fix rust-lang#81421

Previously rustc tries to emit the suggestion of changing mutablity unless `!trait_ref.has_infer_types_or_consts() && self.predicate_can_apply(obligation.param_env, trait_ref)` and this led to some false negatives to occur.
@bors bors closed this as completed in 46aef0e Feb 12, 2021
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-type-system Area: Type system D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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