From 691f5b8e74e7a2d11740f54a4a6827ff7ec9f661 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 28 Feb 2024 11:49:37 -0800 Subject: [PATCH] go/types: generate typeset.go from types2 source To simplify the translation, use extra atPos calls where needed in the respective types2 source. This CL reduces the amount of code that needs to be maintained manually by about 420 LOC. Change-Id: I839844a6e85ccb1111d76c43de23127d8f9fbbce Reviewed-on: https://go-review.googlesource.com/c/go/+/567776 Reviewed-by: Robert Findley Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer LUCI-TryBot-Result: Go LUCI --- src/cmd/compile/internal/types2/typeset.go | 24 +++++++++++++--------- src/go/types/generate_test.go | 1 + src/go/types/typeset.go | 14 +++++++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/cmd/compile/internal/types2/typeset.go b/src/cmd/compile/internal/types2/typeset.go index 8913a2145d..778809e42e 100644 --- a/src/cmd/compile/internal/types2/typeset.go +++ b/src/cmd/compile/internal/types2/typeset.go @@ -161,6 +161,10 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ // set (and don't store it!), so that we still compute the full // type set eventually. Instead, return the top type set and // let any follow-on errors play out. + // + // TODO(gri) Consider recording when this happens and reporting + // it as an error (but only if there were no other errors so to + // to not have unnecessary follow-on errors). if !ityp.complete { return &topTypeSet } @@ -222,8 +226,8 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ case explicit: if check != nil { err := check.newError(DuplicateDecl) - err.addf(pos, "duplicate method %s", m.name) - err.addf(mpos[other.(*Func)], "other declaration of %s", m.name) + err.addf(atPos(pos), "duplicate method %s", m.name) + err.addf(atPos(mpos[other.(*Func)]), "other declaration of %s", m.name) err.report() } default: @@ -234,13 +238,13 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ // error message. if check != nil { check.later(func() { - if !check.allowVersion(m.pkg, pos, go1_14) || !Identical(m.typ, other.Type()) { + if !check.allowVersion(m.pkg, atPos(pos), go1_14) || !Identical(m.typ, other.Type()) { err := check.newError(DuplicateDecl) - err.addf(pos, "duplicate method %s", m.name) - err.addf(mpos[other.(*Func)], "other declaration of %s", m.name) + err.addf(atPos(pos), "duplicate method %s", m.name) + err.addf(atPos(mpos[other.(*Func)]), "other declaration of %s", m.name) err.report() } - }).describef(pos, "duplicate method check for %s", m.name) + }).describef(atPos(pos), "duplicate method check for %s", m.name) } } } @@ -268,7 +272,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ assert(!isTypeParam(typ)) tset := computeInterfaceTypeSet(check, pos, u) // If typ is local, an error was already reported where typ is specified/defined. - if check != nil && check.isImportedConstraint(typ) && !check.verifyVersionf(pos, go1_18, "embedding constraint interface %s", typ) { + if check != nil && check.isImportedConstraint(typ) && !check.verifyVersionf(atPos(pos), go1_18, "embedding constraint interface %s", typ) { continue } comparable = tset.comparable @@ -277,7 +281,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ } terms = tset.terms case *Union: - if check != nil && !check.verifyVersionf(pos, go1_18, "embedding interface element %s", u) { + if check != nil && !check.verifyVersionf(atPos(pos), go1_18, "embedding interface element %s", u) { continue } tset := computeUnionTypeSet(check, unionSets, pos, u) @@ -291,7 +295,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ if !isValid(u) { continue } - if check != nil && !check.verifyVersionf(pos, go1_18, "embedding non-interface type %s", typ) { + if check != nil && !check.verifyVersionf(atPos(pos), go1_18, "embedding non-interface type %s", typ) { continue } terms = termlist{{false, typ}} @@ -401,7 +405,7 @@ func computeUnionTypeSet(check *Checker, unionSets map[*Union]*_TypeSet, pos syn allTerms = allTerms.union(terms) if len(allTerms) > maxTermCount { if check != nil { - check.errorf(pos, InvalidUnion, "cannot handle more than %d union terms (implementation limitation)", maxTermCount) + check.errorf(atPos(pos), InvalidUnion, "cannot handle more than %d union terms (implementation limitation)", maxTermCount) } unionSets[utyp] = &invalidTypeSet return unionSets[utyp] diff --git a/src/go/types/generate_test.go b/src/go/types/generate_test.go index a85dfc9b42..f7ba479c3b 100644 --- a/src/go/types/generate_test.go +++ b/src/go/types/generate_test.go @@ -165,6 +165,7 @@ var filemap = map[string]action{ "termlist_test.go": nil, "tuple.go": nil, "typelists.go": nil, + "typeset.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") }, "typeparam.go": nil, "typeterm_test.go": nil, "typeterm.go": nil, diff --git a/src/go/types/typeset.go b/src/go/types/typeset.go index 4e3865ddc4..16bc62cc19 100644 --- a/src/go/types/typeset.go +++ b/src/go/types/typeset.go @@ -1,3 +1,5 @@ +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. + // 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. @@ -225,8 +227,10 @@ func computeInterfaceTypeSet(check *Checker, pos token.Pos, ityp *Interface) *_T mpos[m] = pos case explicit: if check != nil { - check.errorf(atPos(pos), DuplicateDecl, "duplicate method %s", m.name) - check.errorf(atPos(mpos[other.(*Func)]), DuplicateDecl, "\tother declaration of %s", m.name) // secondary error, \t indented + err := check.newError(DuplicateDecl) + err.addf(atPos(pos), "duplicate method %s", m.name) + err.addf(atPos(mpos[other.(*Func)]), "other declaration of %s", m.name) + err.report() } default: // We have a duplicate method name in an embedded (not explicitly declared) method. @@ -237,8 +241,10 @@ func computeInterfaceTypeSet(check *Checker, pos token.Pos, ityp *Interface) *_T if check != nil { check.later(func() { if !check.allowVersion(m.pkg, atPos(pos), go1_14) || !Identical(m.typ, other.Type()) { - check.errorf(atPos(pos), DuplicateDecl, "duplicate method %s", m.name) - check.errorf(atPos(mpos[other.(*Func)]), DuplicateDecl, "\tother declaration of %s", m.name) // secondary error, \t indented + err := check.newError(DuplicateDecl) + err.addf(atPos(pos), "duplicate method %s", m.name) + err.addf(atPos(mpos[other.(*Func)]), "other declaration of %s", m.name) + err.report() } }).describef(atPos(pos), "duplicate method check for %s", m.name) } -- 2.48.1