summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-10-30 01:10:33 +0000
committerJosh Stone <jistone@redhat.com>2024-11-14 16:17:05 -0800
commit5f7387c37e05bb174bd430febbcc87e331d17cbb (patch)
treed8ed83f02bb39ac9ff5b69272a526de49a94cdc6
parentf0db79f31ada049bf490c8f27be7443f45460f09 (diff)
downloadrust-5f7387c37e05bb174bd430febbcc87e331d17cbb.tar.gz
rust-5f7387c37e05bb174bd430febbcc87e331d17cbb.zip
Reject raw lifetime followed by \' as well
(cherry picked from commit 1990f1560801ca3f9e6a3286e58204aa329ee037)
-rw-r--r--compiler/rustc_lexer/src/lib.rs12
-rw-r--r--tests/ui/lifetimes/raw/immediately-followed-by-lt.rs14
-rw-r--r--tests/ui/lifetimes/raw/immediately-followed-by-lt.stderr13
3 files changed, 38 insertions, 1 deletions
diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs
index b0ab50dd773..f9f2a14dbd2 100644
--- a/compiler/rustc_lexer/src/lib.rs
+++ b/compiler/rustc_lexer/src/lib.rs
@@ -715,7 +715,17 @@ impl Cursor<'_> {
             self.bump();
             self.bump();
             self.eat_while(is_id_continue);
-            return RawLifetime;
+            match self.first() {
+                '\'' => {
+                    // Check if after skipping literal contents we've met a closing
+                    // single quote (which means that user attempted to create a
+                    // string with single quotes).
+                    self.bump();
+                    let kind = Char { terminated: true };
+                    return Literal { kind, suffix_start: self.pos_within_token() };
+                }
+                _ => return RawLifetime,
+            }
         }
 
         // Either a lifetime or a character literal with
diff --git a/tests/ui/lifetimes/raw/immediately-followed-by-lt.rs b/tests/ui/lifetimes/raw/immediately-followed-by-lt.rs
new file mode 100644
index 00000000000..fe2b6de7bb3
--- /dev/null
+++ b/tests/ui/lifetimes/raw/immediately-followed-by-lt.rs
@@ -0,0 +1,14 @@
+//@ edition: 2021
+
+// Make sure we reject the case where a raw lifetime is immediately followed by another
+// lifetime. This reserves a modest amount of space for changing lexing to, for example,
+// delay rejection of overlong char literals like `'r#long'id`.
+
+macro_rules! w {
+    ($($tt:tt)*) => {}
+}
+
+w!('r#long'id);
+//~^ ERROR character literal may only contain one codepoint
+
+fn main() {}
diff --git a/tests/ui/lifetimes/raw/immediately-followed-by-lt.stderr b/tests/ui/lifetimes/raw/immediately-followed-by-lt.stderr
new file mode 100644
index 00000000000..1caeec84b22
--- /dev/null
+++ b/tests/ui/lifetimes/raw/immediately-followed-by-lt.stderr
@@ -0,0 +1,13 @@
+error: character literal may only contain one codepoint
+  --> $DIR/immediately-followed-by-lt.rs:11:4
+   |
+LL | w!('r#long'id);
+   |    ^^^^^^^^
+   |
+help: if you meant to write a string literal, use double quotes
+   |
+LL | w!("r#long"id);
+   |    ~      ~
+
+error: aborting due to 1 previous error
+