Skip to content

Commit ac3290e

Browse files
committed
Add suggestion to use &*var when &str: From<String> is expected
1 parent 9f91bee commit ac3290e

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/doc/unstable-book/src/language-features/on-unimplemented.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,15 @@ error[E0277]: `&str` is not an iterator
138138
= help: the trait `std::iter::Iterator` is not implemented for `&str`
139139
= note: required by `std::iter::IntoIterator::into_iter`
140140
```
141+
142+
If you need to filter on multiple attributes, you can use `all` in the following way:
143+
144+
```rust,compile_fail
145+
#[rustc_on_unimplemented(
146+
on(
147+
all(_Self="&str", T="std::string::String"),
148+
note="you can coerce a `{T}` into a `{Self}` by writing `&*variable`"
149+
)
150+
)]
151+
pub trait From<T>: Sized { /* ... */ }
152+
```

src/libcore/convert.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ pub trait Into<T>: Sized {
352352
/// [`from`]: trait.From.html#tymethod.from
353353
/// [book]: ../../book/ch09-00-error-handling.html
354354
#[stable(feature = "rust1", since = "1.0.0")]
355+
#[rustc_on_unimplemented(
356+
on(
357+
all(_Self="&str", T="std::string::String"),
358+
note="you can coerce a `{T}` into a `{Self}` by writing `&*variable`"
359+
)
360+
)]
355361
pub trait From<T>: Sized {
356362
/// Performs the conversion.
357363
#[stable(feature = "rust1", since = "1.0.0")]

src/test/ui/suggestions/into-str.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn foo<'a, T>(_t: T) where T: Into<&'a str> {}
2+
3+
fn main() {
4+
foo(String::new());
5+
//~^ ERROR the trait bound `&str: std::convert::From<std::string::String>` is not satisfied
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0277]: the trait bound `&str: std::convert::From<std::string::String>` is not satisfied
2+
--> $DIR/into-str.rs:4:5
3+
|
4+
LL | foo(String::new());
5+
| ^^^ the trait `std::convert::From<std::string::String>` is not implemented for `&str`
6+
|
7+
= note: you can coerce a `std::string::String` into a `&str` by writing `&*variable`
8+
= note: required because of the requirements on the impl of `std::convert::Into<&str>` for `std::string::String`
9+
note: required by `foo`
10+
--> $DIR/into-str.rs:1:1
11+
|
12+
LL | fn foo<'a, T>(_t: T) where T: Into<&'a str> {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)