Skip to content

Commit 7696c94

Browse files
committed
[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 <[email protected]> Trust: Robert Griesemer <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent c2358a1 commit 7696c94

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

src/go/types/decl.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (check *Checker) objDecl(obj Object, def *Named) {
189189
check.varDecl(obj, d.lhs, d.typ, d.init)
190190
case *TypeName:
191191
// invalid recursive types are detected via path
192-
check.typeDecl(obj, d.typ, def, d.alias)
192+
check.typeDecl(obj, d.typ, def, d.aliasPos)
193193
case *Func:
194194
// functions may be recursive - no need to track dependencies
195195
check.funcDecl(obj, d)
@@ -234,7 +234,7 @@ func (check *Checker) cycle(obj Object) (isCycle bool) {
234234
// this information explicitly in the object.
235235
var alias bool
236236
if d := check.objMap[obj]; d != nil {
237-
alias = d.alias // package-level object
237+
alias = d.aliasPos.IsValid() // package-level object
238238
} else {
239239
alias = obj.IsAlias() // function local object
240240
}
@@ -640,14 +640,17 @@ func (n *Named) setUnderlying(typ Type) {
640640
}
641641
}
642642

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

646646
check.later(func() {
647647
check.validType(obj.typ, nil)
648648
})
649649

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

652655
obj.typ = Typ[Invalid]
653656
obj.typ = check.typ(typ)
@@ -678,9 +681,12 @@ func (check *Checker) typeDecl(obj *TypeName, typ ast.Expr, def *Named, alias bo
678681

679682
}
680683

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

689+
// TODO(rFindley): rename to collectMethods, to be consistent with types2.
684690
func (check *Checker) addMethodDecls(obj *TypeName) {
685691
// get associated methods
686692
// (Checker.collectObjects only collects methods with non-blank names;
@@ -691,7 +697,7 @@ func (check *Checker) addMethodDecls(obj *TypeName) {
691697
return
692698
}
693699
delete(check.methods, obj)
694-
assert(!check.objMap[obj].alias) // don't use TypeName.IsAlias (requires fully set up object)
700+
assert(!check.objMap[obj].aliasPos.IsValid()) // don't use TypeName.IsAlias (requires fully set up object)
695701

696702
// use an objset to check for name conflicts
697703
var mset objset
@@ -864,7 +870,7 @@ func (check *Checker) declStmt(d ast.Decl) {
864870
check.declare(check.scope, d.spec.Name, obj, scopePos)
865871
// mark and unmark type before calling typeDecl; its type is still nil (see Checker.objDecl)
866872
obj.setColor(grey + color(check.push(obj)))
867-
check.typeDecl(obj, d.spec.Type, nil, d.spec.Assign.IsValid())
873+
check.typeDecl(obj, d.spec.Type, nil, d.spec.Assign)
868874
check.pop().setColor(black)
869875
default:
870876
check.invalidAST(d.node(), "unknown ast.Decl node %T", d.node())

src/go/types/errorcodes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,4 +1366,7 @@ const (
13661366
// return i
13671367
// }
13681368
_InvalidGo
1369+
1370+
// _BadDecl occurs when a declaration has invalid syntax.
1371+
_BadDecl
13691372
)

src/go/types/resolver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type declInfo struct {
2323
init ast.Expr // init/orig expression, or nil
2424
inherited bool // if set, the init expression is inherited from a previous constant declaration
2525
fdecl *ast.FuncDecl // func declaration, or nil
26-
alias bool // type alias declaration
26+
aliasPos token.Pos // If valid, the decl is a type alias and aliasPos is the position of '='.
2727

2828
// The deps field tracks initialization expression dependencies.
2929
deps map[Object]bool // lazily initialized
@@ -366,7 +366,7 @@ func (check *Checker) collectObjects() {
366366
}
367367
case typeDecl:
368368
obj := NewTypeName(d.spec.Name.Pos(), pkg, d.spec.Name.Name, nil)
369-
check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, typ: d.spec.Type, alias: d.spec.Assign.IsValid()})
369+
check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, typ: d.spec.Type, aliasPos: d.spec.Assign})
370370
case funcDecl:
371371
info := &declInfo{file: fileScope, fdecl: d.decl}
372372
name := d.decl.Name.Name
@@ -493,7 +493,7 @@ func (check *Checker) resolveBaseTypeName(typ ast.Expr) (ptr bool, base *TypeNam
493493
// we're done if tdecl defined tname as a new type
494494
// (rather than an alias)
495495
tdecl := check.objMap[tname] // must exist for objects in package scope
496-
if !tdecl.alias {
496+
if !tdecl.aliasPos.IsValid() {
497497
return ptr, tname
498498
}
499499

@@ -534,7 +534,7 @@ func (check *Checker) packageObjects() {
534534
// phase 1
535535
for _, obj := range objList {
536536
// If we have a type alias, collect it for the 2nd phase.
537-
if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].alias {
537+
if tname, _ := obj.(*TypeName); tname != nil && check.objMap[tname].aliasPos.IsValid() {
538538
aliasList = append(aliasList, tname)
539539
continue
540540
}

src/go/types/testdata/go1_8.src

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2021 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+
// Check Go language version-specific errors.
6+
7+
package go1_8 // go1.8
8+
9+
// type alias declarations
10+
type any = /* ERROR type aliases requires go1.9 or later */ interface{}
11+

0 commit comments

Comments
 (0)