IgnoreBranchErrors: true, // parser already checked via syntax.CheckBranches mode
Importer: &importer,
Sizes: types2.SizesFor("gc", buildcfg.GOARCH),
- // Currently, the compiler panics when using Alias types.
- // TODO(gri) set to true once this is fixed (issue #66873)
- EnableAlias: false,
+ EnableAlias: true,
}
if base.Flag.ErrorURL {
conf.ErrorURL = " [go.dev/e/%s]"
r.dict = dict
typ := r.doTyp()
- assert(typ != nil)
+ if typ == nil {
+ base.Fatalf("doTyp returned nil for info=%v", info)
+ }
// For recursive type declarations involving interfaces and aliases,
// above r.doTyp() call may have already set pr.typs[idx], so just
case pkgbits.ObjAlias:
name := do(ir.OTYPE, false)
- setType(name, r.typ())
+
+ // Clumsy dance: the r.typ() call here might recursively find this
+ // type alias name, before we've set its type (#66873). So we
+ // temporarily clear sym.Def and then restore it later, if still
+ // unset.
+ hack := sym.Def == name
+ if hack {
+ sym.Def = nil
+ }
+ typ := r.typ()
+ if hack {
+ if sym.Def != nil {
+ name = sym.Def.(*ir.Name)
+ assert(name.Type() == typ)
+ return name, nil
+ }
+ sym.Def = name
+ }
+
+ setType(name, typ)
name.SetAlias(true)
return name, nil
// typIdx also reports whether typ is a derived type; that is, whether
// its identity depends on type parameters.
func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
+ // Strip non-global aliases, because they only appear in inline
+ // bodies anyway. Otherwise, they can cause types.Sym collisions
+ // (e.g., "main.C" for both of the local type aliases in
+ // test/fixedbugs/issue50190.go).
+ for {
+ if alias, ok := typ.(*types2.Alias); ok && !isGlobal(alias.Obj()) {
+ typ = alias.Rhs()
+ } else {
+ break
+ }
+ }
+
if idx, ok := pw.typsIdx[typ]; ok {
return typeInfo{idx: idx, derived: false}
}
// But aliases and original types cannot be used with new types based on them.
var _ N0 = T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration"
-var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type T0\) as N0 value in variable declaration"
+var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type A0\) as N0 value in variable declaration"
var _ A5 = Value{}
var _ T0 = A0{}
var _ N0 = T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration"
- var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type T0\) as N0 value in variable declaration"
+ var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type A0\) as N0 value in variable declaration"
var _ A5 = Value{} // ERROR "cannot use Value{} \(value of type reflect\.Value\) as A5 value in variable declaration"
}
type _ = reflect.ValueOf // ERROR "reflect.ValueOf .*is not a type|expected type"
-func (A1) m() {} // ERROR "cannot define new methods on non-local type int|may not define methods on non-local type"
+func (A1) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type"
func (A2) m() {} // ERROR "invalid receiver type"
-func (A3) m() {} // ERROR "cannot define new methods on non-local type reflect.Value|may not define methods on non-local type"
-func (A4) m() {} // ERROR "cannot define new methods on non-local type reflect.Value|may not define methods on non-local type"
+func (A3) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type"
+func (A4) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type"
type B1 = struct{}
--- /dev/null
+// compile
+
+// Copyright 2024 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.
+
+package p
+
+func f(A) {}
+
+type T int
+
+type A = T
+
+func (A) m() {}