about summary refs log tree commit diff
path: root/compiler/rustc_infer
diff options
context:
space:
mode:
authorNick Lamb <git@tree.tlrmx.org>2023-01-14 14:51:17 +0000
committerNick Lamb <git@tree.tlrmx.org>2023-01-14 21:27:14 +0000
commit130d02b62e65c5f2a434eaec63c4249e9d508487 (patch)
tree00831f01020403ed503ca559cd8abeb15a2c1085 /compiler/rustc_infer
parent44a500c8c187b245638684748f54bd6ec67e0b25 (diff)
downloadrust-130d02b62e65c5f2a434eaec63c4249e9d508487.tar.gz
rust-130d02b62e65c5f2a434eaec63c4249e9d508487.zip
Improve E0308: suggest user meant to use byte literal, w/ tests and fix
suggested by Nilstrieb

Co-authored-by: nils <48135649+Nilstrieb@users.noreply.github.com>
Diffstat (limited to 'compiler/rustc_infer')
-rw-r--r--compiler/rustc_infer/src/infer/error_reporting/mod.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs
index 533a3c768eb..abd99fc74da 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs
@@ -1923,6 +1923,22 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
                         (ty::Tuple(fields), _) => {
                             self.emit_tuple_wrap_err(&mut err, span, found, fields)
                         }
+                        // If a byte was expected and the found expression is a char literal
+                        // containing a single ASCII character, perhaps the user meant to write `b'c'` to
+                        // specify a byte literal
+                        (ty::Uint(ty::UintTy::U8), ty::Char) => {
+                            if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
+                                && let Some(code) = code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
+                                && code.chars().next().map_or(false, |c| c.is_ascii())
+                            {
+                                err.span_suggestion(
+                                    span,
+                                    "if you meant to write a byte literal, prefix with `b`",
+                                    format!("b'{}'", escape_literal(code)),
+                                    Applicability::MachineApplicable,
+                                );
+                            }
+                        }
                         // If a character was expected and the found expression is a string literal
                         // containing a single character, perhaps the user meant to write `'c'` to
                         // specify a character literal (issue #92479)