]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax, types2: remove ability to handle type lists
authorRobert Griesemer <gri@golang.org>
Tue, 5 Oct 2021 21:03:47 +0000 (14:03 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 6 Oct 2021 19:44:05 +0000 (19:44 +0000)
The type set notation has been accepted a while ago.
We're not going back to supporting the original
type list notation. Remove support for it in the
parser and type checker.

Change-Id: I860651f80b89fa43a3a5a2a02cf823ec0dae583c
Reviewed-on: https://go-review.googlesource.com/c/go/+/354131
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
13 files changed:
src/cmd/compile/internal/syntax/error_test.go
src/cmd/compile/internal/syntax/nodes.go
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/parser_test.go
src/cmd/compile/internal/syntax/printer.go
src/cmd/compile/internal/syntax/printer_test.go
src/cmd/compile/internal/syntax/syntax.go
src/cmd/compile/internal/syntax/testdata/go2/typeinst2.go2
src/cmd/compile/internal/syntax/testdata/interface.go2
src/cmd/compile/internal/types2/check_test.go
src/cmd/compile/internal/types2/interface.go
src/cmd/compile/internal/types2/testdata/check/typeinst2.go2
src/cmd/compile/internal/types2/testdata/examples/constraints.go2

index 966b36f6bcf1fe244c7d4f145bd113922c7625d3..0ab3c20ce53a12dcd39b89433a2baee66972b96b 100644 (file)
@@ -130,7 +130,7 @@ func testSyntaxErrors(t *testing.T, filename string) {
 
        var mode Mode
        if strings.HasSuffix(filename, ".go2") {
-               mode = AllowGenerics | AllowTypeSets | AllowTypeLists
+               mode = AllowGenerics | AllowTypeSets
        }
        ParseFile(filename, func(err error) {
                e, ok := err.(Error)
index fb9786daa325c580d5402603e9241ae67ab36ddc..2f9b43edef98ad42edd4ad01e676fff5d83c26e0 100644 (file)
@@ -275,14 +275,14 @@ type (
        // Name Type
        //      Type
        Field struct {
-               Name *Name // nil means anonymous field/parameter (structs/parameters), or embedded interface (interfaces)
+               Name *Name // nil means anonymous field/parameter (structs/parameters), or embedded element (interfaces)
                Type Expr  // field names declared in a list share the same Type (identical pointers)
                node
        }
 
        // interface { MethodList[0]; MethodList[1]; ... }
        InterfaceType struct {
-               MethodList []*Field // a field named "type" means a type constraint
+               MethodList []*Field
                expr
        }
 
index 54e77b9958f77839d19c9bdc45f42e95519f524a..a669c5471267d2bb113f709302aacb7a0988f42f 100644 (file)
@@ -1450,30 +1450,6 @@ func (p *parser) interfaceType() *InterfaceType {
                                return false
                        }
 
-               case _Type:
-                       // TODO(gri) remove TypeList syntax if we accept #45346
-                       if p.allowGenerics() && p.mode&AllowTypeLists != 0 {
-                               type_ := NewName(p.pos(), "type") // cannot have a method named "type"
-                               p.next()
-                               if p.tok != _Semi && p.tok != _Rbrace {
-                                       f := new(Field)
-                                       f.pos = p.pos()
-                                       f.Name = type_
-                                       f.Type = p.type_()
-                                       typ.MethodList = append(typ.MethodList, f)
-                                       for p.got(_Comma) {
-                                               f := new(Field)
-                                               f.pos = p.pos()
-                                               f.Name = type_
-                                               f.Type = p.type_()
-                                               typ.MethodList = append(typ.MethodList, f)
-                                       }
-                               } else {
-                                       p.syntaxError("expecting type")
-                               }
-                               return false
-                       }
-
                default:
                        if p.allowGenerics() {
                                pos := p.pos()
@@ -1488,13 +1464,8 @@ func (p *parser) interfaceType() *InterfaceType {
                }
 
                if p.allowGenerics() {
-                       if p.mode&AllowTypeLists != 0 {
-                               p.syntaxError("expecting method, type list, or embedded element")
-                               p.advance(_Semi, _Rbrace, _Type)
-                       } else {
-                               p.syntaxError("expecting method or embedded element")
-                               p.advance(_Semi, _Rbrace)
-                       }
+                       p.syntaxError("expecting method or embedded element")
+                       p.advance(_Semi, _Rbrace)
                        return false
                }
 
index fb02c8b95feffe284adb6ce602f0c07aeaa901b6..29682012e5d263df23acac63b9db9c1d15738e9a 100644 (file)
@@ -46,7 +46,7 @@ func TestParseGo2(t *testing.T) {
        for _, fi := range list {
                name := fi.Name()
                if !fi.IsDir() && !strings.HasPrefix(name, ".") {
-                       ParseFile(filepath.Join(dir, name), func(err error) { t.Error(err) }, nil, AllowGenerics|AllowTypeSets|AllowTypeLists)
+                       ParseFile(filepath.Join(dir, name), func(err error) { t.Error(err) }, nil, AllowGenerics|AllowTypeSets)
                }
        }
 }
index e557f5d9247b521a67984f5b60d8963174b45a1d..c8d31799afcb4e479f31e6a07b004b8de028d5ae 100644 (file)
@@ -494,39 +494,16 @@ func (p *printer) printRawNode(n Node) {
                p.printSignature(n)
 
        case *InterfaceType:
-               // separate type list and method list
-               var types []Expr
-               var methods []*Field
-               for _, f := range n.MethodList {
-                       if f.Name != nil && f.Name.Value == "type" {
-                               types = append(types, f.Type)
-                       } else {
-                               // method or embedded interface
-                               methods = append(methods, f)
-                       }
-               }
-
-               multiLine := len(n.MethodList) > 0 && p.linebreaks
                p.print(_Interface)
-               if multiLine {
+               if p.linebreaks && len(n.MethodList) > 1 {
                        p.print(blank)
-               }
-               p.print(_Lbrace)
-               if multiLine {
+                       p.print(_Lbrace)
                        p.print(newline, indent)
-               }
-               if len(types) > 0 {
-                       p.print(_Type, blank)
-                       p.printExprList(types)
-                       if len(methods) > 0 {
-                               p.print(_Semi, blank)
-                       }
-               }
-               if len(methods) > 0 {
-                       p.printMethodList(methods)
-               }
-               if multiLine {
+                       p.printMethodList(n.MethodList)
                        p.print(outdent, newline)
+               } else {
+                       p.print(_Lbrace)
+                       p.printMethodList(n.MethodList)
                }
                p.print(_Rbrace)
 
index ee083ad1593a267272f78539e5a97976db692fca..9b5331b148813a734305420c3fe5cc7f1ace875a 100644 (file)
@@ -60,12 +60,12 @@ var stringTests = []string{
        // generic type declarations
        "package p; type _[T any] struct{}",
        "package p; type _[A, B, C interface{m()}] struct{}",
-       "package p; type _[T any, A, B, C interface{m()}, X, Y, Z interface{type int}] struct{}",
+       "package p; type _[T any, A, B, C interface{m()}, X, Y, Z interface{~int}] struct{}",
 
        // generic function declarations
        "package p; func _[T any]()",
        "package p; func _[A, B, C interface{m()}]()",
-       "package p; func _[T any, A, B, C interface{m()}, X, Y, Z interface{type int}]()",
+       "package p; func _[T any, A, B, C interface{m()}, X, Y, Z interface{~int}]()",
 
        // methods with generic receiver types
        "package p; func (R[T]) _()",
@@ -94,7 +94,7 @@ var stringTests = []string{
 
 func TestPrintString(t *testing.T) {
        for _, want := range stringTests {
-               ast, err := Parse(nil, strings.NewReader(want), nil, nil, AllowGenerics|AllowTypeSets|AllowTypeLists)
+               ast, err := Parse(nil, strings.NewReader(want), nil, nil, AllowGenerics|AllowTypeSets)
                if err != nil {
                        t.Error(err)
                        continue
@@ -140,10 +140,10 @@ var exprTests = [][2]string{
        dup("func(int, float32) string"),
        dup("interface{m()}"),
        dup("interface{m() string; n(x int)}"),
-       dup("interface{type int}"),
-       dup("interface{type int, float64, string}"),
-       dup("interface{type int; m()}"),
-       dup("interface{type int, float64, string; m() string; n(x int)}"),
+       dup("interface{~int}"),
+       dup("interface{~int | ~float64 | ~string}"),
+       dup("interface{~int; m()}"),
+       dup("interface{~int | ~float64 | ~string; m() string; n(x int)}"),
        dup("map[string]int"),
        dup("chan E"),
        dup("<-chan E"),
@@ -155,7 +155,7 @@ var exprTests = [][2]string{
        dup("interface{~int}"),
        dup("interface{int | string}"),
        dup("interface{~int | ~string; float64; m()}"),
-       dup("interface{type a, b, c; ~int | ~string; float64; m()}"),
+       dup("interface{~a | ~b | ~c; ~int | ~string; float64; m()}"),
        dup("interface{~T[int, string] | string}"),
 
        // non-type expressions
@@ -214,7 +214,7 @@ var exprTests = [][2]string{
 func TestShortString(t *testing.T) {
        for _, test := range exprTests {
                src := "package p; var _ = " + test[0]
-               ast, err := Parse(nil, strings.NewReader(src), nil, nil, AllowGenerics|AllowTypeLists)
+               ast, err := Parse(nil, strings.NewReader(src), nil, nil, AllowGenerics)
                if err != nil {
                        t.Errorf("%s: %s", test[0], err)
                        continue
index 8828c39ad55d01fea9e8d8374c5be1eeeef4f9e9..49ba87786ee113c995baa8b0ad3a02ce956f148b 100644 (file)
@@ -17,8 +17,7 @@ type Mode uint
 const (
        CheckBranches Mode = 1 << iota // check correct use of labels, break, continue, and goto statements
        AllowGenerics
-       AllowTypeSets  // requires AllowGenerics; remove once #48424 is decided
-       AllowTypeLists // requires AllowGenerics; remove once 1.18 is out
+       AllowTypeSets // requires AllowGenerics; remove once #48424 is decided
 )
 
 // Error describes a syntax error. Error implements the error interface.
index f3deb703b6770077251c213c517e252c84b98161..76b8d5591fbf99cfa49264e51f13c67e6d154046 100644 (file)
@@ -148,31 +148,7 @@ func _[T any](r R2[T, int], p *R2[string, T]) {
        p.pm()
 }
 
-// An interface can (explicitly) declare at most one type list.
-type _ interface {
-       m0()
-       type int, string, bool
-       type /* ERROR multiple type lists */ float32, float64
-       m1()
-       m2()
-       type /* ERROR multiple type lists */ complex64, complex128
-       type /* ERROR multiple type lists */ rune
-}
-
-// Interface type lists may contain each type at most once.
-// (If there are multiple lists, we assume the author intended
-// for them to be all in a single list, and we report the error
-// as well.)
-type _ interface {
-       type int, int /* ERROR duplicate type int */
-       type /* ERROR multiple type lists */ int /* ERROR duplicate type int */
-}
-
-type _ interface {
-       type struct{f int}, struct{g int}, struct /* ERROR duplicate type */ {f int}
-}
-
-// Interface type lists can contain any type, incl. *Named types.
+// Interface type constraints can contain any type, incl. *Named types.
 // Verify that we use the underlying type to compute the operational type.
 type MyInt int
 func add1[T interface{ ~MyInt }](x T) T {
@@ -184,9 +160,9 @@ func double[T interface{ ~MyInt | ~MyString }](x T) T {
        return x + x
 }
 
-// Embedding of interfaces with type lists leads to interfaces
-// with type lists that are the intersection of the embedded
-// type lists.
+// Embedding of interfaces with type constraints leads to interfaces
+// with type constraints that are the intersection of the embedded
+// type constraints.
 
 type E0 interface {
        ~int | ~bool | ~string
@@ -246,7 +222,7 @@ var _ = f12[float64]
 
 type I0_ interface {
        E0
-       type int
+       ~int
 }
 
 func f0_[T I0_]()
index b399d7514889f39c5e8889088dd2c4ada0d2c857..dbc41879896920495d064f92cb1c16cc0c8d71fb 100644 (file)
@@ -4,16 +4,11 @@
 
 // This file contains test cases for interfaces containing
 // constraint elements.
-//
-// For now, we accept both ordinary type lists and the
-// more complex constraint elements.
 
 package p
 
 type _ interface {
        m()
-       type int
-       type int, string
        E
 }
 
@@ -31,7 +26,6 @@ type _ interface {
        T[int, string] | string
        int | ~T[string, struct{}]
        ~int | ~string
-       type bool, int, float64
 }
 
 type _ interface {
index 1ca2eea5c6ac6579e5e20fed8b938ee579163e28..5b2f09425bb55a9efbfec8206f3322850e7b26fd 100644 (file)
@@ -100,7 +100,7 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
 
        var mode syntax.Mode
        if strings.HasSuffix(filenames[0], ".go2") {
-               mode |= syntax.AllowGenerics | syntax.AllowTypeSets | syntax.AllowTypeLists
+               mode |= syntax.AllowGenerics | syntax.AllowTypeSets
        }
        // parse files and collect parser errors
        files, errlist := parseFiles(t, filenames, mode)
index 431ba93c17cf8c14ea6200a10c89f64053b07206..0879d29d3d79df295f858c74902792355b2aa72e 100644 (file)
@@ -93,9 +93,6 @@ func (t *Interface) String() string   { return TypeString(t, nil) }
 // Implementation
 
 func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType, def *Named) {
-       var tlist []syntax.Expr // types collected from all type lists
-       var tname *syntax.Name  // most recent "type" name
-
        addEmbedded := func(pos syntax.Pos, typ Type) {
                ityp.embeddeds = append(ityp.embeddeds, typ)
                if ityp.embedPos == nil {
@@ -122,31 +119,6 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
                        continue // ignore
                }
 
-               // TODO(gri) Remove type list handling once the parser doesn't accept type lists anymore.
-               if name == "type" {
-                       // Report an error for the first type list per interface
-                       // if we don't allow type lists, but continue.
-                       if !check.conf.AllowTypeLists && tlist == nil {
-                               check.softErrorf(f.Name, "use generalized embedding syntax instead of a type list")
-                       }
-                       // For now, collect all type list entries as if it
-                       // were a single union, where each union element is
-                       // of the form ~T.
-                       op := new(syntax.Operation)
-                       // We should also set the position (but there is no setter);
-                       // we don't care because this code will eventually go away.
-                       op.Op = syntax.Tilde
-                       op.X = f.Type
-                       tlist = append(tlist, op)
-                       // Report an error if we have multiple type lists in an
-                       // interface, but only if they are permitted in the first place.
-                       if check.conf.AllowTypeLists && tname != nil && tname != f.Name {
-                               check.error(f.Name, "cannot have multiple type lists in an interface")
-                       }
-                       tname = f.Name
-                       continue
-               }
-
                typ := check.typ(f.Type)
                sig, _ := typ.(*Signature)
                if sig == nil {
@@ -175,13 +147,6 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
                ityp.methods = append(ityp.methods, m)
        }
 
-       // If we saw a type list, add it like an embedded union.
-       if tlist != nil {
-               // Types T in a type list are added as ~T expressions but we don't
-               // have the position of the '~'. Use the first type position instead.
-               addEmbedded(tlist[0].(*syntax.Operation).X.Pos(), parseUnion(check, tlist))
-       }
-
        // All methods and embedded elements for this interface are collected;
        // i.e., this interface may be used in a type set computation.
        ityp.complete = true
index d087c26a47e637836a8cc56308dfb53e7314bf1f..49f48c7283c6f949363c9e1b5a854e2311478aa9 100644 (file)
@@ -159,10 +159,7 @@ type _ interface {
        ~rune
 }
 
-// Interface type lists may contain each type at most once.
-// (If there are multiple lists, we assume the author intended
-// for them to be all in a single list, and we report the error
-// as well.)
+// Type sets may contain each type at most once.
 type _ interface {
        ~int|~int /* ERROR overlapping terms ~int */
        ~int|int /* ERROR overlapping terms int */
index ecc75c1a46f98b7fa7e57fe0d1d8e2517ff9660c..4d7f70313a20ae48cf0afea39d9efe88c4f1f4dc 100644 (file)
@@ -6,18 +6,6 @@
 
 package p
 
-type (
-       // Type lists are processed as unions but an error is reported.
-       // TODO(gri) remove this once the parser doesn't accept type lists anymore.
-       _ interface{
-               type /* ERROR use generalized embedding syntax instead of a type list */ int
-       }
-       _ interface{
-               type /* ERROR use generalized embedding syntax instead of a type list */ int
-               type float32
-       }
-)
-
 type MyInt int
 
 type (