Skip to content

Commit

Permalink
Merge pull request #469 from visualfc/validtype
Browse files Browse the repository at this point in the history
validType for check recursive
  • Loading branch information
xushiwei authored Feb 6, 2025
2 parents 2c8b37b + dc0284f commit 5afc508
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
22 changes: 22 additions & 0 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1386,4 +1386,26 @@ func TestAssignableUntyped(t *testing.T) {
}
}

func TestValidType(t *testing.T) {
var errs []error
conf := &Config{
HandleErr: func(err error) {
errs = append(errs, err)
},
}
pkg := NewPackage("", "foo", conf)
typeA := types.NewNamed(types.NewTypeName(token.NoPos, pkg.Types, "A", nil), nil, nil)
typeB := types.NewNamed(types.NewTypeName(token.NoPos, pkg.Types, "B", nil), nil, nil)
typeA.SetUnderlying(types.NewStruct([]*types.Var{
types.NewField(token.NoPos, pkg.Types, "B", typeB, true), // Embed B.
}, nil))
typeB.SetUnderlying(types.NewStruct([]*types.Var{
types.NewField(token.NoPos, pkg.Types, "A", typeA, true), // Embed A.
}, nil))
pkg.ValidType(typeA)
if len(errs) == 0 {
t.Fatal("TestValidType: no error?")
}
}

// ----------------------------------------------------------------------------
31 changes: 31 additions & 0 deletions validtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2024 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gogen

import (
"go/types"
_ "unsafe"
)

//go:linkname validType go/types.(*Checker).validType
func validType(check *types.Checker, typ *types.Named)

// ValidType verifies that the given type does not "expand" indefinitely
// producing a cycle in the type graph. Cycles are detected by marking
// defined types.
func (pkg *Package) ValidType(typ *types.Named) {
conf := &types.Config{Error: pkg.conf.HandleErr}
checker := types.NewChecker(conf, pkg.Fset, pkg.Types, nil)
validType(checker, typ)
}

0 comments on commit 5afc508

Please sign in to comment.