Skip to content

Commit dfce933

Browse files
authored
Rollup merge of rust-lang#59268 - estebank:from-string, r=QuietMisdreavus
Add suggestion to use `&*var` when `&str: From<String>` is expected Fix rust-lang#53879.
2 parents 692760d + e929d19 commit dfce933

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,16 @@ 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`, `any` or
143+
`not` in the following way:
144+
145+
```rust,compile_fail
146+
#[rustc_on_unimplemented(
147+
on(
148+
all(_Self="&str", T="std::string::String"),
149+
note="you can coerce a `{T}` into a `{Self}` by writing `&*variable`"
150+
)
151+
)]
152+
pub trait From<T>: Sized { /* ... */ }
153+
```

src/libcore/convert.rs

+6
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ pub trait Into<T>: Sized {
363363
/// [`from`]: trait.From.html#tymethod.from
364364
/// [book]: ../../book/ch09-00-error-handling.html
365365
#[stable(feature = "rust1", since = "1.0.0")]
366+
#[rustc_on_unimplemented(
367+
on(
368+
all(_Self="&str", T="std::string::String"),
369+
note="to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
370+
)
371+
)]
366372
pub trait From<T>: Sized {
367373
/// Performs the conversion.
368374
#[stable(feature = "rust1", since = "1.0.0")]

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

+6
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+
}
+17
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: to coerce a `std::string::String` into a `&str`, use `&*` as a prefix
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)