Skip to content

Commit 26ba61d

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: remove most remaining references to coreType in builtin.go
For now, use commonUnder (formerly called sharedUnder) and update error messages and comments. We can provide better error messages in individual cases eventually. Kepp using coreType for make built-in for now because it must accept different channel types with non-conflicting directions and identical element types. Added extra test cases. While at it, rename sharedUnder, sharedUnderOrChan to commonUnder and commonUnderOrChan, respectively (per suggestion from rfindley). For #70128. Change-Id: I11f3d5ce858746574f4302271d8cb763c2cdcf98 Reviewed-on: https://go-review.googlesource.com/c/go/+/653139 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 19d0b3e commit 26ba61d

File tree

11 files changed

+50
-38
lines changed

11 files changed

+50
-38
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
377377

378378
case _Copy:
379379
// copy(x, y []T) int
380-
dst, _ := coreType(x.typ).(*Slice)
380+
dst, _ := commonUnder(check, x.typ, nil).(*Slice)
381381

382382
y := args[1]
383383
src0 := coreString(y.typ)
@@ -520,7 +520,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
520520
case *Map, *Chan:
521521
min = 1
522522
case nil:
523-
check.errorf(arg0, InvalidMake, invalidArg+"cannot make %s: no core type", arg0)
523+
check.errorf(arg0, InvalidMake, invalidArg+"cannot make %s: no common underlying type", arg0)
524524
return
525525
default:
526526
check.errorf(arg0, InvalidMake, invalidArg+"cannot make %s; type must be slice, map, or channel", arg0)
@@ -818,7 +818,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
818818
// unsafe.Slice(ptr *T, len IntegerType) []T
819819
check.verifyVersionf(call.Fun, go1_17, "unsafe.Slice")
820820

821-
ptr, _ := coreType(x.typ).(*Pointer)
821+
ptr, _ := commonUnder(check, x.typ, nil).(*Pointer)
822822
if ptr == nil {
823823
check.errorf(x, InvalidUnsafeSlice, invalidArg+"%s is not a pointer", x)
824824
return
@@ -839,7 +839,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
839839
// unsafe.SliceData(slice []T) *T
840840
check.verifyVersionf(call.Fun, go1_20, "unsafe.SliceData")
841841

842-
slice, _ := coreType(x.typ).(*Slice)
842+
slice, _ := commonUnder(check, x.typ, nil).(*Slice)
843843
if slice == nil {
844844
check.errorf(x, InvalidUnsafeSliceData, invalidArg+"%s is not a slice", x)
845845
return

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
243243
cgocall := x.mode == cgofunc
244244

245245
// If the operand type is a type parameter, all types in its type set
246-
// must have a shared underlying type, which must be a signature.
246+
// must have a common underlying type, which must be a signature.
247247
var cause string
248-
sig, _ := sharedUnder(check, x.typ, &cause).(*Signature)
248+
sig, _ := commonUnder(check, x.typ, &cause).(*Signature)
249249
if sig == nil {
250250
if cause != "" {
251251
check.errorf(x, InvalidCall, invalidOp+"cannot call %s: %s", x, cause)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string, fo
6767

6868
obj, index, indirect = lookupFieldOrMethodImpl(T, addressable, pkg, name, foldCase)
6969

70-
// If we didn't find anything and if we have a type parameter with a shared underlying
70+
// If we didn't find anything and if we have a type parameter with a common underlying
7171
// type, see if there is a matching field (but not a method, those need to be declared
7272
// explicitly in the constraint). If the constraint is a named pointer type (see above),
7373
// we are ok here because only fields are accepted as results.
7474
const enableTParamFieldLookup = false // see go.dev/issue/51576
7575
if enableTParamFieldLookup && obj == nil && isTypeParam(T) {
76-
if t := sharedUnder(nil, T, nil); t != nil {
76+
if t := commonUnder(nil, T, nil); t != nil {
7777
obj, index, indirect = lookupFieldOrMethodImpl(t, addressable, pkg, name, foldCase)
7878
if _, ok := obj.(*Var); !ok {
7979
obj, index, indirect = nil, nil, false // accept fields (variables) only

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
10021002
}
10031003

10041004
var cause1 string
1005-
rtyp := sharedUnderOrChan(check, orig, &cause1)
1005+
rtyp := commonUnderOrChan(check, orig, &cause1)
10061006
if rtyp == nil {
10071007
return bad(cause1)
10081008
}
@@ -1041,7 +1041,7 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
10411041
assert(typ.Recv() == nil)
10421042
// check iterator argument type
10431043
var cause2 string
1044-
cb, _ := sharedUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
1044+
cb, _ := commonUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
10451045
switch {
10461046
case cb == nil:
10471047
if cause2 != "" {

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ func typeset(t Type, yield func(t, u Type) bool) {
4040
yield(t, under(t))
4141
}
4242

43-
// If t is not a type parameter, sharedUnder returns the underlying type.
44-
// If t is a type parameter, sharedUnder returns the single underlying
43+
// TODO(gri) commonUnder, commonUnderOrChan, and Checker.chanElem (expr.go)
44+
// have a lot of similarities. Maybe we can find common ground
45+
// between them and distill a better factorization.
46+
47+
// If t is not a type parameter, commonUnder returns the underlying type.
48+
// If t is a type parameter, commonUnder returns the common underlying
4549
// type of all types in its type set if it exists.
4650
// Otherwise the result is nil, and *cause reports the error if a non-nil
4751
// cause is provided.
4852
// The check parameter is only used if *cause reports an error; it may be nil.
49-
func sharedUnder(check *Checker, t Type, cause *string) Type {
53+
func commonUnder(check *Checker, t Type, cause *string) Type {
5054
var s, su Type
5155

5256
bad := func(s string) bool {
@@ -72,16 +76,16 @@ func sharedUnder(check *Checker, t Type, cause *string) Type {
7276
return su
7377
}
7478

75-
// If t is not a type parameter, sharedUnderOrChan returns the underlying type;
79+
// If t is not a type parameter, commonUnderOrChan returns the underlying type;
7680
// if that type is a channel type it must permit receive operations.
77-
// If t is a type parameter, sharedUnderOrChan returns the single underlying
81+
// If t is a type parameter, commonUnderOrChan returns the common underlying
7882
// type of all types in its type set if it exists, or, if the type set contains
7983
// only channel types permitting receive operations and with identical element
80-
// types, sharedUnderOrChan returns one of those channel types.
84+
// types, commonUnderOrChan returns one of those channel types.
8185
// Otherwise the result is nil, and *cause reports the error if a non-nil cause
8286
// is provided.
8387
// The check parameter is only used if *cause reports an error; it may be nil.
84-
func sharedUnderOrChan(check *Checker, t Type, cause *string) Type {
88+
func commonUnderOrChan(check *Checker, t Type, cause *string) Type {
8589
var s, su Type
8690
var sc *Chan
8791

src/go/types/builtins.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/call.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
245245
cgocall := x.mode == cgofunc
246246

247247
// If the operand type is a type parameter, all types in its type set
248-
// must have a shared underlying type, which must be a signature.
248+
// must have a common underlying type, which must be a signature.
249249
var cause string
250-
sig, _ := sharedUnder(check, x.typ, &cause).(*Signature)
250+
sig, _ := commonUnder(check, x.typ, &cause).(*Signature)
251251
if sig == nil {
252252
if cause != "" {
253253
check.errorf(x, InvalidCall, invalidOp+"cannot call %s: %s", x, cause)

src/go/types/lookup.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/stmt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
10201020
}
10211021

10221022
var cause1 string
1023-
rtyp := sharedUnderOrChan(check, orig, &cause1)
1023+
rtyp := commonUnderOrChan(check, orig, &cause1)
10241024
if rtyp == nil {
10251025
return bad(cause1)
10261026
}
@@ -1059,7 +1059,7 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
10591059
assert(typ.Recv() == nil)
10601060
// check iterator argument type
10611061
var cause2 string
1062-
cb, _ := sharedUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
1062+
cb, _ := commonUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
10631063
switch {
10641064
case cb == nil:
10651065
if cause2 != "" {

src/go/types/under.go

Lines changed: 11 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/internal/types/testdata/check/builtins1.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ func _[
152152

153153
C1 ~chan int,
154154
C2 ~chan int | ~chan string,
155-
C3 chan int | myChan, // single underlying type
155+
C3 chan int | myChan, // single underlying type
156+
C4 chan int | chan<- int, // channels may have different (non-conflicting) directions
157+
C5 <-chan int | chan<- int,
156158
]() {
157159
type S0 []int
158160
_ = make([]int, 10)
@@ -162,24 +164,26 @@ func _[
162164
_ = make /* ERROR "expects 2 or 3 arguments" */ (S1)
163165
_ = make(S1, 10, 20)
164166
_ = make /* ERROR "expects 2 or 3 arguments" */ (S1, 10, 20, 30)
165-
_ = make(S2 /* ERROR "cannot make S2: no core type" */ , 10)
167+
_ = make(S2 /* ERROR "cannot make S2: no common underlying type" */ , 10)
166168

167169
type M0 map[string]int
168170
_ = make(map[string]int)
169171
_ = make(M0)
170172
_ = make(M1)
171173
_ = make(M1, 10)
172174
_ = make/* ERROR "expects 1 or 2 arguments" */(M1, 10, 20)
173-
_ = make(M2 /* ERROR "cannot make M2: no core type" */ )
175+
_ = make(M2 /* ERROR "cannot make M2: no common underlying type" */ )
174176

175177
type C0 chan int
176178
_ = make(chan int)
177179
_ = make(C0)
178180
_ = make(C1)
179181
_ = make(C1, 10)
180182
_ = make/* ERROR "expects 1 or 2 arguments" */(C1, 10, 20)
181-
_ = make(C2 /* ERROR "cannot make C2: no core type" */ )
183+
_ = make(C2 /* ERROR "cannot make C2: no common underlying type" */ )
182184
_ = make(C3)
185+
_ = make(C4)
186+
_ = make(C5 /* ERROR "cannot make C5: no common underlying type" */ )
183187
}
184188

185189
// max

0 commit comments

Comments
 (0)