]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: report an error for invalid constant...
authorRobert Griesemer <gri@golang.org>
Thu, 19 Nov 2020 17:09:15 +0000 (09:09 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 19 Nov 2020 18:46:33 +0000 (18:46 +0000)
This is https://golang.org/cl/271377 ported to types2.

Updates #42695.

Change-Id: I475bdcaeace5b0e87d4476a6d660996534289666
Reviewed-on: https://go-review.googlesource.com/c/go/+/271520
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/fixedbugs/issue42695.src [new file with mode: 0644]
src/cmd/compile/internal/types2/operand.go

index 7c07950b01f13891d6863fdf3c3b7a708c02e9ff..94649ca4cceff3d7416e3c4fa38e96485989d1fb 100644 (file)
@@ -1168,7 +1168,11 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
        case *syntax.BasicLit:
                x.setConst(e.Kind, e.Value)
                if x.mode == invalid {
-                       check.invalidASTf(e, "invalid literal %v", e.Value)
+                       // The parser already establishes syntactic correctness.
+                       // If we reach here it's because of number under-/overflow.
+                       // TODO(gri) setConst (and in turn the go/constant package)
+                       // should return an error describing the issue.
+                       check.errorf(e, "malformed constant: %s", e.Value)
                        goto Error
                }
 
diff --git a/src/cmd/compile/internal/types2/fixedbugs/issue42695.src b/src/cmd/compile/internal/types2/fixedbugs/issue42695.src
new file mode 100644 (file)
index 0000000..d0d6200
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package issue42695
+
+const _ = 6e5518446744 // ERROR malformed constant
+const _ uint8 = 6e5518446744 // ERROR malformed constant
+
+var _ = 6e5518446744 // ERROR malformed constant
+var _ uint8 = 6e5518446744 // ERROR malformed constant
+
+func f(x int) int {
+        return x + 6e5518446744 // ERROR malformed constant
+}
+
+var _ = f(6e5518446744 /* ERROR malformed constant */ )
index fe88921893eb534e9951353392a3cdd7f2cffc54..0a19760423805cb8118f8757c8f8514f287f88f4 100644 (file)
@@ -211,9 +211,15 @@ func (x *operand) setConst(k syntax.LitKind, lit string) {
                unreachable()
        }
 
+       val := constant.MakeFromLiteral(lit, tok, 0)
+       if val.Kind() == constant.Unknown {
+               x.mode = invalid
+               x.typ = Typ[Invalid]
+               return
+       }
        x.mode = constant_
        x.typ = Typ[kind]
-       x.val = constant.MakeFromLiteral(lit, tok, 0)
+       x.val = val
 }
 
 // isNil reports whether x is the nil value.