-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix suggestion to constrain trait for method to be found
- Loading branch information
Showing
4 changed files
with
191 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// run-rustfix | ||
// check-only | ||
|
||
#[derive(Debug)] | ||
struct Demo { | ||
a: String | ||
} | ||
|
||
trait GetString { | ||
fn get_a(&self) -> &String; | ||
} | ||
|
||
trait UseString: std::fmt::Debug + GetString { | ||
fn use_string(&self) { | ||
println!("{:?}", self.get_a()); //~ ERROR no method named `get_a` found for type `&Self` | ||
} | ||
} | ||
|
||
trait UseString2: GetString { | ||
fn use_string(&self) { | ||
println!("{:?}", self.get_a()); //~ ERROR no method named `get_a` found for type `&Self` | ||
} | ||
} | ||
|
||
impl GetString for Demo { | ||
fn get_a(&self) -> &String { | ||
&self.a | ||
} | ||
} | ||
|
||
impl UseString for Demo {} | ||
impl UseString2 for Demo {} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{Demo, UseString}; | ||
|
||
#[test] | ||
fn it_works() { | ||
let d = Demo { a: "test".to_string() }; | ||
d.use_string(); | ||
} | ||
} | ||
|
||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// run-rustfix | ||
// check-only | ||
|
||
#[derive(Debug)] | ||
struct Demo { | ||
a: String | ||
} | ||
|
||
trait GetString { | ||
fn get_a(&self) -> &String; | ||
} | ||
|
||
trait UseString: std::fmt::Debug { | ||
fn use_string(&self) { | ||
println!("{:?}", self.get_a()); //~ ERROR no method named `get_a` found for type `&Self` | ||
} | ||
} | ||
|
||
trait UseString2 { | ||
fn use_string(&self) { | ||
println!("{:?}", self.get_a()); //~ ERROR no method named `get_a` found for type `&Self` | ||
} | ||
} | ||
|
||
impl GetString for Demo { | ||
fn get_a(&self) -> &String { | ||
&self.a | ||
} | ||
} | ||
|
||
impl UseString for Demo {} | ||
impl UseString2 for Demo {} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{Demo, UseString}; | ||
|
||
#[test] | ||
fn it_works() { | ||
let d = Demo { a: "test".to_string() }; | ||
d.use_string(); | ||
} | ||
} | ||
|
||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0599]: no method named `get_a` found for type `&Self` in the current scope | ||
--> $DIR/constrain-trait.rs:15:31 | ||
| | ||
LL | println!("{:?}", self.get_a()); | ||
| ^^^^^ method not found in `&Self` | ||
| | ||
= 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 add another supertrait for it: | ||
| | ||
LL | trait UseString: std::fmt::Debug + GetString { | ||
| ^^^^^^^^^^^ | ||
|
||
error[E0599]: no method named `get_a` found for type `&Self` in the current scope | ||
--> $DIR/constrain-trait.rs:21:31 | ||
| | ||
LL | println!("{:?}", self.get_a()); | ||
| ^^^^^ method not found in `&Self` | ||
| | ||
= 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 add a supertrait for it: | ||
| | ||
LL | trait UseString2: GetString { | ||
| ^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0599`. |