Skip to content

Commit

Permalink
Auto merge of rust-lang#51075 - estebank:and_the_case_of_the_confusab…
Browse files Browse the repository at this point in the history
…le_float_exponent, r=eddyb

Check for confusable Unicode chars in float literal exponent

Fixing tests for rust-lang#49989. Resolves rust-lang#49746.
  • Loading branch information
bors committed May 27, 2018
2 parents f0805a4 + 7dec8a4 commit 1a6bda6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,18 @@ impl<'a> StringReader<'a> {
self.bump();
}
if self.scan_digits(10, 10) == 0 {
self.err_span_(self.pos,
self.next_pos,
"expected at least one digit in exponent")
let mut err = self.struct_span_fatal(
self.pos, self.next_pos,
"expected at least one digit in exponent"
);
if let Some(ch) = self.ch {
// check for e.g. Unicode minus '−' (Issue #49746)
if unicode_chars::check_for_substitution(self, ch, &mut err) {
self.bump();
self.scan_digits(10, 10);
}
}
err.emit();
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/libsyntax/parse/lexer/unicode_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ const ASCII_ARRAY: &'static [(char, &'static str)] = &[

pub fn check_for_substitution<'a>(reader: &StringReader<'a>,
ch: char,
err: &mut DiagnosticBuilder<'a>) {
err: &mut DiagnosticBuilder<'a>) -> bool {
UNICODE_ARRAY
.iter()
.find(|&&(c, _, _)| c == ch)
Expand All @@ -344,14 +344,16 @@ pub fn check_for_substitution<'a>(reader: &StringReader<'a>,
match ASCII_ARRAY.iter().find(|&&(c, _)| c == ascii_char) {
Some(&(ascii_char, ascii_name)) => {
let msg =
format!("unicode character '{}' ({}) looks like '{}' ({}), but it's not",
format!("Unicode character '{}' ({}) looks like '{}' ({}), but it is not",
ch, u_name, ascii_char, ascii_name);
err.span_help(span, &msg);
err.span_suggestion(span, &msg, ascii_char.to_string());
true
},
None => {
let msg = format!("substitution character not found for '{}'", ch);
reader.sess.span_diagnostic.span_bug_no_panic(span, &msg);
false
}
}
});
}).unwrap_or(false)
}
2 changes: 1 addition & 1 deletion src/test/parse-fail/unicode-chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
fn main() {
let y = 0;
//~^ ERROR unknown start of token: \u{37e}
//~^^ HELP unicode character ';' (Greek Question Mark) looks like ';' (Semicolon), but it's not
//~^^ HELP Unicode character ';' (Greek Question Mark) looks like ';' (Semicolon), but it is not
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

const UNIVERSAL_GRAVITATIONAL_CONSTANT: f64 = 6.674e−11; // m³⋅kg⁻¹⋅s⁻²
//~^ ERROR expected at least one digit in exponent

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: expected at least one digit in exponent
--> $DIR/issue-49746-unicode-confusable-in-float-literal-expt.rs:11:53
|
LL | const UNIVERSAL_GRAVITATIONAL_CONSTANT: f64 = 6.674e−11; // m³⋅kg⁻¹⋅s⁻²
| ^
help: Unicode character '−' (Minus Sign) looks like '-' (Minus/Hyphen), but it is not
|
LL | const UNIVERSAL_GRAVITATIONAL_CONSTANT: f64 = 6.674e-11; // m³⋅kg⁻¹⋅s⁻²
| ^

error: aborting due to previous error

0 comments on commit 1a6bda6

Please sign in to comment.