1
0
mirror of https://github.com/golang/go synced 2024-11-15 00:30:31 -07:00

go/types, types2: factor out check for updated type arguments (cleanup)

Change-Id: I3e2668e4a24c145f121199a5f7f4278ff5d5f1da
Reviewed-on: https://go-review.googlesource.com/c/go/+/587676
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Robert Griesemer 2024-05-22 19:36:05 -07:00 committed by Gopher Robot
parent f294ddeb29
commit 83ff4fd571
2 changed files with 24 additions and 110 deletions

View File

@ -105,32 +105,19 @@ func (subst *subster) typ(typ Type) Type {
} }
// TODO(gri) do we need this for Alias types? // TODO(gri) do we need this for Alias types?
var newTArgs []Type
if t.TypeArgs().Len() != n { if t.TypeArgs().Len() != n {
return Typ[Invalid] // error reported elsewhere return Typ[Invalid] // error reported elsewhere
} }
// already instantiated // already instantiated
// For each (existing) type argument targ, determine if it needs // For each (existing) type argument determine if it needs
// to be substituted; i.e., if it is or contains a type parameter // to be substituted; i.e., if it is or contains a type parameter
// that has a type argument for it. // that has a type argument for it.
for i, targ := range t.TypeArgs().list() { targs, updated := subst.typeList(t.TypeArgs().list())
new_targ := subst.typ(targ) if updated {
if new_targ != targ { return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.ctxt)
if newTArgs == nil {
newTArgs = make([]Type, n)
copy(newTArgs, t.TypeArgs().list())
}
newTArgs[i] = new_targ
}
} }
if newTArgs == nil {
return t // nothing to substitute
}
return subst.check.newAliasInstance(subst.pos, t.orig, newTArgs, subst.ctxt)
case *Array: case *Array:
elem := subst.typOrNil(t.elem) elem := subst.typOrNil(t.elem)
if elem != t.elem { if elem != t.elem {
@ -245,18 +232,6 @@ func (subst *subster) typ(typ Type) Type {
} }
case *Named: case *Named:
// dump is for debugging
dump := func(string, ...interface{}) {}
if subst.check != nil && subst.check.conf.Trace {
subst.check.indent++
defer func() {
subst.check.indent--
}()
dump = func(format string, args ...interface{}) {
subst.check.trace(subst.pos, format, args...)
}
}
// subst is called during expansion, so in this function we need to be // subst is called during expansion, so in this function we need to be
// careful not to call any methods that would cause t to be expanded: doing // careful not to call any methods that would cause t to be expanded: doing
// so would result in deadlock. // so would result in deadlock.
@ -265,44 +240,26 @@ func (subst *subster) typ(typ Type) Type {
orig := t.Origin() orig := t.Origin()
n := orig.TypeParams().Len() n := orig.TypeParams().Len()
if n == 0 { if n == 0 {
dump(">>> %s is not parameterized", t)
return t // type is not parameterized return t // type is not parameterized
} }
var newTArgs []Type
if t.TypeArgs().Len() != n { if t.TypeArgs().Len() != n {
return Typ[Invalid] // error reported elsewhere return Typ[Invalid] // error reported elsewhere
} }
// already instantiated // already instantiated
dump(">>> %s already instantiated", t) // For each (existing) type argument determine if it needs
// For each (existing) type argument targ, determine if it needs
// to be substituted; i.e., if it is or contains a type parameter // to be substituted; i.e., if it is or contains a type parameter
// that has a type argument for it. // that has a type argument for it.
for i, targ := range t.TypeArgs().list() { targs, updated := subst.typeList(t.TypeArgs().list())
dump(">>> %d targ = %s", i, targ) if updated {
new_targ := subst.typ(targ) // Create a new instance and populate the context to avoid endless
if new_targ != targ { // recursion. The position used here is irrelevant because validation only
dump(">>> substituted %d targ %s => %s", i, targ, new_targ) // occurs on t (we don't call validType on named), but we use subst.pos to
if newTArgs == nil { // help with debugging.
newTArgs = make([]Type, n) return subst.check.instance(subst.pos, orig, targs, subst.expanding, subst.ctxt)
copy(newTArgs, t.TypeArgs().list())
}
newTArgs[i] = new_targ
}
} }
if newTArgs == nil {
dump(">>> nothing to substitute in %s", t)
return t // nothing to substitute
}
// Create a new instance and populate the context to avoid endless
// recursion. The position used here is irrelevant because validation only
// occurs on t (we don't call validType on named), but we use subst.pos to
// help with debugging.
return subst.check.instance(subst.pos, orig, newTArgs, subst.expanding, subst.ctxt)
case *TypeParam: case *TypeParam:
return subst.smap.lookup(t) return subst.smap.lookup(t)

View File

@ -108,32 +108,19 @@ func (subst *subster) typ(typ Type) Type {
} }
// TODO(gri) do we need this for Alias types? // TODO(gri) do we need this for Alias types?
var newTArgs []Type
if t.TypeArgs().Len() != n { if t.TypeArgs().Len() != n {
return Typ[Invalid] // error reported elsewhere return Typ[Invalid] // error reported elsewhere
} }
// already instantiated // already instantiated
// For each (existing) type argument targ, determine if it needs // For each (existing) type argument determine if it needs
// to be substituted; i.e., if it is or contains a type parameter // to be substituted; i.e., if it is or contains a type parameter
// that has a type argument for it. // that has a type argument for it.
for i, targ := range t.TypeArgs().list() { targs, updated := subst.typeList(t.TypeArgs().list())
new_targ := subst.typ(targ) if updated {
if new_targ != targ { return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.ctxt)
if newTArgs == nil {
newTArgs = make([]Type, n)
copy(newTArgs, t.TypeArgs().list())
}
newTArgs[i] = new_targ
}
} }
if newTArgs == nil {
return t // nothing to substitute
}
return subst.check.newAliasInstance(subst.pos, t.orig, newTArgs, subst.ctxt)
case *Array: case *Array:
elem := subst.typOrNil(t.elem) elem := subst.typOrNil(t.elem)
if elem != t.elem { if elem != t.elem {
@ -248,18 +235,6 @@ func (subst *subster) typ(typ Type) Type {
} }
case *Named: case *Named:
// dump is for debugging
dump := func(string, ...interface{}) {}
if subst.check != nil && subst.check.conf._Trace {
subst.check.indent++
defer func() {
subst.check.indent--
}()
dump = func(format string, args ...interface{}) {
subst.check.trace(subst.pos, format, args...)
}
}
// subst is called during expansion, so in this function we need to be // subst is called during expansion, so in this function we need to be
// careful not to call any methods that would cause t to be expanded: doing // careful not to call any methods that would cause t to be expanded: doing
// so would result in deadlock. // so would result in deadlock.
@ -268,44 +243,26 @@ func (subst *subster) typ(typ Type) Type {
orig := t.Origin() orig := t.Origin()
n := orig.TypeParams().Len() n := orig.TypeParams().Len()
if n == 0 { if n == 0 {
dump(">>> %s is not parameterized", t)
return t // type is not parameterized return t // type is not parameterized
} }
var newTArgs []Type
if t.TypeArgs().Len() != n { if t.TypeArgs().Len() != n {
return Typ[Invalid] // error reported elsewhere return Typ[Invalid] // error reported elsewhere
} }
// already instantiated // already instantiated
dump(">>> %s already instantiated", t) // For each (existing) type argument determine if it needs
// For each (existing) type argument targ, determine if it needs
// to be substituted; i.e., if it is or contains a type parameter // to be substituted; i.e., if it is or contains a type parameter
// that has a type argument for it. // that has a type argument for it.
for i, targ := range t.TypeArgs().list() { targs, updated := subst.typeList(t.TypeArgs().list())
dump(">>> %d targ = %s", i, targ) if updated {
new_targ := subst.typ(targ) // Create a new instance and populate the context to avoid endless
if new_targ != targ { // recursion. The position used here is irrelevant because validation only
dump(">>> substituted %d targ %s => %s", i, targ, new_targ) // occurs on t (we don't call validType on named), but we use subst.pos to
if newTArgs == nil { // help with debugging.
newTArgs = make([]Type, n) return subst.check.instance(subst.pos, orig, targs, subst.expanding, subst.ctxt)
copy(newTArgs, t.TypeArgs().list())
}
newTArgs[i] = new_targ
}
} }
if newTArgs == nil {
dump(">>> nothing to substitute in %s", t)
return t // nothing to substitute
}
// Create a new instance and populate the context to avoid endless
// recursion. The position used here is irrelevant because validation only
// occurs on t (we don't call validType on named), but we use subst.pos to
// help with debugging.
return subst.check.instance(subst.pos, orig, newTArgs, subst.expanding, subst.ctxt)
case *TypeParam: case *TypeParam:
return subst.smap.lookup(t) return subst.smap.lookup(t)