-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Refine when invalid prefix case error arises #105161
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
Conversation
r? @fee1-dead (rustbot has picked a reviewer for you, use r? to override) |
r? @nnethercote |
This comment has been minimized.
This comment has been minimized.
6a894ca
to
33d1f80
Compare
This comment has been minimized.
This comment has been minimized.
Ah, I apparently lost some progress in an Emacs crash. Will fix these errors. |
compiler/rustc_session/src/errors.rs
Outdated
@@ -324,7 +341,8 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span: | |||
if looks_like_width_suffix(&['i', 'u'], &suf) { | |||
// If it looks like a width, try to be helpful. | |||
sess.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() }); | |||
} else if let Some(fixed) = fix_base_capitalisation(suf) { | |||
} else if let Some(fixed) = fix_base_capitalisation(suf) | |||
&& looks_like_base_prefixed_number(symbol.as_str(), fix_base_capitalisation(suf)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The repeated call to fix_base_capitalisation
is a bit clumsy. I think you can change looks_like_base_prefix_number
to look for an upper-case X
/O
/B
, and then call just it in the condition, and then call fix_base_capitalisation
in the body?
compiler/rustc_session/src/errors.rs
Outdated
@@ -291,6 +291,23 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span: | |||
s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit()) | |||
} | |||
|
|||
// Check if a prefix and suffix look correct irrespective of base. | |||
fn looks_like_base_prefixed_number(prefix: &str, suffix: &str) -> bool { | |||
let base = match suffix.chars().next().unwrap() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid the repeated suffix.chars()
in this function. Do let chars = suffix.chars()
, then do chars.next().unwrap()
for the base check, then do chars().filter(...)...
in the second part (thus avoiding the skip(1)
).
33d1f80
to
0f548f6
Compare
Issues mentioned have been fixed! Thanks for the review so far. |
Apologies: due to the combination of communication in this PR and via Zulip, and some initial incorrect suggestions I made, you've ended up making changes that I didn't intend you to make. Let me try to clarify:
Sorry! I hope that's clearer. |
0f548f6
to
e6dd291
Compare
Apologies for the confusion! I went ahead and merged the behavior of |
c12d230
to
f59655f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there! Just two minor suggestions.
Fix cases where the "invalid base prefix for number literal" error arises with suffixes that look erroneously capitalized but which are in fact invalid.
f59655f
to
52a9280
Compare
Okay, those changes have been applied. |
Looks good, thanks! @bors r+ |
This is small enough to be easily included in a rollup. @bors rollup |
…r=nnethercote Refine when invalid prefix case error arises Fix cases where the "invalid base prefix for number literal" error arises with suffixes that look erroneously capitalized but which are actually invalid.
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#103644 (Add docs for question mark operator for Option) - rust-lang#105161 (Refine when invalid prefix case error arises) - rust-lang#105491 (Illegal sized bounds: only suggest mutability change if needed) - rust-lang#105502 (Suggest impl in the scenario of typo with fn) - rust-lang#105523 (Suggest `collect`ing into `Vec<_>`) - rust-lang#105595 (Suggest dereferencing receiver arguments properly) - rust-lang#105611 (fold instead of obliterating args) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Fix cases where the "invalid base prefix for number literal" error arises with suffixes that look erroneously capitalized but which are actually invalid.