Skip to content

Commit

Permalink
Rollup merge of rust-lang#109613 - lukas-code:match-str-to-char-sugge…
Browse files Browse the repository at this point in the history
…stion, r=compiler-errors

fix type suggestions in match arms

fix rust-lang#109586
  • Loading branch information
Dylan-DPC committed Mar 27, 2023
2 parents 6371b91 + 08f3deb commit ac61f26
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
escaped
}
let mut err = struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str);
if let Some((expected, found)) = trace.values.ty() {
let values = self.resolve_vars_if_possible(trace.values);
if let Some((expected, found)) = values.ty() {
match (expected.kind(), found.kind()) {
(ty::Tuple(_), ty::Tuple(_)) => {}
// If a tuple of length one was expected and the found expression has
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/inference/char-as-str-single.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ fn main() {
let _: char = '人'; //~ ERROR mismatched types
let _: char = '\''; //~ ERROR mismatched types
}

// regression test for https://github.com/rust-lang/rust/issues/109586
#[allow(dead_code)]
fn convert_c_to_str(c: char) {
match c {
'A' => {} //~ ERROR mismatched types
_ => {}
}
}
9 changes: 9 additions & 0 deletions tests/ui/inference/char-as-str-single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ fn main() {
let _: char = "人"; //~ ERROR mismatched types
let _: char = "'"; //~ ERROR mismatched types
}

// regression test for https://github.com/rust-lang/rust/issues/109586
#[allow(dead_code)]
fn convert_c_to_str(c: char) {
match c {
"A" => {} //~ ERROR mismatched types
_ => {}
}
}
15 changes: 14 additions & 1 deletion tests/ui/inference/char-as-str-single.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ help: if you meant to write a `char` literal, use single quotes
LL | let _: char = '\'';
| ~~~~

error: aborting due to 3 previous errors
error[E0308]: mismatched types
--> $DIR/char-as-str-single.rs:18:9
|
LL | match c {
| - this expression has type `char`
LL | "A" => {}
| ^^^ expected `char`, found `&str`
|
help: if you meant to write a `char` literal, use single quotes
|
LL | 'A' => {}
| ~~~

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit ac61f26

Please sign in to comment.