diff options
author | Matthew Dempsky <mdempsky@google.com> | 2021-04-21 02:11:15 -0700 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2021-05-02 20:38:13 +0000 |
commit | fadad851a3222867b374e901ede9c4919594837f (patch) | |
tree | 7b4efe046ea2da139b27ce5091d0cbd058bdf331 /src/cmd/compile/internal/typecheck/typecheck.go | |
parent | 0d32d9e8a8784cf3ef39c471b73e502c51085b6d (diff) | |
download | go-fadad851a3222867b374e901ede9c4919594837f.tar.gz go-fadad851a3222867b374e901ede9c4919594837f.zip |
cmd/compile: implement unsafe.Add and unsafe.Slice
Updates #19367.
Updates #40481.
Change-Id: Iabd2afdd0d520e5d68fd9e6dedd013335a4b3886
Reviewed-on: https://go-review.googlesource.com/c/go/+/312214
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/typecheck/typecheck.go')
-rw-r--r-- | src/cmd/compile/internal/typecheck/typecheck.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/typecheck/typecheck.go b/src/cmd/compile/internal/typecheck/typecheck.go index 00dd44b96b..1650144375 100644 --- a/src/cmd/compile/internal/typecheck/typecheck.go +++ b/src/cmd/compile/internal/typecheck/typecheck.go @@ -775,6 +775,14 @@ func typecheck1(n ir.Node, top int) ir.Node { n := n.(*ir.CallExpr) return tcRecover(n) + case ir.OUNSAFEADD: + n := n.(*ir.BinaryExpr) + return tcUnsafeAdd(n) + + case ir.OUNSAFESLICE: + n := n.(*ir.BinaryExpr) + return tcUnsafeSlice(n) + case ir.OCLOSURE: n := n.(*ir.ClosureExpr) tcClosure(n, top) @@ -1934,6 +1942,35 @@ func checkmake(t *types.Type, arg string, np *ir.Node) bool { return true } +// checkunsafeslice is like checkmake but for unsafe.Slice. +func checkunsafeslice(np *ir.Node) bool { + n := *np + if !n.Type().IsInteger() && n.Type().Kind() != types.TIDEAL { + base.Errorf("non-integer len argument in unsafe.Slice - %v", n.Type()) + return false + } + + // Do range checks for constants before DefaultLit + // to avoid redundant "constant NNN overflows int" errors. + if n.Op() == ir.OLITERAL { + v := toint(n.Val()) + if constant.Sign(v) < 0 { + base.Errorf("negative len argument in unsafe.Slice") + return false + } + if ir.ConstOverflow(v, types.Types[types.TINT]) { + base.Errorf("len argument too large in unsafe.Slice") + return false + } + } + + // DefaultLit is necessary for non-constants too: n might be 1.1<<k. + n = DefaultLit(n, types.Types[types.TINT]) + *np = n + + return true +} + // markBreak marks control statements containing break statements with SetHasBreak(true). func markBreak(fn *ir.Func) { var labels map[*types.Sym]ir.Node |