Skip to content

Commit 14056d0

Browse files
committed
cmd/compile/internal/types2: add unsafe.Add and unsafe.Slice
This is a port of CL 312212, CL 312591 (except check_test.go), and CL 312790 to types2. Updates #19367. Updates #40481. Change-Id: I58ba0b0dad157baba3f82c909d5eb1268b931be4 Reviewed-on: https://go-review.googlesource.com/c/go/+/312511 Trust: Matthew Dempsky <[email protected]> Trust: Robert Griesemer <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 050b408 commit 14056d0

File tree

9 files changed

+102
-27
lines changed

9 files changed

+102
-27
lines changed

src/cmd/compile/internal/types2/builtins.go

+39
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,25 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
577577
check.recordBuiltinType(call.Fun, makeSig(x.typ))
578578
}
579579

580+
case _Add:
581+
// unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
582+
check.assignment(x, Typ[UnsafePointer], "argument to unsafe.Add")
583+
if x.mode == invalid {
584+
return
585+
}
586+
587+
var y operand
588+
arg(&y, 1)
589+
if !check.isValidIndex(&y, "length", true) {
590+
return
591+
}
592+
593+
x.mode = value
594+
x.typ = Typ[UnsafePointer]
595+
if check.Types != nil {
596+
check.recordBuiltinType(call.Fun, makeSig(x.typ, x.typ, y.typ))
597+
}
598+
580599
case _Alignof:
581600
// unsafe.Alignof(x T) uintptr
582601
if asTypeParam(x.typ) != nil {
@@ -654,6 +673,26 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
654673
x.typ = Typ[Uintptr]
655674
// result is constant - no need to record signature
656675

676+
case _Slice:
677+
// unsafe.Slice(ptr *T, len IntegerType) []T
678+
typ := asPointer(x.typ)
679+
if typ == nil {
680+
check.errorf(x, invalidArg+"%s is not a pointer", x)
681+
return
682+
}
683+
684+
var y operand
685+
arg(&y, 1)
686+
if !check.isValidIndex(&y, "length", false) {
687+
return
688+
}
689+
690+
x.mode = value
691+
x.typ = NewSlice(typ.base)
692+
if check.Types != nil {
693+
check.recordBuiltinType(call.Fun, makeSig(x.typ, typ, y.typ))
694+
}
695+
657696
case _Assert:
658697
// assert(pred) causes a typechecker error if pred is false.
659698
// The result of assert is the value of pred if there is no error.

src/cmd/compile/internal/types2/builtins_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ var builtinCalls = []struct {
8585
{"make", `var c int32; _ = make([]float64 , 0, c)`, `func([]float64, int, int32) []float64`},
8686
{"make", `var l, c uint ; _ = make([]complex128, l, c)`, `func([]complex128, uint, uint) []complex128`},
8787

88+
// issue #45667
89+
{"make", `const l uint = 1; _ = make([]int, l)`, `func([]int, uint) []int`},
90+
8891
{"new", `_ = new(int)`, `func(int) *int`},
8992
{"new", `type T struct{}; _ = new(T)`, `func(p.T) *p.T`},
9093

@@ -102,6 +105,10 @@ var builtinCalls = []struct {
102105
{"recover", `recover()`, `func() interface{}`},
103106
{"recover", `_ = recover()`, `func() interface{}`},
104107

108+
{"Add", `var p unsafe.Pointer; _ = unsafe.Add(p, -1.0)`, `func(unsafe.Pointer, int) unsafe.Pointer`},
109+
{"Add", `var p unsafe.Pointer; var n uintptr; _ = unsafe.Add(p, n)`, `func(unsafe.Pointer, uintptr) unsafe.Pointer`},
110+
{"Add", `_ = unsafe.Add(nil, 0)`, `func(unsafe.Pointer, int) unsafe.Pointer`},
111+
105112
{"Alignof", `_ = unsafe.Alignof(0)`, `invalid type`}, // constant
106113
{"Alignof", `var x struct{}; _ = unsafe.Alignof(x)`, `invalid type`}, // constant
107114

@@ -111,6 +118,9 @@ var builtinCalls = []struct {
111118
{"Sizeof", `_ = unsafe.Sizeof(0)`, `invalid type`}, // constant
112119
{"Sizeof", `var x struct{}; _ = unsafe.Sizeof(x)`, `invalid type`}, // constant
113120

121+
{"Slice", `var p *int; _ = unsafe.Slice(p, 1)`, `func(*int, int) []int`},
122+
{"Slice", `var p *byte; var n uintptr; _ = unsafe.Slice(p, n)`, `func(*byte, uintptr) []byte`},
123+
114124
{"assert", `assert(true)`, `invalid type`}, // constant
115125
{"assert", `type B bool; const pred B = 1 < 2; assert(pred)`, `invalid type`}, // constant
116126

src/cmd/compile/internal/types2/check.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,14 @@ func (check *Checker) recordTypeAndValue(x syntax.Expr, mode operandMode, typ Ty
364364
}
365365

366366
func (check *Checker) recordBuiltinType(f syntax.Expr, sig *Signature) {
367-
// f must be a (possibly parenthesized) identifier denoting a built-in
368-
// (built-ins in package unsafe always produce a constant result and
369-
// we don't record their signatures, so we don't see qualified idents
370-
// here): record the signature for f and possible children.
367+
// f must be a (possibly parenthesized, possibly qualified)
368+
// identifier denoting a built-in (including unsafe's non-constant
369+
// functions Add and Slice): record the signature for f and possible
370+
// children.
371371
for {
372372
check.recordTypeAndValue(f, builtin, sig, nil)
373373
switch p := f.(type) {
374-
case *syntax.Name:
374+
case *syntax.Name, *syntax.SelectorExpr:
375375
return // we're done
376376
case *syntax.ParenExpr:
377377
f = p.X

src/cmd/compile/internal/types2/check_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func TestCheck(t *testing.T) {
250250
checkFiles(t, strings.Split(*testFiles, ","), *goVersion, 0, testing.Verbose())
251251
}
252252

253-
// TODO(gri) go/types has an extra TestLongConstants test
253+
// TODO(gri) go/types has extra TestLongConstants and TestIndexRepresentability tests
254254

255255
func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, "testdata", 75) } // TODO(gri) narrow column tolerance
256256
func TestExamples(t *testing.T) { testDir(t, "examples", 0) }

src/cmd/compile/internal/types2/index.go

+39-18
Original file line numberDiff line numberDiff line change
@@ -320,34 +320,21 @@ func (check *Checker) index(index syntax.Expr, max int64) (typ Type, val int64)
320320

321321
var x operand
322322
check.expr(&x, index)
323-
if x.mode == invalid {
324-
return
325-
}
326-
327-
// an untyped constant must be representable as Int
328-
check.convertUntyped(&x, Typ[Int])
329-
if x.mode == invalid {
330-
return
331-
}
332-
333-
// the index must be of integer type
334-
if !isInteger(x.typ) {
335-
check.errorf(&x, invalidArg+"index %s must be integer", &x)
323+
if !check.isValidIndex(&x, "index", false) {
336324
return
337325
}
338326

339327
if x.mode != constant_ {
340328
return x.typ, -1
341329
}
342330

343-
// a constant index i must be in bounds
344-
if constant.Sign(x.val) < 0 {
345-
check.errorf(&x, invalidArg+"index %s must not be negative", &x)
331+
if x.val.Kind() == constant.Unknown {
346332
return
347333
}
348334

349-
v, valid := constant.Int64Val(constant.ToInt(x.val))
350-
if !valid || max >= 0 && v >= max {
335+
v, ok := constant.Int64Val(x.val)
336+
assert(ok)
337+
if max >= 0 && v >= max {
351338
if check.conf.CompilerErrorMessages {
352339
check.errorf(&x, invalidArg+"array index %s out of bounds [0:%d]", x.val.String(), max)
353340
} else {
@@ -360,6 +347,40 @@ func (check *Checker) index(index syntax.Expr, max int64) (typ Type, val int64)
360347
return x.typ, v
361348
}
362349

350+
func (check *Checker) isValidIndex(x *operand, what string, allowNegative bool) bool {
351+
if x.mode == invalid {
352+
return false
353+
}
354+
355+
// spec: "a constant index that is untyped is given type int"
356+
check.convertUntyped(x, Typ[Int])
357+
if x.mode == invalid {
358+
return false
359+
}
360+
361+
// spec: "the index x must be of integer type or an untyped constant"
362+
if !isInteger(x.typ) {
363+
check.errorf(x, invalidArg+"%s %s must be integer", what, x)
364+
return false
365+
}
366+
367+
if x.mode == constant_ {
368+
// spec: "a constant index must be non-negative ..."
369+
if !allowNegative && constant.Sign(x.val) < 0 {
370+
check.errorf(x, invalidArg+"%s %s must not be negative", what, x)
371+
return false
372+
}
373+
374+
// spec: "... and representable by a value of type int"
375+
if !representableConst(x.val, check, Typ[Int], &x.val) {
376+
check.errorf(x, invalidArg+"%s %s overflows int", what, x)
377+
return false
378+
}
379+
}
380+
381+
return true
382+
}
383+
363384
// indexElts checks the elements (elts) of an array or slice composite literal
364385
// against the literal's element type (typ), and the element indices against
365386
// the literal length if known (length >= 0). It returns the length of the

src/cmd/compile/internal/types2/testdata/expr3.src

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func indexes() {
3535
_ = a[9]
3636
_ = a[10 /* ERROR "index .* out of bounds" */ ]
3737
_ = a[1 /* ERROR "overflows" */ <<100]
38+
_ = a[1<< /* ERROR "constant shift overflow" */ 1000] // no out-of-bounds follow-on error
3839
_ = a[10:]
3940
_ = a[:10]
4041
_ = a[10:10]

src/cmd/compile/internal/types2/universe.go

+4
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ const (
136136
_Recover
137137

138138
// package unsafe
139+
_Add
139140
_Alignof
140141
_Offsetof
141142
_Sizeof
143+
_Slice
142144

143145
// testing support
144146
_Assert
@@ -167,9 +169,11 @@ var predeclaredFuncs = [...]struct {
167169
_Real: {"real", 1, false, expression},
168170
_Recover: {"recover", 0, false, statement},
169171

172+
_Add: {"Add", 2, false, expression},
170173
_Alignof: {"Alignof", 1, false, expression},
171174
_Offsetof: {"Offsetof", 1, false, expression},
172175
_Sizeof: {"Sizeof", 1, false, expression},
176+
_Slice: {"Slice", 2, false, expression},
173177

174178
_Assert: {"assert", 1, false, statement},
175179
_Trace: {"trace", 0, true, statement},

test/makechan.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var sink T
1616

1717
func main() {
1818
sink = make(T, -1) // ERROR "negative buffer argument in make.*|must not be negative"
19-
sink = make(T, uint64(1<<63)) // ERROR "buffer argument too large in make.*|out of bounds"
19+
sink = make(T, uint64(1<<63)) // ERROR "buffer argument too large in make.*|overflows int"
2020

2121
sink = make(T, 0.5) // ERROR "constant 0.5 truncated to integer|truncated to int"
2222
sink = make(T, 1.0)

test/makemap.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ var sink T
1616

1717
func main() {
1818
sink = make(T, -1) // ERROR "negative size argument in make.*|must not be negative"
19-
sink = make(T, uint64(1<<63)) // ERROR "size argument too large in make.*|out of bounds"
19+
sink = make(T, uint64(1<<63)) // ERROR "size argument too large in make.*|overflows int"
2020

2121
// Test that errors are emitted at call sites, not const declarations
2222
const x = -1
2323
sink = make(T, x) // ERROR "negative size argument in make.*|must not be negative"
2424
const y = uint64(1 << 63)
25-
sink = make(T, y) // ERROR "size argument too large in make.*|out of bounds"
25+
sink = make(T, y) // ERROR "size argument too large in make.*|overflows int"
2626

2727
sink = make(T, 0.5) // ERROR "constant 0.5 truncated to integer|truncated to int"
2828
sink = make(T, 1.0)

0 commit comments

Comments
 (0)