]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types2/decl.go
go/types, types2: introduce _Alias type node
[gostls13.git] / src / cmd / compile / internal / types2 / decl.go
index ed6b96eb090aa2c790a00b1b851de3d913a5f90a..6fd2c2cad8f112e1bd1fed4ed4b31520c33805cd 100644 (file)
@@ -251,10 +251,14 @@ loop:
                        // the syntactic information. We should consider storing
                        // this information explicitly in the object.
                        var alias bool
-                       if d := check.objMap[obj]; d != nil {
-                               alias = d.tdecl.Alias // package-level object
+                       if check.conf._EnableAlias {
+                               alias = obj.IsAlias()
                        } else {
-                               alias = obj.IsAlias() // function local object
+                               if d := check.objMap[obj]; d != nil {
+                                       alias = d.tdecl.Alias // package-level object
+                               } else {
+                                       alias = obj.IsAlias() // function local object
+                               }
                        }
                        if !alias {
                                ndef++
@@ -322,7 +326,11 @@ func (check *Checker) cycleError(cycle []Object) {
        // If obj is a type alias, mark it as valid (not broken) in order to avoid follow-on errors.
        tname, _ := obj.(*TypeName)
        if tname != nil && tname.IsAlias() {
-               check.validAlias(tname, Typ[Invalid])
+               // If we use Alias nodes, it is initialized with Typ[Invalid].
+               // TODO(gri) Adjust this code if we initialize with nil.
+               if !check.conf._EnableAlias {
+                       check.validAlias(tname, Typ[Invalid])
+               }
        }
 
        // report a more concise error for self references
@@ -495,20 +503,32 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
                _ = check.isImportedConstraint(rhs) && check.verifyVersionf(tdecl.Type, go1_18, "using type constraint %s", rhs)
        }).describef(obj, "validType(%s)", obj.Name())
 
-       alias := tdecl.Alias
-       if alias && tdecl.TParamList != nil {
+       aliasDecl := tdecl.Alias
+       if aliasDecl && tdecl.TParamList != nil {
                // The parser will ensure this but we may still get an invalid AST.
                // Complain and continue as regular type definition.
                check.error(tdecl, BadDecl, "generic type cannot be alias")
-               alias = false
+               aliasDecl = false
        }
 
        // alias declaration
-       if alias {
+       if aliasDecl {
                check.verifyVersionf(tdecl, go1_9, "type aliases")
-               check.brokenAlias(obj)
-               rhs = check.typ(tdecl.Type)
-               check.validAlias(obj, rhs)
+               if check.conf._EnableAlias {
+                       // TODO(gri) Should be able to use nil instead of Typ[Invalid] to mark
+                       //           the alias as incomplete. Currently this causes problems
+                       //           with certain cycles. Investigate.
+                       alias := check.newAlias(obj, Typ[Invalid])
+                       setDefType(def, alias)
+                       rhs = check.definedType(tdecl.Type, obj)
+                       assert(rhs != nil)
+                       alias.fromRHS = rhs
+                       _Unalias(alias) // resolve alias.actual
+               } else {
+                       check.brokenAlias(obj)
+                       rhs = check.typ(tdecl.Type)
+                       check.validAlias(obj, rhs)
+               }
                return
        }