Skip to content

Commit 86388f6

Browse files
authored
Rollup merge of #95251 - GrishaVar:hashes-u16-to-u8, r=dtolnay
Reduce max hash in raw strings from u16 to u8 [Relevant discussion](https://rust-lang.zulipchat.com/#narrow/stream/237824-t-lang.2Fdoc/topic/Max.20raw.20string.20delimiters)
2 parents 1c3657b + 759d1e6 commit 86388f6

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ pub enum StrStyle {
16161616
/// A raw string, like `r##"foo"##`.
16171617
///
16181618
/// The value is the number of `#` symbols used.
1619-
Raw(u16),
1619+
Raw(u8),
16201620
}
16211621

16221622
/// An AST literal.

compiler/rustc_ast/src/token.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ pub enum LitKind {
5959
Integer,
6060
Float,
6161
Str,
62-
StrRaw(u16), // raw string delimited by `n` hash symbols
62+
StrRaw(u8), // raw string delimited by `n` hash symbols
6363
ByteStr,
64-
ByteStrRaw(u16), // raw byte string delimited by `n` hash symbols
64+
ByteStrRaw(u8), // raw byte string delimited by `n` hash symbols
6565
Err,
6666
}
6767

compiler/rustc_lexer/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ pub enum LiteralKind {
161161
/// "b"abc"", "b"abc"
162162
ByteStr { terminated: bool },
163163
/// "r"abc"", "r#"abc"#", "r####"ab"###"c"####", "r#"a"
164-
RawStr { n_hashes: u16, err: Option<RawStrError> },
164+
RawStr { n_hashes: u8, err: Option<RawStrError> },
165165
/// "br"abc"", "br#"abc"#", "br####"ab"###"c"####", "br#"a"
166-
RawByteStr { n_hashes: u16, err: Option<RawStrError> },
166+
RawByteStr { n_hashes: u8, err: Option<RawStrError> },
167167
}
168168

169169
/// Error produced validating a raw string. Represents cases like:
170170
/// - `r##~"abcde"##`: `InvalidStarter`
171171
/// - `r###"abcde"##`: `NoTerminator { expected: 3, found: 2, possible_terminator_offset: Some(11)`
172-
/// - Too many `#`s (>65535): `TooManyDelimiters`
172+
/// - Too many `#`s (>255): `TooManyDelimiters`
173173
// perf note: It doesn't matter that this makes `Token` 36 bytes bigger. See #77629
174174
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
175175
pub enum RawStrError {
@@ -178,7 +178,7 @@ pub enum RawStrError {
178178
/// The string was never terminated. `possible_terminator_offset` is the number of characters after `r` or `br` where they
179179
/// may have intended to terminate it.
180180
NoTerminator { expected: usize, found: usize, possible_terminator_offset: Option<usize> },
181-
/// More than 65535 `#`s exist.
181+
/// More than 255 `#`s exist.
182182
TooManyDelimiters { found: usize },
183183
}
184184

@@ -698,12 +698,12 @@ impl Cursor<'_> {
698698
}
699699

700700
/// Eats the double-quoted string and returns `n_hashes` and an error if encountered.
701-
fn raw_double_quoted_string(&mut self, prefix_len: usize) -> (u16, Option<RawStrError>) {
701+
fn raw_double_quoted_string(&mut self, prefix_len: usize) -> (u8, Option<RawStrError>) {
702702
// Wrap the actual function to handle the error with too many hashes.
703703
// This way, it eats the whole raw string.
704704
let (n_hashes, err) = self.raw_string_unvalidated(prefix_len);
705-
// Only up to 65535 `#`s are allowed in raw strings
706-
match u16::try_from(n_hashes) {
705+
// Only up to 255 `#`s are allowed in raw strings
706+
match u8::try_from(n_hashes) {
707707
Ok(num) => (num, err),
708708
// We lie about the number of hashes here :P
709709
Err(_) => (0, Some(RawStrError::TooManyDelimiters { found: n_hashes })),

compiler/rustc_lexer/src/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22

33
use expect_test::{expect, Expect};
44

5-
fn check_raw_str(s: &str, expected_hashes: u16, expected_err: Option<RawStrError>) {
5+
fn check_raw_str(s: &str, expected_hashes: u8, expected_err: Option<RawStrError>) {
66
let s = &format!("r{}", s);
77
let mut cursor = Cursor::new(s);
88
cursor.bump();
@@ -68,13 +68,13 @@ fn test_unterminated_no_pound() {
6868

6969
#[test]
7070
fn test_too_many_hashes() {
71-
let max_count = u16::MAX;
71+
let max_count = u8::MAX;
7272
let mut hashes: String = "#".repeat(max_count.into());
7373

74-
// Valid number of hashes (65535 = 2^16 - 1), but invalid string.
74+
// Valid number of hashes (255 = 2^8 - 1 = u8::MAX), but invalid string.
7575
check_raw_str(&hashes, max_count, Some(RawStrError::InvalidStarter { bad_char: '\u{0}' }));
7676

77-
// One more hash sign (65536 = 2^16) becomes too many.
77+
// One more hash sign (256 = 2^8) becomes too many.
7878
hashes.push('#');
7979
check_raw_str(
8080
&hashes,

compiler/rustc_parse/src/lexer/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -597,15 +597,13 @@ impl<'a> StringReader<'a> {
597597
}
598598
}
599599

600-
/// Note: It was decided to not add a test case, because it would be too big.
601-
/// <https://github.com/rust-lang/rust/pull/50296#issuecomment-392135180>
602600
fn report_too_many_hashes(&self, start: BytePos, found: usize) -> ! {
603601
self.fatal_span_(
604602
start,
605603
self.pos,
606604
&format!(
607605
"too many `#` symbols: raw strings may be delimited \
608-
by up to 65535 `#` symbols, but found {}",
606+
by up to 255 `#` symbols, but found {}",
609607
found
610608
),
611609
)

src/tools/clippy/clippy_lints/src/regex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'tcx> LateLintPass<'tcx> for Regex {
8181

8282
#[allow(clippy::cast_possible_truncation)] // truncation very unlikely here
8383
#[must_use]
84-
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
84+
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u8) -> Span {
8585
let offset = u32::from(offset);
8686
let end = base.lo() + BytePos(u32::try_from(c.end.offset).expect("offset too large") + offset);
8787
let start = base.lo() + BytePos(u32::try_from(c.start.offset).expect("offset too large") + offset);

0 commit comments

Comments
 (0)