Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 9 pull requests #94331

Closed
wants to merge 25 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c00f635
Remove in-band lifetimes
compiler-errors Feb 10, 2022
43dbd83
Remove LifetimeDefOrigin
compiler-errors Feb 20, 2022
8ca47d7
Stop manually SIMDing in swap_nonoverlapping
scottmcm Feb 21, 2022
da896d3
Improve CheckCfg internal representation
Urgau Feb 19, 2022
fbe1c15
Add test for well known names defined by rustc
Urgau Feb 19, 2022
3d23477
Improve diagnostic of the unexpected_cfgs lint
Urgau Feb 19, 2022
8d3de56
Continue improvements on the --check-cfg implementation
Urgau Feb 20, 2022
a556a2a
Add compiler flag `--check-cfg` to the unstable book
Urgau Feb 21, 2022
c73a2f8
properly handle fat pointers to uninhabitable types
compiler-errors Feb 23, 2022
f047af2
Normalize main return type during mono item collection & codegen
tmiasko Feb 23, 2022
37d9ea7
Improve `scan_escape`.
nnethercote Feb 24, 2022
44308dc
Inline a hot closure in `from_lit_token`.
nnethercote Feb 24, 2022
70018c1
update auto trait lint
lcnr Feb 24, 2022
34319ff
Avoid emitting full macro body into JSON
Mark-Simulacrum Feb 24, 2022
8ba7436
better ObligationCause for normalization errors in can_type_implement…
compiler-errors Feb 6, 2022
ee98dc8
restore spans for issue-50480
compiler-errors Feb 6, 2022
44de8c0
Rollup merge of #93714 - compiler-errors:can-type-impl-copy-error-spa…
Dylan-DPC Feb 24, 2022
b33098f
Rollup merge of #93845 - compiler-errors:in-band-lifetimes, r=cjgillot
Dylan-DPC Feb 24, 2022
92b5d1d
Rollup merge of #94175 - Urgau:check-cfg-improvements, r=petrochenkov
Dylan-DPC Feb 24, 2022
87f826d
Rollup merge of #94212 - scottmcm:swapper, r=dtolnay
Dylan-DPC Feb 24, 2022
2da2737
Rollup merge of #94242 - compiler-errors:fat-uninhabitable-pointer, r…
Dylan-DPC Feb 24, 2022
c20ed90
Rollup merge of #94308 - tmiasko:normalize-main-ret-ty, r=oli-obk
Dylan-DPC Feb 24, 2022
d4db2be
Rollup merge of #94315 - lcnr:auto-trait-lint-update, r=oli-obk
Dylan-DPC Feb 24, 2022
25cc094
Rollup merge of #94316 - nnethercote:improve-string-literal-unescapin…
Dylan-DPC Feb 24, 2022
39d8195
Rollup merge of #94327 - Mark-Simulacrum:avoid-macro-sp, r=petrochenkov
Dylan-DPC Feb 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve scan_escape.
`scan_escape` currently has a fast path (for when the first char isn't
'\\') and a slow path.

This commit changes `scan_escape` so it only handles the slow path, i.e.
the actual escaping code. The fast path is inlined into the two call
sites.

This change makes the code faster, because there is no function call
overhead on the fast path. (`scan_escape` is a big function and doesn't
get inlined.)

This change also improves readability, because it removes a bunch of
mode checks on the the fast paths.
  • Loading branch information
nnethercote committed Feb 24, 2022
commit 37d9ea745b358a5b9f48560600841fc6619e545d
45 changes: 22 additions & 23 deletions compiler/rustc_lexer/src/unescape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,8 @@ impl Mode {
}
}

fn scan_escape(first_char: char, chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
if first_char != '\\' {
// Previous character was not a slash, and we don't expect it to be
// an escape-only character.
return match first_char {
'\t' | '\n' => Err(EscapeError::EscapeOnlyChar),
'\r' => Err(EscapeError::BareCarriageReturn),
'\'' if mode.in_single_quotes() => Err(EscapeError::EscapeOnlyChar),
'"' if mode.in_double_quotes() => Err(EscapeError::EscapeOnlyChar),
_ => {
if mode.is_bytes() && !first_char.is_ascii() {
// Byte literal can't be a non-ascii character.
return Err(EscapeError::NonAsciiCharInByte);
}
Ok(first_char)
}
};
}

// Previous character is '\\', try to unescape it.
fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
// Previous character was '\\', unescape what follows.

let second_char = chars.next().ok_or(EscapeError::LoneSlash)?;

Expand Down Expand Up @@ -270,9 +252,24 @@ fn scan_escape(first_char: char, chars: &mut Chars<'_>, mode: Mode) -> Result<ch
Ok(res)
}

#[inline]
fn ascii_check(first_char: char, mode: Mode) -> Result<char, EscapeError> {
if mode.is_bytes() && !first_char.is_ascii() {
// Byte literal can't be a non-ascii character.
Err(EscapeError::NonAsciiCharInByte)
} else {
Ok(first_char)
}
}

fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
let first_char = chars.next().ok_or(EscapeError::ZeroChars)?;
let res = scan_escape(first_char, chars, mode)?;
let res = match first_char {
'\\' => scan_escape(chars, mode),
'\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar),
'\r' => Err(EscapeError::BareCarriageReturn),
_ => ascii_check(first_char, mode),
}?;
if chars.next().is_some() {
return Err(EscapeError::MoreThanOneChar);
}
Expand Down Expand Up @@ -303,12 +300,14 @@ where
skip_ascii_whitespace(&mut chars, start, callback);
continue;
}
_ => scan_escape(first_char, &mut chars, mode),
_ => scan_escape(&mut chars, mode),
}
}
'\n' => Ok('\n'),
'\t' => Ok('\t'),
_ => scan_escape(first_char, &mut chars, mode),
'"' => Err(EscapeError::EscapeOnlyChar),
'\r' => Err(EscapeError::BareCarriageReturn),
_ => ascii_check(first_char, mode),
};
let end = initial_len - chars.as_str().len();
callback(start..end, unescaped_char);
Expand Down