about summary refs log tree commit diff
path: root/src/libsyntax/parse/lexer
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2017-12-10 23:35:53 -0800
committerEsteban Küber <esteban@kuber.com.ar>2017-12-14 22:51:42 -0800
commitc60aab29f1a84b799d4b2a27ed26eeda29438eda (patch)
tree0e92783df5f21256b5059f20f9eed554364b3d90 /src/libsyntax/parse/lexer
parent933103190950c97b966e789e9206bff2f7bd6118 (diff)
downloadrust-c60aab29f1a84b799d4b2a27ed26eeda29438eda.tar.gz
rust-c60aab29f1a84b799d4b2a27ed26eeda29438eda.zip
When attempting to write str with single quote suggest double quotes
Diffstat (limited to 'src/libsyntax/parse/lexer')
-rw-r--r--src/libsyntax/parse/lexer/mod.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs
index 798dfc6d209..1e84fb98a66 100644
--- a/src/libsyntax/parse/lexer/mod.rs
+++ b/src/libsyntax/parse/lexer/mod.rs
@@ -1306,8 +1306,34 @@ impl<'a> StringReader<'a> {
                                                    '\'');
 
                 if !self.ch_is('\'') {
+                    let pos = self.pos;
+                    loop {
+                        self.bump();
+                        if self.ch_is('\'') {
+                            let start = self.byte_offset(start).to_usize();
+                            let end = self.byte_offset(self.pos).to_usize();
+                            self.bump();
+                            let span = self.mk_sp(start_with_quote, self.pos);
+                            self.sess.span_diagnostic
+                                .struct_span_err(span,
+                                                 "character literal may only contain one codepoint")
+                                .span_suggestion(span,
+                                                 "if you meant to write a `str` literal, \
+                                                  use double quotes",
+                                                 format!("\"{}\"",
+                                                         &self.source_text[start..end]))
+                                .emit();
+                            return Ok(token::Literal(token::Str_(Symbol::intern("??")), None))
+                        }
+                        if self.ch_is('\n') || self.is_eof() || self.ch_is('/') {
+                            // Only attempt to infer single line string literals. If we encounter
+                            // a slash, bail out in order to avoid nonsensical suggestion when
+                            // involving comments.
+                            break;
+                        }
+                    }
                     panic!(self.fatal_span_verbose(
-                           start_with_quote, self.pos,
+                           start_with_quote, pos,
                            String::from("character literal may only contain one codepoint")));
                 }