diff options
| author | bors <bors@rust-lang.org> | 2017-12-15 08:13:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-12-15 08:13:37 +0000 |
| commit | 04b23449c0eac1923472a117a3bc57eab8ff1868 (patch) | |
| tree | b80a3382f87eb12f2b520fa5d70d1c0b0696df5b /src/libsyntax/parse | |
| parent | 84feab34e455722061bb55260eafd1eb02a538fa (diff) | |
| parent | c60aab29f1a84b799d4b2a27ed26eeda29438eda (diff) | |
| download | rust-04b23449c0eac1923472a117a3bc57eab8ff1868.tar.gz rust-04b23449c0eac1923472a117a3bc57eab8ff1868.zip | |
Auto merge of #46653 - estebank:str-as-ch, r=petrochenkov
When attempting to write str with single quote suggest double quotes Fix #26101.
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/lexer/mod.rs | 28 |
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"))); } |
