cmd/compile: reject p-notation floats in Go source files · golang/go@596bb76 (original) (raw)

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1434,6 +1434,11 @@ casedot:
1434 1434 }
1435 1435
1436 1436 caseep:
1437 +if importpkg == nil && (c == 'p' |
1438 +// p is allowed in .a/.o imports,
1439 +// but not in .go sources. See #9036.
1440 +Yyerror("malformed floating point constant")
1441 + }
1437 1442 cp.WriteByte(byte(c))
1438 1443 c = getc()
1439 1444 if c == '+' |
@@ -1442,7 +1447,7 @@ caseep:
1442 1447 }
1443 1448
1444 1449 if !yy_isdigit(c) {
1445 -Yyerror("malformed fp constant exponent")
1450 +Yyerror("malformed floating point constant exponent")
1446 1451 }
1447 1452 for yy_isdigit(c) {
1448 1453 cp.WriteByte(byte(c))
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
1 +// errorcheck
2 +
3 +// Copyright 2015 The Go Authors. All rights reserved.
4 +// Use of this source code is governed by a BSD-style
5 +// license that can be found in the LICENSE file.
6 +
7 +// Expects to see error messages on "p" exponents.
8 +
9 +package main
10 +
11 +import "fmt"
12 +
13 +const (
14 +x1 = 1.1 // float
15 +x2 = 1e10 // float
16 +x3 = 0x1e10 // integer (e is a hex digit)
17 +x4 = 0x1p10 // ERROR "malformed floating point constant"
18 +x5 = 1p10 // ERROR "malformed floating point constant"
19 +x6 = 0p0 // ERROR "malformed floating point constant"
20 +)
21 +
22 +func main() {
23 +fmt.Printf("%g %T\n", x1, x1)
24 +fmt.Printf("%g %T\n", x2, x2)
25 +fmt.Printf("%g %T\n", x3, x3)
26 +fmt.Printf("%g %T\n", x4, x4)
27 +fmt.Printf("%g %T\n", x5, x5)
28 +fmt.Printf("%g %T\n", x6, x6)
29 +}