Skip to content

Commit 6437295

Browse files
zackmdavisestebank
authored andcommitted
in which we check for confusable Unicodepoints in float literal exponent
The `FatalError.raise()` might seem unmotivated (in most places in the compiler, `err.emit()` suffices), but it's actually used to maintain behavior (viz., stop lexing, don't emit potentially spurious errors looking for the next token after the bad Unicodepoint in the exponent): the previous revision's `self.err_span_` ultimately calls `Handler::emit`, which aborts if the `Handler`'s continue_after_error flag is set, which seems to typically be true during lexing (see `phase_1_parse_input` and and how `CompileController::basic` has `continue_parse_after_error: false` in librustc_driver). Also, let's avoid apostrophes in error messages (the present author would argue that users expect a reassuringly detached, formal, above-it-all tone from a Serious tool like a compiler), and use an RLS-friendly structured suggestion. Resolves #49746.
1 parent 7426f5c commit 6437295

4 files changed

+38
-5
lines changed

src/libsyntax/parse/lexer/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,16 @@ impl<'a> StringReader<'a> {
10751075
self.bump();
10761076
}
10771077
if self.scan_digits(10, 10) == 0 {
1078-
self.err_span_(self.pos,
1079-
self.next_pos,
1080-
"expected at least one digit in exponent")
1078+
let mut err = self.struct_span_fatal(
1079+
self.pos, self.next_pos,
1080+
"expected at least one digit in exponent"
1081+
);
1082+
if let Some(ch) = self.ch {
1083+
// check for e.g. Unicode minus '−' (Issue #49746)
1084+
unicode_chars::check_for_substitution(self, ch, &mut err);
1085+
}
1086+
err.emit();
1087+
FatalError.raise();
10811088
}
10821089
}
10831090
}

src/libsyntax/parse/lexer/unicode_chars.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ pub fn check_for_substitution<'a>(reader: &StringReader<'a>,
344344
match ASCII_ARRAY.iter().find(|&&(c, _)| c == ascii_char) {
345345
Some(&(ascii_char, ascii_name)) => {
346346
let msg =
347-
format!("unicode character '{}' ({}) looks like '{}' ({}), but it's not",
347+
format!("Unicode character '{}' ({}) looks like '{}' ({}), but it is not",
348348
ch, u_name, ascii_char, ascii_name);
349-
err.span_help(span, &msg);
349+
err.span_suggestion(span, &msg, ascii_char.to_string());
350350
},
351351
None => {
352352
let msg = format!("substitution character not found for '{}'", ch);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const UNIVERSAL_GRAVITATIONAL_CONSTANT = 6.674e−11; // m³⋅kg⁻¹⋅s⁻²
12+
//~^ ERROR expected at least one digit in exponent
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: expected at least one digit in exponent
2+
--> $DIR/issue-49746-unicode-confusable-in-float-literal-expt.rs:11:48
3+
|
4+
LL | const UNIVERSAL_GRAVITATIONAL_CONSTANT = 6.674e−11; // m³⋅kg⁻¹⋅s⁻²
5+
| ^
6+
help: Unicode character '−' (Minus Sign) looks like '-' (Minus/Hyphen), but it is not
7+
|
8+
LL | const UNIVERSAL_GRAVITATIONAL_CONSTANT = 6.674e-11; // m³⋅kg⁻¹⋅s⁻²
9+
| ^
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)