Skip to content

Commit 86fa255

Browse files
committed
cmd/compile/internal/types2: permit parentheses around types in interfaces
Before Go 1.18, an embedded type name in an interface could not be parenthesized. With generalized embedding of types in interfaces, where one might write ~(chan<- int) for clarity (making clear that the ~ applies to the entire channel type), it also makes sense to permit (chan<- int), or (int) for that matter. Adjust the parser accordingly to match the spec. (go/types already accepts the notation as specified by the spec.) Fixes #52391. Change-Id: Ifdd9a199c5ccc3473b2dac40dbca31d2df10d12b Reviewed-on: https://go-review.googlesource.com/c/go/+/400797 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 3ae414c commit 86fa255

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

src/cmd/compile/internal/syntax/parser.go

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,44 +1499,14 @@ func (p *parser) interfaceType() *InterfaceType {
14991499
p.want(_Interface)
15001500
p.want(_Lbrace)
15011501
p.list("interface type", _Semi, _Rbrace, func() bool {
1502-
switch p.tok {
1503-
case _Name:
1504-
f := p.methodDecl()
1505-
if f.Name == nil {
1506-
f = p.embeddedElem(f)
1507-
}
1508-
typ.MethodList = append(typ.MethodList, f)
1509-
return false
1510-
1511-
case _Lparen:
1512-
p.syntaxError("cannot parenthesize embedded type")
1513-
f := new(Field)
1514-
f.pos = p.pos()
1515-
p.next()
1516-
f.Type = p.qualifiedName(nil)
1517-
p.want(_Rparen)
1518-
typ.MethodList = append(typ.MethodList, f)
1519-
return false
1520-
1521-
case _Operator:
1522-
if p.op == Tilde {
1523-
typ.MethodList = append(typ.MethodList, p.embeddedElem(nil))
1524-
return false
1525-
}
1526-
1527-
default:
1528-
pos := p.pos()
1529-
if t := p.typeOrNil(); t != nil {
1530-
f := new(Field)
1531-
f.pos = pos
1532-
f.Type = t
1533-
typ.MethodList = append(typ.MethodList, p.embeddedElem(f))
1534-
return false
1535-
}
1502+
var f *Field
1503+
if p.tok == _Name {
1504+
f = p.methodDecl()
15361505
}
1537-
1538-
p.syntaxError("expecting method or embedded element")
1539-
p.advance(_Semi, _Rbrace)
1506+
if f == nil || f.Name == nil {
1507+
f = p.embeddedElem(f)
1508+
}
1509+
typ.MethodList = append(typ.MethodList, f)
15401510
return false
15411511
})
15421512

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
type _ interface {
8+
int
9+
(int)
10+
(*int)
11+
*([]byte)
12+
~(int)
13+
(int) | (string)
14+
(int) | ~(string)
15+
(/* ERROR unexpected ~ */ ~int)
16+
(int /* ERROR unexpected \| */ | /* ERROR unexpected string */ string /* ERROR unexpected \) */ )
17+
}

0 commit comments

Comments
 (0)