From 630d5f355fc85fc2c3bab28a278c517d945d328d Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Mon, 10 Jun 2019 17:32:15 +0200 Subject: [PATCH] Don't suggest using \r in raw strings --- src/libsyntax/parse/unescape.rs | 3 ++- src/libsyntax/parse/unescape_error_reporting.rs | 5 +++++ src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.rs | 2 +- .../ui/parser/lex-bare-cr-string-literal-doc-comment.stderr | 2 +- src/test/ui/parser/raw-byte-string-literals.rs | 2 +- src/test/ui/parser/raw-byte-string-literals.stderr | 2 +- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/parse/unescape.rs b/src/libsyntax/parse/unescape.rs index e816aa0271cf6..22cce67b5eeb7 100644 --- a/src/libsyntax/parse/unescape.rs +++ b/src/libsyntax/parse/unescape.rs @@ -12,6 +12,7 @@ pub(crate) enum EscapeError { LoneSlash, InvalidEscape, BareCarriageReturn, + BareCarriageReturnInRawString, EscapeOnlyChar, TooShortHexEscape, @@ -299,7 +300,7 @@ where chars.next(); Ok('\n') }, - ('\r', _) => Err(EscapeError::BareCarriageReturn), + ('\r', _) => Err(EscapeError::BareCarriageReturnInRawString), (c, _) if mode.is_bytes() && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString), (c, _) => Ok(c), diff --git a/src/libsyntax/parse/unescape_error_reporting.rs b/src/libsyntax/parse/unescape_error_reporting.rs index 8f152974a6d3f..71b41161ad8c6 100644 --- a/src/libsyntax/parse/unescape_error_reporting.rs +++ b/src/libsyntax/parse/unescape_error_reporting.rs @@ -80,6 +80,11 @@ pub(crate) fn emit_unescape_error( }; handler.span_err(span, msg); } + EscapeError::BareCarriageReturnInRawString => { + assert!(mode.in_double_quotes()); + let msg = "bare CR not allowed in raw string"; + handler.span_err(span, msg); + } EscapeError::InvalidEscape => { let (c, span) = last_char(); diff --git a/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.rs b/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.rs index ed5df42f9dd4e..b588b007ae929 100644 --- a/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.rs +++ b/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.rs @@ -21,7 +21,7 @@ fn main() { let _s = "foo bar"; //~ ERROR: bare CR not allowed in string // the following string literal has a bare CR in it - let _s = r"bar foo"; //~ ERROR: bare CR not allowed in string + let _s = r"bar foo"; //~ ERROR: bare CR not allowed in raw string // the following string literal has a bare CR in it let _s = "foo\ bar"; //~ ERROR: unknown character escape: \r diff --git a/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.stderr b/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.stderr index 153237a7f71b4..b0fe4b6acd484 100644 --- a/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.stderr +++ b/src/test/ui/parser/lex-bare-cr-string-literal-doc-comment.stderr @@ -28,7 +28,7 @@ error: bare CR not allowed in string, use \r instead LL | let _s = "foo bar"; | ^ -error: bare CR not allowed in string, use \r instead +error: bare CR not allowed in raw string --> $DIR/lex-bare-cr-string-literal-doc-comment.rs:24:19 | LL | let _s = r"bar foo"; diff --git a/src/test/ui/parser/raw-byte-string-literals.rs b/src/test/ui/parser/raw-byte-string-literals.rs index 87ecfb5c5445c..534afabdf777e 100644 --- a/src/test/ui/parser/raw-byte-string-literals.rs +++ b/src/test/ui/parser/raw-byte-string-literals.rs @@ -1,7 +1,7 @@ // ignore-tidy-cr // compile-flags: -Z continue-parse-after-error pub fn main() { - br"a "; //~ ERROR bare CR not allowed in string + br"a "; //~ ERROR bare CR not allowed in raw string br"é"; //~ ERROR raw byte string must be ASCII br##~"a"~##; //~ ERROR only `#` is allowed in raw string delimitation } diff --git a/src/test/ui/parser/raw-byte-string-literals.stderr b/src/test/ui/parser/raw-byte-string-literals.stderr index 03fe79722b844..4880d1fdbe8a7 100644 --- a/src/test/ui/parser/raw-byte-string-literals.stderr +++ b/src/test/ui/parser/raw-byte-string-literals.stderr @@ -1,4 +1,4 @@ -error: bare CR not allowed in string, use \r instead +error: bare CR not allowed in raw string --> $DIR/raw-byte-string-literals.rs:4:9 | LL | br"a ";