forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#107633 - clubby789:option-string-coerce-fix…
…, r=Nilstrieb Fix suggestion for coercing Option<&String> to Option<&str> Fixes rust-lang#107604 This also makes the diagnostic `MachineApplicable`, and runs `rustfix` to check we're not producing incorrect code. ``@rustbot`` label +A-diagnostics
- Loading branch information
Showing
5 changed files
with
88 additions
and
13 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
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,18 @@ | ||
// run-rustfix | ||
|
||
fn take_str_maybe(_: Option<&str>) { } | ||
fn main() { | ||
let string = String::from("Hello, world"); | ||
|
||
let option: Option<String> = Some(string.clone()); | ||
take_str_maybe(option.as_deref()); | ||
//~^ ERROR: mismatched types [E0308] | ||
|
||
let option_ref = Some(&string); | ||
take_str_maybe(option_ref.map(|x| x.as_str())); | ||
//~^ ERROR: mismatched types [E0308] | ||
|
||
let option_ref_ref = option_ref.as_ref(); | ||
take_str_maybe(option_ref_ref.map(|x| x.as_str())); | ||
//~^ ERROR: mismatched types [E0308] | ||
} |
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 |
---|---|---|
@@ -1,8 +1,18 @@ | ||
fn take_str_maybe(x: Option<&str>) -> Option<&str> { None } | ||
// run-rustfix | ||
|
||
fn take_str_maybe(_: Option<&str>) { } | ||
fn main() { | ||
let string = String::from("Hello, world"); | ||
let option = Some(&string); | ||
|
||
let option: Option<String> = Some(string.clone()); | ||
take_str_maybe(option); | ||
//~^ ERROR: mismatched types [E0308] | ||
|
||
let option_ref = Some(&string); | ||
take_str_maybe(option_ref); | ||
//~^ ERROR: mismatched types [E0308] | ||
|
||
let option_ref_ref = option_ref.as_ref(); | ||
take_str_maybe(option_ref_ref); | ||
//~^ ERROR: mismatched types [E0308] | ||
} |
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 |
---|---|---|
@@ -1,23 +1,63 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-89856.rs:6:20 | ||
--> $DIR/issue-89856.rs:8:20 | ||
| | ||
LL | take_str_maybe(option); | ||
| -------------- ^^^^^^ expected `Option<&str>`, found `Option<&String>` | ||
| -------------- ^^^^^^ expected `Option<&str>`, found `Option<String>` | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected enum `Option<&str>` | ||
found enum `Option<String>` | ||
note: function defined here | ||
--> $DIR/issue-89856.rs:3:4 | ||
| | ||
LL | fn take_str_maybe(_: Option<&str>) { } | ||
| ^^^^^^^^^^^^^^ --------------- | ||
help: try converting the passed type into a `&str` | ||
| | ||
LL | take_str_maybe(option.as_deref()); | ||
| +++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-89856.rs:12:20 | ||
| | ||
LL | take_str_maybe(option_ref); | ||
| -------------- ^^^^^^^^^^ expected `Option<&str>`, found `Option<&String>` | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected enum `Option<&str>` | ||
found enum `Option<&String>` | ||
note: function defined here | ||
--> $DIR/issue-89856.rs:1:4 | ||
--> $DIR/issue-89856.rs:3:4 | ||
| | ||
LL | fn take_str_maybe(_: Option<&str>) { } | ||
| ^^^^^^^^^^^^^^ --------------- | ||
help: try converting the passed type into a `&str` | ||
| | ||
LL | take_str_maybe(option_ref.map(|x| x.as_str())); | ||
| ++++++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-89856.rs:16:20 | ||
| | ||
LL | take_str_maybe(option_ref_ref); | ||
| -------------- ^^^^^^^^^^^^^^ expected `Option<&str>`, found `Option<&&String>` | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected enum `Option<&str>` | ||
found enum `Option<&&String>` | ||
note: function defined here | ||
--> $DIR/issue-89856.rs:3:4 | ||
| | ||
LL | fn take_str_maybe(x: Option<&str>) -> Option<&str> { None } | ||
LL | fn take_str_maybe(_: Option<&str>) { } | ||
| ^^^^^^^^^^^^^^ --------------- | ||
help: try converting the passed type into a `&str` | ||
| | ||
LL | take_str_maybe(option.map(|x| &**x)); | ||
| ++++++++++++++ | ||
LL | take_str_maybe(option_ref_ref.map(|x| x.as_str())); | ||
| ++++++++++++++++++++ | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |