$98 GRAYBYTE WORDPRESS FILE MANAGER $34

SERVER : vnpttt-amd7f72-h1.vietnix.vn #1 SMP Fri May 24 12:42:50 UTC 2024
SERVER IP : 103.200.23.149 | ADMIN IP 216.73.216.22
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/lib/golang/src/go/types/

HOME
Current File : /lib/golang/src/go/types//validtype.go
// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
// Source: ../../cmd/compile/internal/types2/validtype.go

// Copyright 2022 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 types

import "go/token"

// validType verifies that the given type does not "expand" indefinitely
// producing a cycle in the type graph.
// (Cycles involving alias types, as in "type A = [10]A" are detected
// earlier, via the objDecl cycle detection mechanism.)
func (check *Checker) validType(typ *Named) {
	check.validType0(nopos, typ, nil, nil)
}

// validType0 checks if the given type is valid. If typ is a type parameter
// its value is looked up in the type argument list of the instantiated
// (enclosing) type, if it exists. Otherwise the type parameter must be from
// an enclosing function and can be ignored.
// The nest list describes the stack (the "nest in memory") of types which
// contain (or embed in the case of interfaces) other types. For instance, a
// struct named S which contains a field of named type F contains (the memory
// of) F in S, leading to the nest S->F. If a type appears in its own nest
// (say S->F->S) we have an invalid recursive type. The path list is the full
// path of named types in a cycle, it is only needed for error reporting.
func (check *Checker) validType0(pos token.Pos, typ Type, nest, path []*Named) bool {
	typ = Unalias(typ)

	if check.conf._Trace {
		if t, _ := typ.(*Named); t != nil && t.obj != nil /* obj should always exist but be conservative */ {
			pos = t.obj.pos
		}
		check.indent++
		check.trace(pos, "validType(%s) nest %v, path %v", typ, pathString(makeObjList(nest)), pathString(makeObjList(path)))
		defer func() {
			check.indent--
		}()
	}

	switch t := typ.(type) {
	case nil:
		// We should never see a nil type but be conservative and panic
		// only in debug mode.
		if debug {
			panic("validType0(nil)")
		}

	case *Array:
		return check.validType0(pos, t.elem, nest, path)

	case *Struct:
		for _, f := range t.fields {
			if !check.validType0(pos, f.typ, nest, path) {
				return false
			}
		}

	case *Union:
		for _, t := range t.terms {
			if !check.validType0(pos, t.typ, nest, path) {
				return false
			}
		}

	case *Interface:
		for _, etyp := range t.embeddeds {
			if !check.validType0(pos, etyp, nest, path) {
				return false
			}
		}

	case *Named:
		// TODO(gri) The optimization below is incorrect (see go.dev/issue/65711):
		//           in that issue `type A[P any] [1]P` is a valid type on its own
		//           and the (uninstantiated) A is recorded in check.valids. As a
		//           consequence, when checking the remaining declarations, which
		//           are not valid, the validity check ends prematurely because A
		//           is considered valid, even though its validity depends on the
		//           type argument provided to it.
		//
		//           A correct optimization is important for pathological cases.
		//           Keep code around for reference until we found an optimization.
		//
		// // Exit early if we already know t is valid.
		// // This is purely an optimization but it prevents excessive computation
		// // times in pathological cases such as testdata/fixedbugs/issue6977.go.
		// // (Note: The valids map could also be allocated locally, once for each
		// // validType call.)
		// if check.valids.lookup(t) != nil {
		// 	break
		// }

		// Don't report a 2nd error if we already know the type is invalid
		// (e.g., if a cycle was detected earlier, via under).
		// Note: ensure that t.orig is fully resolved by calling Underlying().
		if !isValid(t.Underlying()) {
			return false
		}

		// If the current type t is also found in nest, (the memory of) t is
		// embedded in itself, indicating an invalid recursive type.
		for _, e := range nest {
			if Identical(e, t) {
				// We have a cycle. If t != t.Origin() then t is an instance of
				// the generic type t.Origin(). Because t is in the nest, t must
				// occur within the definition (RHS) of the generic type t.Origin(),
				// directly or indirectly, after expansion of the RHS.
				// Therefore t.Origin() must be invalid, no matter how it is
				// instantiated since the instantiation t of t.Origin() happens
				// inside t.Origin()'s RHS and thus is always the same and always
				// present.
				// Therefore we can mark the underlying of both t and t.Origin()
				// as invalid. If t is not an instance of a generic type, t and
				// t.Origin() are the same.
				// Furthermore, because we check all types in a package for validity
				// before type checking is complete, any exported type that is invalid
				// will have an invalid underlying type and we can't reach here with
				// such a type (invalid types are excluded above).
				// Thus, if we reach here with a type t, both t and t.Origin() (if
				// different in the first place) must be from the current package;
				// they cannot have been imported.
				// Therefore it is safe to change their underlying types; there is
				// no chance for a race condition (the types of the current package
				// are not yet available to other goroutines).
				assert(t.obj.pkg == check.pkg)
				assert(t.Origin().obj.pkg == check.pkg)
				t.underlying = Typ[Invalid]
				t.Origin().underlying = Typ[Invalid]

				// Find the starting point of the cycle and report it.
				// Because each type in nest must also appear in path (see invariant below),
				// type t must be in path since it was found in nest. But not every type in path
				// is in nest. Specifically t may appear in path with an earlier index than the
				// index of t in nest. Search again.
				for start, p := range path {
					if Identical(p, t) {
						check.cycleError(makeObjList(path[start:]), 0)
						return false
					}
				}
				panic("cycle start not found")
			}
		}

		// No cycle was found. Check the RHS of t.
		// Every type added to nest is also added to path; thus every type that is in nest
		// must also be in path (invariant). But not every type in path is in nest, since
		// nest may be pruned (see below, *TypeParam case).
		if !check.validType0(pos, t.Origin().fromRHS, append(nest, t), append(path, t)) {
			return false
		}

		// see TODO above
		// check.valids.add(t) // t is valid

	case *TypeParam:
		// A type parameter stands for the type (argument) it was instantiated with.
		// Check the corresponding type argument for validity if we are in an
		// instantiated type.
		if d := len(nest) - 1; d >= 0 {
			inst := nest[d] // the type instance
			// Find the corresponding type argument for the type parameter
			// and proceed with checking that type argument.
			for i, tparam := range inst.TypeParams().list() {
				// The type parameter and type argument lists should
				// match in length but be careful in case of errors.
				if t == tparam && i < inst.TypeArgs().Len() {
					targ := inst.TypeArgs().At(i)
					// The type argument must be valid in the enclosing
					// type (where inst was instantiated), hence we must
					// check targ's validity in the type nest excluding
					// the current (instantiated) type (see the example
					// at the end of this file).
					// For error reporting we keep the full path.
					res := check.validType0(pos, targ, nest[:d], path)
					// The check.validType0 call with nest[:d] may have
					// overwritten the entry at the current depth d.
					// Restore the entry (was issue go.dev/issue/66323).
					nest[d] = inst
					return res
				}
			}
		}
	}

	return true
}

// makeObjList returns the list of type name objects for the given
// list of named types.
func makeObjList(tlist []*Named) []Object {
	olist := make([]Object, len(tlist))
	for i, t := range tlist {
		olist[i] = t.obj
	}
	return olist
}

// Here is an example illustrating why we need to exclude the
// instantiated type from nest when evaluating the validity of
// a type parameter. Given the declarations
//
//   var _ A[A[string]]
//
//   type A[P any] struct { _ B[P] }
//   type B[P any] struct { _ P }
//
// we want to determine if the type A[A[string]] is valid.
// We start evaluating A[A[string]] outside any type nest:
//
//   A[A[string]]
//         nest =
//         path =
//
// The RHS of A is now evaluated in the A[A[string]] nest:
//
//   struct{_ B[P₁]}
//         nest = A[A[string]]
//         path = A[A[string]]
//
// The struct has a single field of type B[P₁] with which
// we continue:
//
//   B[P₁]
//         nest = A[A[string]]
//         path = A[A[string]]
//
//   struct{_ P₂}
//         nest = A[A[string]]->B[P]
//         path = A[A[string]]->B[P]
//
// Eventually we reach the type parameter P of type B (P₂):
//
//   P₂
//         nest = A[A[string]]->B[P]
//         path = A[A[string]]->B[P]
//
// The type argument for P of B is the type parameter P of A (P₁).
// It must be evaluated in the type nest that existed when B was
// instantiated:
//
//   P₁
//         nest = A[A[string]]        <== type nest at B's instantiation time
//         path = A[A[string]]->B[P]
//
// If we'd use the current nest it would correspond to the path
// which will be wrong as we will see shortly. P's type argument
// is A[string], which again must be evaluated in the type nest
// that existed when A was instantiated with A[string]. That type
// nest is empty:
//
//   A[string]
//         nest =                     <== type nest at A's instantiation time
//         path = A[A[string]]->B[P]
//
// Evaluation then proceeds as before for A[string]:
//
//   struct{_ B[P₁]}
//         nest = A[string]
//         path = A[A[string]]->B[P]->A[string]
//
// Now we reach B[P] again. If we had not adjusted nest, it would
// correspond to path, and we would find B[P] in nest, indicating
// a cycle, which would clearly be wrong since there's no cycle in
// A[string]:
//
//   B[P₁]
//         nest = A[string]
//         path = A[A[string]]->B[P]->A[string]  <== path contains B[P]!
//
// But because we use the correct type nest, evaluation proceeds without
// errors and we get the evaluation sequence:
//
//   struct{_ P₂}
//         nest = A[string]->B[P]
//         path = A[A[string]]->B[P]->A[string]->B[P]
//   P₂
//         nest = A[string]->B[P]
//         path = A[A[string]]->B[P]->A[string]->B[P]
//   P₁
//         nest = A[string]
//         path = A[A[string]]->B[P]->A[string]->B[P]
//   string
//         nest =
//         path = A[A[string]]->B[P]->A[string]->B[P]
//
// At this point we're done and A[A[string]] and is valid.

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
4 Dec 2025 6.06 PM
root / root
0755
README.md
0.133 KB
4 Dec 2025 6.06 PM
root / root
0644
alias.go
6.063 KB
4 Dec 2025 6.06 PM
root / root
0644
api.go
17.887 KB
4 Dec 2025 6.06 PM
root / root
0644
api_predicates.go
3.451 KB
4 Dec 2025 6.06 PM
root / root
0644
array.go
0.905 KB
4 Dec 2025 6.06 PM
root / root
0644
assignments.go
16.889 KB
4 Dec 2025 6.06 PM
root / root
0644
badlinkname.go
0.686 KB
4 Dec 2025 6.06 PM
root / root
0644
basic.go
1.605 KB
4 Dec 2025 6.06 PM
root / root
0644
builtins.go
29.413 KB
4 Dec 2025 6.06 PM
root / root
0644
call.go
33.833 KB
4 Dec 2025 6.06 PM
root / root
0644
chan.go
1.009 KB
4 Dec 2025 6.06 PM
root / root
0644
check.go
21.786 KB
4 Dec 2025 6.06 PM
root / root
0644
const.go
7.618 KB
4 Dec 2025 6.06 PM
root / root
0644
context.go
4.429 KB
4 Dec 2025 6.06 PM
root / root
0644
conversions.go
9.082 KB
4 Dec 2025 6.06 PM
root / root
0644
decl.go
31.035 KB
4 Dec 2025 6.06 PM
root / root
0644
errors.go
8.49 KB
4 Dec 2025 6.06 PM
root / root
0644
errsupport.go
4.528 KB
4 Dec 2025 6.06 PM
root / root
0644
eval.go
3.069 KB
4 Dec 2025 6.06 PM
root / root
0644
expr.go
39.407 KB
4 Dec 2025 6.06 PM
root / root
0644
exprstring.go
4.781 KB
4 Dec 2025 6.06 PM
root / root
0644
format.go
4.031 KB
4 Dec 2025 6.06 PM
root / root
0644
gccgosizes.go
1.143 KB
4 Dec 2025 6.06 PM
root / root
0644
gcsizes.go
4.357 KB
4 Dec 2025 6.06 PM
root / root
0644
generate.go
0.263 KB
4 Dec 2025 6.06 PM
root / root
0644
gotype.go
8.33 KB
4 Dec 2025 6.06 PM
root / root
0644
index.go
11.772 KB
4 Dec 2025 6.06 PM
root / root
0644
infer.go
27.142 KB
4 Dec 2025 6.06 PM
root / root
0644
initorder.go
10.131 KB
4 Dec 2025 6.06 PM
root / root
0644
instantiate.go
13.211 KB
4 Dec 2025 6.06 PM
root / root
0644
interface.go
8.155 KB
4 Dec 2025 6.06 PM
root / root
0644
iter.go
3.873 KB
4 Dec 2025 6.06 PM
root / root
0644
labels.go
7.295 KB
4 Dec 2025 6.06 PM
root / root
0644
literals.go
12.77 KB
4 Dec 2025 6.06 PM
root / root
0644
lookup.go
22.653 KB
4 Dec 2025 6.06 PM
root / root
0644
map.go
0.763 KB
4 Dec 2025 6.06 PM
root / root
0644
methodset.go
7.058 KB
4 Dec 2025 6.06 PM
root / root
0644
mono.go
9.194 KB
4 Dec 2025 6.06 PM
root / root
0644
named.go
24.34 KB
4 Dec 2025 6.06 PM
root / root
0644
object.go
22.06 KB
4 Dec 2025 6.06 PM
root / root
0644
objset.go
1.028 KB
4 Dec 2025 6.06 PM
root / root
0644
operand.go
12.16 KB
4 Dec 2025 6.06 PM
root / root
0644
package.go
3.05 KB
4 Dec 2025 6.06 PM
root / root
0644
pointer.go
0.743 KB
4 Dec 2025 6.06 PM
root / root
0644
predicates.go
17.609 KB
4 Dec 2025 6.06 PM
root / root
0644
range.go
9.338 KB
4 Dec 2025 6.06 PM
root / root
0644
recording.go
4.648 KB
4 Dec 2025 6.06 PM
root / root
0644
resolver.go
25.255 KB
4 Dec 2025 6.06 PM
root / root
0644
return.go
4.232 KB
4 Dec 2025 6.06 PM
root / root
0644
scope.go
7.632 KB
4 Dec 2025 6.06 PM
root / root
0644
scope2.go
2.554 KB
4 Dec 2025 6.06 PM
root / root
0644
selection.go
5.906 KB
4 Dec 2025 6.06 PM
root / root
0644
signature.go
17.81 KB
4 Dec 2025 6.06 PM
root / root
0644
sizes.go
8.939 KB
4 Dec 2025 6.06 PM
root / root
0644
slice.go
0.685 KB
4 Dec 2025 6.06 PM
root / root
0644
stmt.go
23.696 KB
4 Dec 2025 6.06 PM
root / root
0644
struct.go
6.149 KB
4 Dec 2025 6.06 PM
root / root
0644
subst.go
10.411 KB
4 Dec 2025 6.06 PM
root / root
0644
termlist.go
3.894 KB
4 Dec 2025 6.06 PM
root / root
0644
tuple.go
1.028 KB
4 Dec 2025 6.06 PM
root / root
0644
type.go
0.528 KB
4 Dec 2025 6.06 PM
root / root
0644
typelists.go
1.967 KB
4 Dec 2025 6.06 PM
root / root
0644
typeparam.go
5.224 KB
4 Dec 2025 6.06 PM
root / root
0644
typeset.go
13.31 KB
4 Dec 2025 6.06 PM
root / root
0644
typestring.go
12.479 KB
4 Dec 2025 6.06 PM
root / root
0644
typeterm.go
3.643 KB
4 Dec 2025 6.06 PM
root / root
0644
typexpr.go
16.179 KB
4 Dec 2025 6.06 PM
root / root
0644
under.go
4.331 KB
4 Dec 2025 6.06 PM
root / root
0644
unify.go
27.898 KB
4 Dec 2025 6.06 PM
root / root
0644
union.go
6.225 KB
4 Dec 2025 6.06 PM
root / root
0644
universe.go
9.354 KB
4 Dec 2025 6.06 PM
root / root
0644
util.go
1.9 KB
4 Dec 2025 6.06 PM
root / root
0644
validtype.go
10.345 KB
4 Dec 2025 6.06 PM
root / root
0644
version.go
2.036 KB
4 Dec 2025 6.06 PM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF