diff options
| author | Jubilee <46493976+workingjubilee@users.noreply.github.com> | 2024-05-28 02:07:48 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-28 02:07:48 -0700 |
| commit | 01aa2e85110ceba2a675aa1281b56e7df7ed4238 (patch) | |
| tree | cd9362f1ec12c5f3e0d742e9bf912c6060bff116 | |
| parent | 941bf8bee12ebf271cae5bfb8e8dd7a2a5fac1d3 (diff) | |
| parent | 27cdc0df4e73b76466c72ae69ab6545f84089ad2 (diff) | |
| download | rust-01aa2e85110ceba2a675aa1281b56e7df7ed4238.tar.gz rust-01aa2e85110ceba2a675aa1281b56e7df7ed4238.zip | |
Rollup merge of #125640 - fmease:plz-no-stringify, r=estebank
Don't suggest turning non-char-literal exprs of ty `char` into string literals Fixes #125595. Fixes #125081. r? estebank (#122217) or compiler
| -rw-r--r-- | compiler/rustc_infer/src/infer/error_reporting/mod.rs | 13 | ||||
| -rw-r--r-- | tests/crashes/125081.rs | 7 | ||||
| -rw-r--r-- | tests/ui/inference/str-as-char-butchered.rs | 7 | ||||
| -rw-r--r-- | tests/ui/inference/str-as-char-butchered.stderr | 22 | ||||
| -rw-r--r-- | tests/ui/inference/str-as-char-non-lit.rs | 9 | ||||
| -rw-r--r-- | tests/ui/inference/str-as-char-non-lit.stderr | 19 |
6 files changed, 66 insertions, 11 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 7ab148e70a1..95fbc1e66ce 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2102,10 +2102,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // If a string was expected and the found expression is a character literal, // perhaps the user meant to write `"s"` to specify a string literal. (ty::Ref(_, r, _), ty::Char) if r.is_str() => { - suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral { - start: span.with_hi(span.lo() + BytePos(1)), - end: span.with_lo(span.hi() - BytePos(1)), - }) + if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) + && code.starts_with("'") + && code.ends_with("'") + { + suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral { + start: span.with_hi(span.lo() + BytePos(1)), + end: span.with_lo(span.hi() - BytePos(1)), + }); + } } // For code `if Some(..) = expr `, the type mismatch may be expected `bool` but found `()`, // we try to suggest to add the missing `let` for `if let Some(..) = expr` diff --git a/tests/crashes/125081.rs b/tests/crashes/125081.rs deleted file mode 100644 index 7139caaa00d..00000000000 --- a/tests/crashes/125081.rs +++ /dev/null @@ -1,7 +0,0 @@ -//@ known-bug: rust-lang/rust#125081 - -use std::cell::Cell; - -fn main() { - let _: Cell<&str, "a"> = Cell::new('β); -} diff --git a/tests/ui/inference/str-as-char-butchered.rs b/tests/ui/inference/str-as-char-butchered.rs new file mode 100644 index 00000000000..6020cd3422f --- /dev/null +++ b/tests/ui/inference/str-as-char-butchered.rs @@ -0,0 +1,7 @@ +// issue: rust-lang/rust#125081 + +fn main() { + let _: &str = 'β; + //~^ ERROR expected `while`, `for`, `loop` or `{` after a label + //~| ERROR mismatched types +} diff --git a/tests/ui/inference/str-as-char-butchered.stderr b/tests/ui/inference/str-as-char-butchered.stderr new file mode 100644 index 00000000000..ad465f979d4 --- /dev/null +++ b/tests/ui/inference/str-as-char-butchered.stderr @@ -0,0 +1,22 @@ +error: expected `while`, `for`, `loop` or `{` after a label + --> $DIR/str-as-char-butchered.rs:4:21 + | +LL | let _: &str = 'β; + | ^ expected `while`, `for`, `loop` or `{` after a label + | +help: add `'` to close the char literal + | +LL | let _: &str = 'β'; + | + + +error[E0308]: mismatched types + --> $DIR/str-as-char-butchered.rs:4:19 + | +LL | let _: &str = 'β; + | ---- ^^ expected `&str`, found `char` + | | + | expected due to this + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/inference/str-as-char-non-lit.rs b/tests/ui/inference/str-as-char-non-lit.rs new file mode 100644 index 00000000000..d38b46630dc --- /dev/null +++ b/tests/ui/inference/str-as-char-non-lit.rs @@ -0,0 +1,9 @@ +// Don't suggest double quotes when encountering an expr of type `char` where a `&str` +// is expected if the expr is not a char literal. +// issue: rust-lang/rust#125595 + +fn main() { + let _: &str = ('a'); //~ ERROR mismatched types + let token = || 'a'; + let _: &str = token(); //~ ERROR mismatched types +} diff --git a/tests/ui/inference/str-as-char-non-lit.stderr b/tests/ui/inference/str-as-char-non-lit.stderr new file mode 100644 index 00000000000..7675fe01330 --- /dev/null +++ b/tests/ui/inference/str-as-char-non-lit.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/str-as-char-non-lit.rs:6:19 + | +LL | let _: &str = ('a'); + | ---- ^^^^^ expected `&str`, found `char` + | | + | expected due to this + +error[E0308]: mismatched types + --> $DIR/str-as-char-non-lit.rs:8:19 + | +LL | let _: &str = token(); + | ---- ^^^^^^^ expected `&str`, found `char` + | | + | expected due to this + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. |
