Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
c87567c
Add context to compiler error message
Oct 20, 2022
6cb6564
Update tests to match error message changes
Oct 20, 2022
9e36fd9
stabilize `int_log`
Oct 26, 2022
8498e3a
Add examples for `pointer::mask`
WaffleLapkin Oct 28, 2022
f32e678
Rename some variables.
nnethercote Nov 3, 2022
84ca2c3
Clarify range calculations.
nnethercote Nov 3, 2022
34b32b0
Use `Mode` less.
nnethercote Nov 3, 2022
7dbf2c0
Make non-ASCII errors more consistent.
nnethercote Nov 3, 2022
a21c045
Improve comments.
nnethercote Nov 3, 2022
d963686
Refactor `cook_lexer_literal`.
nnethercote Nov 3, 2022
a203482
Inline and remove `validate_int_literal`.
nnethercote Nov 3, 2022
a838952
Remove `unescape_byte_literal`.
nnethercote Nov 4, 2022
43d21b5
Rename some `result` variables as `res`, for consistency.
nnethercote Nov 4, 2022
f67ee43
rustdoc: Add mutable to the description
yancyribbens Nov 7, 2022
8e0cac1
rustdoc: refactor `notable_traits_decl` to just act on the type directly
notriddle Nov 7, 2022
d6c97a3
Simplify `unescape_{char,byte}`.
nnethercote Nov 8, 2022
303653e
rustdoc: use javascript to layout notable traits popups
notriddle Nov 7, 2022
a45151e
rustdoc: fix font color inheritance from body, and test
notriddle Nov 8, 2022
a7a40dd
Simplify some `Autoderef::new` calls
oli-obk Nov 8, 2022
1d93b35
Remove overloaded_span argument from `new`, where it is usually redun…
oli-obk Nov 8, 2022
b745a29
Remove an unused span
oli-obk Nov 8, 2022
ef40824
Update books
ehuss Nov 8, 2022
ba359d8
Add support for custom MIR parsing
JakobDegen Aug 3, 2022
f665847
const Compare Tuples
onestacked Nov 7, 2022
a68ec22
Fix `rustdoc --version` when used with download-rustc
jyn514 Nov 9, 2022
b6c05eb
Cleanup fn trait ref test
onestacked Nov 9, 2022
575f609
Port unknown feature diagnostic to the new framework
SLASHLogin Aug 25, 2022
a54c800
Formatting
SLASHLogin Aug 25, 2022
1c4bd83
locales formatting
SLASHLogin Aug 25, 2022
21b0426
Trailing whitespaces
SLASHLogin Aug 25, 2022
b164790
Change String in structs to &'a str
SLASHLogin Aug 25, 2022
69d412a
Missing lifetime parameter and formatting
SLASHLogin Aug 25, 2022
05ae7ec
Import `error creating import library`
SLASHLogin Aug 25, 2022
4c625dc
Port Instrument coverage requires llvm 12 to the new struct
SLASHLogin Aug 25, 2022
9f0c165
Port `symbol_already_defined` error
SLASHLogin Aug 25, 2022
ec1e101
Fix diag() and formatting
SLASHLogin Aug 25, 2022
59b8aed
Port branch protection on aarch64
SLASHLogin Aug 25, 2022
f031823
Formatting
SLASHLogin Aug 25, 2022
39d363f
Port layout size overflow
SLASHLogin Aug 26, 2022
5d79d3c
Port InvalidMinimumAlignment
SLASHLogin Aug 26, 2022
60ee496
Port LinkageConstOrMutType error
SLASHLogin Aug 26, 2022
02403ee
Reuse SymbolAlreadyDefined
SLASHLogin Aug 26, 2022
978b5f7
Port SanitizerMemtagRequiresMte
SLASHLogin Aug 26, 2022
d32caf9
Port ArchiveBuildFailure
SLASHLogin Aug 26, 2022
ddbb650
Import ErrorWritingDEFFile
SLASHLogin Aug 26, 2022
81f7a8d
Port ErrorCallingDllTool
SLASHLogin Aug 26, 2022
c01546f
Port `DlltoolFailImportLibrary` and implement `IntoDiagnosticArg` for…
SLASHLogin Aug 26, 2022
33ef16f
Port `UnknownArchiveKind`
SLASHLogin Aug 26, 2022
185ef7b
Port `MissingFeatures` and `TargetFeatureDisableOrEnable`
SLASHLogin Aug 26, 2022
1c7a801
Fix CI
SLASHLogin Aug 27, 2022
e9a5329
Correct tests to match errors
SLASHLogin Aug 30, 2022
67394e7
Flatten diagnostic structs
SLASHLogin Oct 30, 2022
3728e95
Port diagnostics created by `Handler`
SLASHLogin Oct 30, 2022
9a15458
Simplify existing Diagnostic implementations
SLASHLogin Oct 30, 2022
b4820a3
Delay diagnostic translation in `rustc_codegen_ssa`
SLASHLogin Nov 2, 2022
3b949eb
Add `replace_args` method for `rustc_errors::diagnostic::Diagnostic`
SLASHLogin Nov 3, 2022
0381e51
Formatting
SLASHLogin Nov 3, 2022
a8a8055
Use `LayoutError`'s implementation of `IntoDiagnostic`
SLASHLogin Nov 4, 2022
0baac88
Update compiler/rustc_codegen_llvm/src/back/archive.rs
SLASHLogin Nov 8, 2022
39895b0
Add constructor for `Diagnostic` that takes `Vec<(DiagnosticMessage, …
SLASHLogin Nov 9, 2022
caada74
Add missing `emitted_at` to the `Diagnostic`
SLASHLogin Nov 9, 2022
edda37a
Rollup merge of #101005 - SLASHLogin:rustc_codegen_llvm_diagnostics, …
Manishearth Nov 9, 2022
bcc1781
Rollup merge of #103307 - b4den:master, r=estebank
Manishearth Nov 9, 2022
25be18b
Rollup merge of #103464 - JakobDegen:mir-parsing, r=oli-obk
Manishearth Nov 9, 2022
147e5df
Rollup merge of #103570 - lukas-code:stabilize-ilog, r=scottmcm
Manishearth Nov 9, 2022
2c75537
Rollup merge of #103694 - WaffleLapkin:mask_doc_example, r=scottmcm
Manishearth Nov 9, 2022
2698b82
Rollup merge of #103919 - nnethercote:unescaping-cleanups, r=matklad
Manishearth Nov 9, 2022
2ad5885
Rollup merge of #104111 - yancyribbens:add-mutable-to-the-description…
Manishearth Nov 9, 2022
57d2005
Rollup merge of #104125 - ink-feather-org:const_cmp_tuples, r=fee1-dead
Manishearth Nov 9, 2022
3a2ab4e
Rollup merge of #104129 - notriddle:notriddle/102576-js-notable-trait…
Manishearth Nov 9, 2022
fd79533
Rollup merge of #104156 - oli-obk:autoderef, r=estebank
Manishearth Nov 9, 2022
ede323e
Rollup merge of #104171 - ehuss:update-books, r=ehuss
Manishearth Nov 9, 2022
aed1890
Rollup merge of #104184 - jyn514:rustdoc-version, r=davidtwco
Manishearth Nov 9, 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
Use Mode less.
It's passed to numerous places where we just need an `is_byte` bool.
Passing the bool avoids the need for some assertions.

Also rename `is_bytes()` as `is_byte()`, to better match `Mode::Byte`,
`Mode::ByteStr`, and `Mode::RawByteStr`.
  • Loading branch information
nnethercote committed Nov 3, 2022
commit 34b32b0dac9da3fad7861bdc2bad89d771172bb3
46 changes: 22 additions & 24 deletions compiler/rustc_lexer/src/unescape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ where
match mode {
Mode::Char | Mode::Byte => {
let mut chars = src.chars();
let result = unescape_char_or_byte(&mut chars, mode);
let result = unescape_char_or_byte(&mut chars, mode == Mode::Byte);
// The Chars iterator moved forward.
callback(0..(src.len() - chars.as_str().len()), result);
}
Mode::Str | Mode::ByteStr => unescape_str_or_byte_str(src, mode, callback),
Mode::Str | Mode::ByteStr => unescape_str_or_byte_str(src, mode == Mode::ByteStr, callback),
// NOTE: Raw strings do not perform any explicit character escaping, here we
// only translate CRLF to LF and produce errors on bare CR.
Mode::RawStr | Mode::RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback),
Mode::RawStr | Mode::RawByteStr => {
unescape_raw_str_or_raw_byte_str(src, mode == Mode::RawByteStr, callback)
}
}
}

Expand All @@ -103,7 +105,7 @@ pub fn unescape_byte_literal<F>(src: &str, mode: Mode, callback: &mut F)
where
F: FnMut(Range<usize>, Result<u8, EscapeError>),
{
debug_assert!(mode.is_bytes());
debug_assert!(mode.is_byte());
unescape_literal(src, mode, &mut |range, result| {
callback(range, result.map(byte_from_char));
})
Expand All @@ -113,15 +115,14 @@ where
/// unescaped char or an error
pub fn unescape_char(src: &str) -> Result<char, (usize, EscapeError)> {
let mut chars = src.chars();
unescape_char_or_byte(&mut chars, Mode::Char)
.map_err(|err| (src.len() - chars.as_str().len(), err))
unescape_char_or_byte(&mut chars, false).map_err(|err| (src.len() - chars.as_str().len(), err))
}

/// Takes a contents of a byte literal (without quotes), and returns an
/// unescaped byte or an error.
pub fn unescape_byte(src: &str) -> Result<u8, (usize, EscapeError)> {
let mut chars = src.chars();
unescape_char_or_byte(&mut chars, Mode::Byte)
unescape_char_or_byte(&mut chars, true)
.map(byte_from_char)
.map_err(|err| (src.len() - chars.as_str().len(), err))
}
Expand All @@ -145,15 +146,15 @@ impl Mode {
}
}

pub fn is_bytes(self) -> bool {
pub fn is_byte(self) -> bool {
match self {
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
Mode::Char | Mode::Str | Mode::RawStr => false,
}
}
}

fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
fn scan_escape(chars: &mut Chars<'_>, is_byte: bool) -> Result<char, EscapeError> {
// Previous character was '\\', unescape what follows.
let res = match chars.next().ok_or(EscapeError::LoneSlash)? {
'"' => '"',
Expand All @@ -176,7 +177,7 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
let value = hi * 16 + lo;

// For a non-byte literal verify that it is within ASCII range.
if !mode.is_bytes() && !is_ascii(value) {
if !is_byte && !is_ascii(value) {
return Err(EscapeError::OutOfRangeHexEscape);
}
let value = value as u8;
Expand Down Expand Up @@ -212,7 +213,7 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {

// Incorrect syntax has higher priority for error reporting
// than unallowed value for a literal.
if mode.is_bytes() {
if is_byte {
return Err(EscapeError::UnicodeEscapeInByte);
}

Expand Down Expand Up @@ -244,23 +245,22 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
}

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

fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
debug_assert!(mode == Mode::Char || mode == Mode::Byte);
fn unescape_char_or_byte(chars: &mut Chars<'_>, is_byte: bool) -> Result<char, EscapeError> {
let c = chars.next().ok_or(EscapeError::ZeroChars)?;
let res = match c {
'\\' => scan_escape(chars, mode),
'\\' => scan_escape(chars, is_byte),
'\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar),
'\r' => Err(EscapeError::BareCarriageReturn),
_ => ascii_check(c, mode),
_ => ascii_check(c, is_byte),
}?;
if chars.next().is_some() {
return Err(EscapeError::MoreThanOneChar);
Expand All @@ -270,11 +270,10 @@ fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, Esca

/// Takes a contents of a string literal (without quotes) and produces a
/// sequence of escaped characters or errors.
fn unescape_str_or_byte_str<F>(src: &str, mode: Mode, callback: &mut F)
fn unescape_str_or_byte_str<F>(src: &str, is_byte: bool, callback: &mut F)
where
F: FnMut(Range<usize>, Result<char, EscapeError>),
{
debug_assert!(mode == Mode::Str || mode == Mode::ByteStr);
let mut chars = src.chars();

// The `start` and `end` computation here is complicated because
Expand All @@ -293,14 +292,14 @@ where
skip_ascii_whitespace(&mut chars, start, callback);
continue;
}
_ => scan_escape(&mut chars, mode),
_ => scan_escape(&mut chars, is_byte),
}
}
'\n' => Ok('\n'),
'\t' => Ok('\t'),
'"' => Err(EscapeError::EscapeOnlyChar),
'\r' => Err(EscapeError::BareCarriageReturn),
_ => ascii_check(c, mode),
_ => ascii_check(c, is_byte),
};
let end = src.len() - chars.as_str().len();
callback(start..end, result);
Expand Down Expand Up @@ -337,11 +336,10 @@ where
/// sequence of characters or errors.
/// NOTE: Raw strings do not perform any explicit character escaping, here we
/// only translate CRLF to LF and produce errors on bare CR.
fn unescape_raw_str_or_raw_byte_str<F>(src: &str, mode: Mode, callback: &mut F)
fn unescape_raw_str_or_raw_byte_str<F>(src: &str, is_byte: bool, callback: &mut F)
where
F: FnMut(Range<usize>, Result<char, EscapeError>),
{
debug_assert!(mode == Mode::RawStr || mode == Mode::RawByteStr);
let mut chars = src.chars();

// The `start` and `end` computation here matches the one in
Expand All @@ -351,7 +349,7 @@ where
let start = src.len() - chars.as_str().len() - c.len_utf8();
let result = match c {
'\r' => Err(EscapeError::BareCarriageReturnInRawString),
c if mode.is_bytes() && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString),
c if is_byte && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString),
c => Ok(c),
};
let end = src.len() - chars.as_str().len();
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub(crate) fn emit_unescape_error(
}

if !has_help {
let (prefix, msg) = if mode.is_bytes() {
let (prefix, msg) = if mode.is_byte() {
("b", "if you meant to write a byte string literal, use double quotes")
} else {
("", "if you meant to write a `str` literal, use double quotes")
Expand Down Expand Up @@ -142,7 +142,7 @@ pub(crate) fn emit_unescape_error(
EscapeError::EscapeOnlyChar => {
let (c, char_span) = last_char();

let msg = if mode.is_bytes() {
let msg = if mode.is_byte() {
"byte constant must be escaped"
} else {
"character constant must be escaped"
Expand Down Expand Up @@ -182,11 +182,11 @@ pub(crate) fn emit_unescape_error(
let (c, span) = last_char();

let label =
if mode.is_bytes() { "unknown byte escape" } else { "unknown character escape" };
if mode.is_byte() { "unknown byte escape" } else { "unknown character escape" };
let ec = escaped_char(c);
let mut diag = handler.struct_span_err(span, &format!("{}: `{}`", label, ec));
diag.span_label(span, label);
if c == '{' || c == '}' && !mode.is_bytes() {
if c == '{' || c == '}' && !mode.is_byte() {
diag.help(
"if used in a formatting string, curly braces are escaped with `{{` and `}}`",
);
Expand All @@ -196,7 +196,7 @@ pub(crate) fn emit_unescape_error(
version control settings",
);
} else {
if !mode.is_bytes() {
if !mode.is_byte() {
diag.span_suggestion(
span_with_quotes,
"if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal",
Expand Down Expand Up @@ -231,7 +231,7 @@ pub(crate) fn emit_unescape_error(
.emit();
}
EscapeError::NonAsciiCharInByte => {
assert!(mode.is_bytes());
assert!(mode.is_byte());
let (c, span) = last_char();
let mut err = handler.struct_span_err(span, "non-ASCII character in byte constant");
let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 {
Expand Down Expand Up @@ -271,7 +271,7 @@ pub(crate) fn emit_unescape_error(
err.emit();
}
EscapeError::NonAsciiCharInByteString => {
assert!(mode.is_bytes());
assert!(mode.is_byte());
let (c, span) = last_char();
let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 {
format!(" but is {:?}", c)
Expand Down