Skip to content

Commit 1a6bda6

Browse files
committed
Auto merge of #51075 - estebank:and_the_case_of_the_confusable_float_exponent, r=eddyb
Check for confusable Unicode chars in float literal exponent Fixing tests for #49989. Resolves #49746.
2 parents f0805a4 + 7dec8a4 commit 1a6bda6

5 files changed

+45
-8
lines changed

src/libsyntax/parse/lexer/mod.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,18 @@ 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+
if unicode_chars::check_for_substitution(self, ch, &mut err) {
1085+
self.bump();
1086+
self.scan_digits(10, 10);
1087+
}
1088+
}
1089+
err.emit();
10811090
}
10821091
}
10831092
}

src/libsyntax/parse/lexer/unicode_chars.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ const ASCII_ARRAY: &'static [(char, &'static str)] = &[
335335

336336
pub fn check_for_substitution<'a>(reader: &StringReader<'a>,
337337
ch: char,
338-
err: &mut DiagnosticBuilder<'a>) {
338+
err: &mut DiagnosticBuilder<'a>) -> bool {
339339
UNICODE_ARRAY
340340
.iter()
341341
.find(|&&(c, _, _)| c == ch)
@@ -344,14 +344,16 @@ 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());
350+
true
350351
},
351352
None => {
352353
let msg = format!("substitution character not found for '{}'", ch);
353354
reader.sess.span_diagnostic.span_bug_no_panic(span, &msg);
355+
false
354356
}
355357
}
356-
});
358+
}).unwrap_or(false)
357359
}

src/test/parse-fail/unicode-chars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
fn main() {
1414
let y = 0;
1515
//~^ ERROR unknown start of token: \u{37e}
16-
//~^^ HELP unicode character ';' (Greek Question Mark) looks like ';' (Semicolon), but it's not
16+
//~^^ HELP Unicode character ';' (Greek Question Mark) looks like ';' (Semicolon), but it is not
1717
}
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: f64 = 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:53
3+
|
4+
LL | const UNIVERSAL_GRAVITATIONAL_CONSTANT: f64 = 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: f64 = 6.674e-11; // m³⋅kg⁻¹⋅s⁻²
9+
| ^
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)