scanner: no longer allow `1.` float literals (#11301)

pull/11311/head
yuyi 2021-08-25 19:39:37 +08:00 committed by GitHub
parent ee961b26e7
commit 4d078edb72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 30 additions and 19 deletions

View File

@ -745,7 +745,7 @@ pub fn dpi_scale() f32 {
// NB: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses, // NB: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses,
// may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0 // may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0
if s < 0.1 { if s < 0.1 {
s = 1. s = 1.0
} }
return s return s
} }

View File

@ -53,7 +53,7 @@ fn test_float_to_str() {
1e23, 1e23,
f32_from_bits1(0x0080_0000), // smallest float32 f32_from_bits1(0x0080_0000), // smallest float32
math.max_f32, math.max_f32,
383260575764816448., 383260575764816448.0,
] ]
exp_result_f32 := [ exp_result_f32 := [

View File

@ -1,7 +1,7 @@
const n = 1000 const n = 1000
fn f(ch chan f64) { fn f(ch chan f64) {
mut s := 0. mut s := 0.0
for _ in 0 .. n { for _ in 0 .. n {
s += <-ch s += <-ch
} }

View File

@ -6,8 +6,8 @@
fn do_select(ch1 chan int, ch2 chan int, chf1 chan f64, chf2 chan f64, sumch1 chan i64, sumch2 chan i64) { fn do_select(ch1 chan int, ch2 chan int, chf1 chan f64, chf2 chan f64, sumch1 chan i64, sumch2 chan i64) {
mut sum1 := i64(0) mut sum1 := i64(0)
mut sum2 := i64(0) mut sum2 := i64(0)
f1 := 17. f1 := 17.0
f2 := 7. f2 := 7.0
for _ in 0 .. 20000 + chf1.cap / 3 { for _ in 0 .. 20000 + chf1.cap / 3 {
select { select {
chf1 <- f1 {} chf1 <- f1 {}
@ -31,7 +31,7 @@ fn do_send_int(ch chan int, factor int) {
} }
fn do_rec_f64(ch chan f64, sumch chan f64) { fn do_rec_f64(ch chan f64, sumch chan f64) {
mut sum := 0. mut sum := 0.0
for _ in 0 .. 10000 { for _ in 0 .. 10000 {
sum += <-ch sum += <-ch
} }
@ -70,6 +70,6 @@ fn test_select() {
expected_sum := i64(10000) * (10000 - 1) / 2 expected_sum := i64(10000) * (10000 - 1) / 2
assert sum1 == 3 * expected_sum assert sum1 == 3 * expected_sum
assert sum2 == (7 + 17) * expected_sum assert sum2 == (7 + 17) * expected_sum
assert sumf1 == 17. * f64(10000 + chf1.cap) assert sumf1 == 17.0 * f64(10000 + chf1.cap)
assert sumf2 == 7. * 20000 assert sumf2 == 7.0 * 20000
} }

View File

@ -19,7 +19,7 @@ fn abc() {
1e23, 1e23,
f32_from_bits1(0x0080_0000), // smallest float32 f32_from_bits1(0x0080_0000), // smallest float32
math.max_f32, math.max_f32,
383260575764816448., 383260575764816448.0,
] ]
exp_result_f32 := [ exp_result_f32 := [

View File

@ -438,6 +438,7 @@ fn (mut s Scanner) ident_dec_number() string {
s.pos-- s.pos--
} else { } else {
// 5. // 5.
s.error('float literals should have a digit after the decimal point, e.g. `5.0`')
} }
} }
} }

View File

@ -45,7 +45,7 @@ fn test_float_conversion_and_reading() {
mut e := 1.2E3 * -1e-1 mut e := 1.2E3 * -1e-1
assert e == -120.0 assert e == -120.0
e = 1.2E3 * 1e-1 e = 1.2E3 * 1e-1
x := 55. x := 55.0
assert e == 120.0 assert e == 120.0
assert 1.23e+10 == 1.23e10 assert 1.23e+10 == 1.23e10
assert 1.23e+10 == 1.23e0010 assert 1.23e+10 == 1.23e0010
@ -54,18 +54,18 @@ fn test_float_conversion_and_reading() {
} }
fn test_float_without_fraction() { fn test_float_without_fraction() {
mut result := scan_kinds('x := 10.') mut result := scan_kinds('x := 10.0')
assert result.len == 3 assert result.len == 3
assert result[0] == .name assert result[0] == .name
assert result[1] == .decl_assign assert result[1] == .decl_assign
assert result[2] == .number assert result[2] == .number
result = scan_kinds('return 3., 4.') result = scan_kinds('return 3.0, 4.0')
assert result.len == 4 assert result.len == 4
assert result[0] == .key_return assert result[0] == .key_return
assert result[1] == .number assert result[1] == .number
assert result[2] == .comma assert result[2] == .comma
assert result[3] == .number assert result[3] == .number
result = scan_kinds('fun(5.)') result = scan_kinds('fun(5.0)')
assert result.len == 4 assert result.len == 4
assert result[0] == .name assert result[0] == .name
assert result[1] == .lpar assert result[1] == .lpar

View File

@ -0,0 +1,6 @@
vlib/v/scanner/tests/float_literals_dot_err.vv:2:9: error: float literals should have a digit after the decimal point, e.g. `5.0`
1 | fn main() {
2 | a := 1.
| ^
3 | println(a)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
a := 1.
println(a)
}

View File

@ -97,13 +97,13 @@ fn test_for_in_mut_val_of_string() {
} }
fn test_for_in_mut_val_of_float() { fn test_for_in_mut_val_of_float() {
mut values := [1., 2, 3] mut values := [1.0, 2, 3]
println(values) println(values)
for mut v in values { for mut v in values {
v = 1. v = 1.0
v = v + 1. v = v + 1.0
} }
println(values) println(values)
assert values == [2., 2, 2] assert values == [2.0, 2, 2]
} }

View File

@ -20,7 +20,7 @@ fn test_assign_mut() {
mut c := Foo(f32(1)) mut c := Foo(f32(1))
a = 12.3 a = 12.3
b = -123456 b = -123456
c = 33. c = 33.0
assert a is f64 assert a is f64
assert b is int assert b is int
assert c is f64 assert c is f64
@ -75,7 +75,7 @@ fn gen_foo(n int) Foo {
return -17.3e23 return -17.3e23
} }
if n == 2 { if n == 2 {
return 32. return 32.0
} }
if n == 3 { if n == 3 {
return i16(13) return i16(13)