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

restrict type parameter Self error is unclear. #65044

Closed
Firstyear opened this issue Oct 3, 2019 · 3 comments · Fixed by #65242
Closed

restrict type parameter Self error is unclear. #65044

Firstyear opened this issue Oct 3, 2019 · 3 comments · Fixed by #65242
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

@Firstyear
Copy link
Contributor

The following example code yields an error:

struct Demo {
    a: String
}

trait GetString {
    fn get_a(&self) -> &String;
}

trait UseString {
    fn use_string(&self) {
        println!("{:?}", self.get_a());
    }
}

impl GetString for Demo {
    fn get_a(&self) -> &String {
        &self.a
    }
}

impl UseString for Demo {}


#[cfg(test)]
mod tests {
    use crate::{Demo, UseString};

    #[test]
    fn it_works() {
        let d = Demo { a: "test".to_string() };
        d.use_string();
    }
}
warning: build failed, waiting for other jobs to finish...
error[E0599]: no method named `get_a` found for type `&Self` in the current scope
  --> src/lib.rs:12:31
   |
12 |         println!("{:?}", self.get_a());
   |                               ^^^^^
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `get_a`, perhaps you need to restrict type parameter `Self` with it:
   |
10 | Self: GetString
   |

error: aborting due to previous error

It's unclear from this error message how to proceed - I'm not sure of the syntax, location, or where I should be putting the "GetString" trait bound.

It should be shown with a better example or link when this error is presented, as the current message is not sufficient for a resolution to be arrived at.

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 3, 2019
@Centril
Copy link
Contributor

Centril commented Oct 3, 2019

cc @estebank

@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Oct 9, 2019
@estebank estebank self-assigned this Oct 9, 2019
@estebank
Copy link
Contributor

estebank commented Oct 9, 2019

Fixing in #65242.

To fix your code write:

trait UseString: GetString {
    fn use_string(&self) {
        println!("{:?}", self.get_a());
    }
}

@Firstyear
Copy link
Contributor Author

Thanks!

Centril added a commit to Centril/rust that referenced this issue Oct 15, 2019
Fix suggestion to constrain trait for method to be found

Fix rust-lang#65044.
tmandry added a commit to tmandry/rust that referenced this issue Oct 15, 2019
Fix suggestion to constrain trait for method to be found

Fix rust-lang#65044.
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` 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

Successfully merging a pull request may close this issue.

3 participants