[go/dev.regabi] [dev.regabi] go/types: type alias decl requires go1.9

26 views
Skip to first unread message

Robert Findley (Gerrit)

unread,
Feb 11, 2021, 10:54:27 AM2/11/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Robert Findley has uploaded this change for review.

View Change

[dev.regabi] go/types: type alias decl requires go1.9

This is a port of CL 289570 to go/types. It has some notable differences
with that CL:
+ A new _BadDecl error code is added, to indicate declarations with bad
syntax.
+ declInfo is updated hold not an 'alias' bool, but an aliasPos
token.Pos to identify the location of the type aliasing '=' token.
This allows for error messages to be accurately placed on the '='

For #31793

Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
---
M src/go/types/decl.go
M src/go/types/errorcodes.go
M src/go/types/resolver.go
A src/go/types/testdata/go1_8.src
4 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/go/types/decl.go b/src/go/types/decl.go
index 571e172..874ed19 100644
--- a/src/go/types/decl.go
+++ b/src/go/types/decl.go
@@ -189,7 +189,7 @@
check.varDecl(obj, d.lhs, d.typ, d.init)
case *TypeName:
// invalid recursive types are detected via path
- check.typeDecl(obj, d.typ, def, d.alias)
+ check.typeDecl(obj, d.typ, def, d.aliasPos)
case *Func:
// functions may be recursive - no need to track dependencies
check.funcDecl(obj, d)
@@ -234,7 +234,7 @@
// this information explicitly in the object.
var alias bool
if d := check.objMap[obj]; d != nil {
- alias = d.alias // package-level object
+ alias = d.aliasPos.IsValid() // package-level object
} else {
alias = obj.IsAlias() // function local object
}
@@ -640,14 +640,17 @@
}
}

-func (check *Checker) typeDecl(obj *TypeName, typ ast.Expr, def *Named, alias bool) {
+func (check *Checker) typeDecl(obj *TypeName, typ ast.Expr, def *Named, aliasPos token.Pos) {
assert(obj.typ == nil)

check.later(func() {
check.validType(obj.typ, nil)
})

- if alias {
+ if aliasPos.IsValid() {
+ if !check.allowVersion(obj.pkg, 1, 9) {
+ check.errorf(atPos(aliasPos), _BadDecl, "type aliases requires go1.9 or later")
+ }

obj.typ = Typ[Invalid]
obj.typ = check.typ(typ)
@@ -691,7 +694,7 @@
return
}
delete(check.methods, obj)
- assert(!check.objMap[obj].alias) // don't use TypeName.IsAlias (requires fully set up object)
+ assert(!check.objMap[obj].aliasPos.IsValid()) // don't use TypeName.IsAlias (requires fully set up object)

// use an objset to check for name conflicts
var mset objset
@@ -864,7 +867,7 @@
check.declare(check.scope, d.spec.Name, obj, scopePos)
// mark and unmark type before calling typeDecl; its type is still nil (see Checker.objDecl)
obj.setColor(grey + color(check.push(obj)))
- check.typeDecl(obj, d.spec.Type, nil, d.spec.Assign.IsValid())
+ check.typeDecl(obj, d.spec.Type, nil, d.spec.Assign)
check.pop().setColor(black)
default:
check.invalidAST(d.node(), "unknown ast.Decl node %T", d.node())
diff --git a/src/go/types/errorcodes.go b/src/go/types/errorcodes.go
index d27abdf..ac28c3b 100644
--- a/src/go/types/errorcodes.go
+++ b/src/go/types/errorcodes.go
@@ -1366,4 +1366,7 @@
// return i
// }
_InvalidGo
+
+ // _BadDecl occurs when a declaration has invalid syntax.
+ _BadDecl
)
diff --git a/src/go/types/resolver.go b/src/go/types/resolver.go
index 47e165d..1fda143 100644
--- a/src/go/types/resolver.go
+++ b/src/go/types/resolver.go
@@ -23,7 +23,7 @@
init ast.Expr // init/orig expression, or nil
inherited bool // if set, the init expression is inherited from a previous constant declaration
fdecl *ast.FuncDecl // func declaration, or nil
- alias bool // type alias declaration
+ aliasPos token.Pos // if valid, the position of '=' in a type alias

// The deps field tracks initialization expression dependencies.
deps map[Object]bool // lazily initialized
@@ -366,7 +366,7 @@
}
case typeDecl:
obj := NewTypeName(d.spec.Name.Pos(), pkg, d.spec.Name.Name, nil)
- check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, typ: d.spec.Type, alias: d.spec.Assign.IsValid()})
+ check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, typ: d.spec.Type, aliasPos: d.spec.Assign})
case funcDecl:
info := &declInfo{file: fileScope, fdecl: d.decl}
name := d.decl.Name.Name
@@ -493,7 +493,7 @@
// we're done if tdecl defined tname as a new type
// (rather than an alias)
tdecl := check.objMap[tname] // must exist for objects in package scope
- if !tdecl.alias {
+ if !tdecl.aliasPos.IsValid() {
return ptr, tname
}

@@ -534,7 +534,7 @@
// phase 1
for _, obj := range objList {
// If we have a type alias, collect it for the 2nd phase.
- if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].alias {
+ if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].aliasPos.IsValid() {
aliasList = append(aliasList, tname)
continue
}
diff --git a/src/go/types/testdata/go1_8.src b/src/go/types/testdata/go1_8.src
new file mode 100644
index 0000000..3ead1e9
--- /dev/null
+++ b/src/go/types/testdata/go1_8.src
@@ -0,0 +1,11 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Check Go language version-specific errors.
+
+package go1_8 // go1.8
+
+// type alias declarations
+type any = /* ERROR type aliases requires go1.9 or later */ interface{}
+

To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: dev.regabi
Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
Gerrit-Change-Number: 291318
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@google.com>
Gerrit-MessageType: newchange

Robert Findley (Gerrit)

unread,
Feb 11, 2021, 10:56:39 AM2/11/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Patch set 1:Run-TryBot +1Trust +1

View Change

    To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: dev.regabi
    Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
    Gerrit-Change-Number: 291318
    Gerrit-PatchSet: 1
    Gerrit-Owner: Robert Findley <rfin...@google.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-Comment-Date: Thu, 11 Feb 2021 15:56:34 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Robert Findley (Gerrit)

    unread,
    Feb 11, 2021, 11:05:15 AM2/11/21
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Robert Findley uploaded patch set #2 to this change.

    View Change

    [dev.regabi] go/types: type alias decl requires go1.9

    This is a port of CL 289570 to go/types. It has some notable differences
    with that CL:
    + A new _BadDecl error code is added, to indicate declarations with bad
    syntax.
    + declInfo is updated hold not an 'alias' bool, but an aliasPos
    token.Pos to identify the location of the type aliasing '=' token.
    This allows for error messages to be accurately placed on the '='

    For #31793

    Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
    ---
    M src/go/types/decl.go
    M src/go/types/errorcodes.go
    M src/go/types/resolver.go
    A src/go/types/testdata/go1_8.src
    4 files changed, 27 insertions(+), 10 deletions(-)

    To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: dev.regabi
    Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
    Gerrit-Change-Number: 291318
    Gerrit-PatchSet: 2
    Gerrit-Owner: Robert Findley <rfin...@google.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-CC: Go Bot <go...@golang.org>
    Gerrit-MessageType: newpatchset

    Robert Findley (Gerrit)

    unread,
    Feb 11, 2021, 11:14:29 AM2/11/21
    to goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

    Attention is currently required from: Robert Griesemer.

    Patch set 3:Run-TryBot +1Trust +1

    View Change

      To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: dev.regabi
      Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
      Gerrit-Change-Number: 291318
      Gerrit-PatchSet: 3
      Gerrit-Owner: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
      Gerrit-CC: Go Bot <go...@golang.org>
      Gerrit-Attention: Robert Griesemer <g...@golang.org>
      Gerrit-Comment-Date: Thu, 11 Feb 2021 16:14:22 +0000

      Robert Griesemer (Gerrit)

      unread,
      Feb 11, 2021, 6:49:08 PM2/11/21
      to Robert Findley, goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

      Attention is currently required from: Robert Findley.

      Patch set 3:Code-Review +2Trust +1

      View Change

      2 comments:

      • File src/go/types/decl.go:

        • Patch Set #3, Line 684: addMethodDecls

          FWIW, this call is happening at the call of typeDecl in types2 (because it's only needed for top-level decls).

          Should probably adjust in a separate CL.

        • Patch Set #3, Line 687: addMethodDecls

          this is called collectMethods in types2

      To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: dev.regabi
      Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
      Gerrit-Change-Number: 291318
      Gerrit-PatchSet: 3
      Gerrit-Owner: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Go Bot <go...@golang.org>
      Gerrit-Reviewer: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
      Gerrit-Attention: Robert Findley <rfin...@google.com>
      Gerrit-Comment-Date: Thu, 11 Feb 2021 23:49:04 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Gerrit-MessageType: comment

      Robert Findley (Gerrit)

      unread,
      Feb 12, 2021, 10:45:23 AM2/12/21
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Attention is currently required from: Robert Findley.

      Robert Findley uploaded patch set #5 to this change.

      View Change

      [dev.regabi] go/types: type alias decl requires go1.9

      This is a port of CL 289570 to go/types. It has some notable differences
      with that CL:
      + A new _BadDecl error code is added, to indicate declarations with bad
      syntax.
      + declInfo is updated hold not an 'alias' bool, but an aliasPos
      token.Pos to identify the location of the type aliasing '=' token.
      This allows for error messages to be accurately placed on the '='

      For #31793

      Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
      ---
      M src/go/types/decl.go
      M src/go/types/errorcodes.go
      M src/go/types/resolver.go
      A src/go/types/testdata/go1_8.src
      4 files changed, 30 insertions(+), 10 deletions(-)

      To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: dev.regabi
      Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
      Gerrit-Change-Number: 291318
      Gerrit-PatchSet: 5
      Gerrit-Owner: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Go Bot <go...@golang.org>
      Gerrit-Reviewer: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
      Gerrit-Attention: Robert Findley <rfin...@google.com>
      Gerrit-MessageType: newpatchset

      Robert Findley (Gerrit)

      unread,
      Feb 12, 2021, 10:46:11 AM2/12/21
      to goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

      Patch set 5:Run-TryBot +1Trust +1

      View Change

      2 comments:

      • File src/go/types/decl.go:

        • FWIW, this call is happening at the call of typeDecl in types2 (because it's only needed for top-lev […]

          Added a TODO. Will do in a follow-up.

        • Added a TODO.

      To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: dev.regabi
      Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
      Gerrit-Change-Number: 291318
      Gerrit-PatchSet: 5
      Gerrit-Owner: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Go Bot <go...@golang.org>
      Gerrit-Reviewer: Robert Findley <rfin...@google.com>
      Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
      Gerrit-Comment-Date: Fri, 12 Feb 2021 15:46:05 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Comment-In-Reply-To: Robert Griesemer <g...@golang.org>
      Gerrit-MessageType: comment

      Robert Findley (Gerrit)

      unread,
      Feb 12, 2021, 11:12:30 AM2/12/21
      to goph...@pubsubhelper.golang.org, Robert Griesemer, Go Bot, golang-co...@googlegroups.com

      Patch set 6:Run-TryBot +1Trust +1

      View Change

        To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: dev.regabi
        Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
        Gerrit-Change-Number: 291318
        Gerrit-PatchSet: 6
        Gerrit-Owner: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: Go Bot <go...@golang.org>
        Gerrit-Reviewer: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
        Gerrit-Comment-Date: Fri, 12 Feb 2021 16:12:25 +0000

        Robert Findley (Gerrit)

        unread,
        Feb 12, 2021, 7:41:28 PM2/12/21
        to goph...@pubsubhelper.golang.org, Go Bot, Robert Griesemer, golang-co...@googlegroups.com

        Attention is currently required from: Robert Findley.

        Robert Findley removed a vote from this change.

        View Change

        Removed TryBot-Result-1 by Go Bot <go...@golang.org>

        To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: dev.regabi
        Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
        Gerrit-Change-Number: 291318
        Gerrit-PatchSet: 6
        Gerrit-Owner: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: Go Bot <go...@golang.org>
        Gerrit-Reviewer: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
        Gerrit-Attention: Robert Findley <rfin...@google.com>
        Gerrit-MessageType: deleteVote

        Robert Findley (Gerrit)

        unread,
        Feb 16, 2021, 4:01:20 PM2/16/21
        to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go Bot, Robert Griesemer, golang-co...@googlegroups.com

        Robert Findley submitted this change.

        View Change

        Approvals: Robert Griesemer: Looks good to me, approved; Trusted Robert Findley: Trusted; Run TryBots Go Bot: TryBots succeeded
        [dev.regabi] go/types: type alias decl requires go1.9

        This is a port of CL 289570 to go/types. It has some notable differences
        with that CL:
        + A new _BadDecl error code is added, to indicate declarations with bad
        syntax.
        + declInfo is updated hold not an 'alias' bool, but an aliasPos
        token.Pos to identify the location of the type aliasing '=' token.
        This allows for error messages to be accurately placed on the '='

        For #31793

        Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
        Reviewed-on: https://go-review.googlesource.com/c/go/+/291318
        Trust: Robert Findley <rfin...@google.com>
        Trust: Robert Griesemer <g...@golang.org>
        Run-TryBot: Robert Findley <rfin...@google.com>
        TryBot-Result: Go Bot <go...@golang.org>
        Reviewed-by: Robert Griesemer <g...@golang.org>

        ---
        M src/go/types/decl.go
        M src/go/types/errorcodes.go
        M src/go/types/resolver.go
        A src/go/types/testdata/go1_8.src
        4 files changed, 30 insertions(+), 10 deletions(-)

        diff --git a/src/go/types/decl.go b/src/go/types/decl.go
        index 571e172..b861cde 100644
        @@ -678,9 +681,12 @@

        }

        + // TODO(rFindley): move to the callsite, as this is only needed for top-level
        + // decls.
        check.addMethodDecls(obj)
        }

        +// TODO(rFindley): rename to collectMethods, to be consistent with types2.
        func (check *Checker) addMethodDecls(obj *TypeName) {
        // get associated methods
        // (Checker.collectObjects only collects methods with non-blank names;
        @@ -691,7 +697,7 @@

        return
        }
        delete(check.methods, obj)
        - assert(!check.objMap[obj].alias) // don't use TypeName.IsAlias (requires fully set up object)
        + assert(!check.objMap[obj].aliasPos.IsValid()) // don't use TypeName.IsAlias (requires fully set up object)

        // use an objset to check for name conflicts
        var mset objset
        @@ -864,7 +870,7 @@

        check.declare(check.scope, d.spec.Name, obj, scopePos)
        // mark and unmark type before calling typeDecl; its type is still nil (see Checker.objDecl)
        obj.setColor(grey + color(check.push(obj)))
        - check.typeDecl(obj, d.spec.Type, nil, d.spec.Assign.IsValid())
        + check.typeDecl(obj, d.spec.Type, nil, d.spec.Assign)
        check.pop().setColor(black)
        default:
        check.invalidAST(d.node(), "unknown ast.Decl node %T", d.node())
        diff --git a/src/go/types/errorcodes.go b/src/go/types/errorcodes.go
        index d27abdf..ac28c3b 100644
        --- a/src/go/types/errorcodes.go
        +++ b/src/go/types/errorcodes.go
        @@ -1366,4 +1366,7 @@
        // return i
        // }
        _InvalidGo
        +
        + // _BadDecl occurs when a declaration has invalid syntax.
        + _BadDecl
        )
        diff --git a/src/go/types/resolver.go b/src/go/types/resolver.go
        index 47e165d..e441159 100644

        --- a/src/go/types/resolver.go
        +++ b/src/go/types/resolver.go
        @@ -23,7 +23,7 @@
        init ast.Expr // init/orig expression, or nil
        inherited bool // if set, the init expression is inherited from a previous constant declaration
        fdecl *ast.FuncDecl // func declaration, or nil
        - alias bool // type alias declaration
        +	aliasPos  token.Pos     // If valid, the decl is a type alias and aliasPos is the position of '='.
        The change was submitted with unreviewed changes in the following files: The name of the file: src/go/types/decl.go Insertions: 3, Deletions: 0. @@ +683:685 @@ + // TODO(rFindley): move to the callsite, as this is only needed for top-level + // decls. @@ +688:689 @@ + // TODO(rFindley): rename to collectMethods, to be consistent with types2.

        To view, visit change 291318. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: dev.regabi
        Gerrit-Change-Id: Ib15969f9cd5be30228b7a4c6406f978d6fc58018
        Gerrit-Change-Number: 291318
        Gerrit-PatchSet: 7
        Gerrit-Owner: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: Go Bot <go...@golang.org>
        Gerrit-Reviewer: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
        Gerrit-MessageType: merged
        Reply all
        Reply to author
        Forward
        0 new messages