Skip to content

Commit 5ea6d02

Browse files
committed
Tighten up float literal lexing.
Specifically, dissallow setting the number base for every type of float literal, not only those that contain the decimal point. This is in line with the description in the manual.
1 parent 4bdda35 commit 5ea6d02

File tree

10 files changed

+133
-11
lines changed

10 files changed

+133
-11
lines changed

src/libsyntax/parse/lexer.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,19 @@ fn scan_digits(rdr: @StringReader, radix: uint) -> ~str {
463463
};
464464
}
465465

466+
fn check_float_base(rdr: @StringReader, start_bpos: BytePos, last_bpos: BytePos,
467+
base: uint) {
468+
match base {
469+
16u => fatal_span(rdr, start_bpos, last_bpos,
470+
~"hexadecimal float literal is not supported"),
471+
8u => fatal_span(rdr, start_bpos, last_bpos,
472+
~"octal float literal is not supported"),
473+
2u => fatal_span(rdr, start_bpos, last_bpos,
474+
~"binary float literal is not supported"),
475+
_ => ()
476+
}
477+
}
478+
466479
fn scan_number(c: char, rdr: @StringReader) -> token::Token {
467480
let mut num_str;
468481
let mut base = 10u;
@@ -540,17 +553,6 @@ fn scan_number(c: char, rdr: @StringReader) -> token::Token {
540553
num_str.push_char('.');
541554
num_str.push_str(dec_part);
542555
}
543-
if is_float {
544-
match base {
545-
16u => fatal_span(rdr, start_bpos, rdr.last_pos.get(),
546-
~"hexadecimal float literal is not supported"),
547-
8u => fatal_span(rdr, start_bpos, rdr.last_pos.get(),
548-
~"octal float literal is not supported"),
549-
2u => fatal_span(rdr, start_bpos, rdr.last_pos.get(),
550-
~"binary float literal is not supported"),
551-
_ => ()
552-
}
553-
}
554556
match scan_exponent(rdr, start_bpos) {
555557
Some(ref s) => {
556558
is_float = true;
@@ -566,10 +568,12 @@ fn scan_number(c: char, rdr: @StringReader) -> token::Token {
566568
if c == '3' && n == '2' {
567569
bump(rdr);
568570
bump(rdr);
571+
check_float_base(rdr, start_bpos, rdr.last_pos.get(), base);
569572
return token::LIT_FLOAT(str_to_ident(num_str), ast::TyF32);
570573
} else if c == '6' && n == '4' {
571574
bump(rdr);
572575
bump(rdr);
576+
check_float_base(rdr, start_bpos, rdr.last_pos.get(), base);
573577
return token::LIT_FLOAT(str_to_ident(num_str), ast::TyF64);
574578
/* FIXME (#2252): if this is out of range for either a
575579
32-bit or 64-bit float, it won't be noticed till the
@@ -580,6 +584,7 @@ fn scan_number(c: char, rdr: @StringReader) -> token::Token {
580584
}
581585
}
582586
if is_float {
587+
check_float_base(rdr, start_bpos, rdr.last_pos.get(), base);
583588
return token::LIT_FLOAT_UNSUFFIXED(str_to_ident(num_str));
584589
} else {
585590
if num_str.len() == 0u {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let a = 0o1.0; //~ ERROR: octal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let b = 0o2f32; //~ ERROR: octal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let c = 0o3.0f32; //~ ERROR: octal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let d = 0o4e4; //~ ERROR: octal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let e = 0o5.0e5; //~ ERROR: octal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let f = 0o6e6f32; //~ ERROR: octal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let g = 0o7.0e7f64; //~ ERROR: octal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let h = 0x8.0e+9; //~ ERROR: hexadecimal float literal is not supported
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 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+
fn main() {
12+
let i = 0x9.0e-9; //~ ERROR: hexadecimal float literal is not supported
13+
}

0 commit comments

Comments
 (0)