package solve

import (
	
	
	
	
	
	

	
	
	
)

// liftTypeParams uses supplied bindings and lifts the remaining parameters.
// Constraints are rewritten after all bindings are known.
func ( *solver) ( *discover.Provider,  map[*types.TypeParam]types.Type) []types.Type {
	 := .Rparams.Len() + .Tparams.Len()
	 := make([]types.Type, 0, )
	 := make(map[*types.TypeParam]types.Type, )
	var  []*types.TypeParam
	for  := range .TypeParams {
		,  := []
		if ! {
			 = .liftOne(, )
			 = append(, )
		}
		[] = 
		 = append(, )
	}
	for ,  := range  {
		 := [].(*types.TypeParam)
		if  := .Constraint();  != nil {
			.SetConstraint(gotypes.Subst(.ctxt, , ))
		}
	}
	return 
}

// liftOne creates a fresh injector type parameter standing in for an unpinned
// template parameter. Its name is chosen to avoid the destination’s identifiers
// and the predeclared names the body emits, so it never shadows anything the
// generated body references unqualified.
func ( *solver) ( *types.TypeParam,  *discover.Provider) *types.TypeParam {
	 := .freshLiftedName(.Obj().Name())
	// Globally-unique live lifted names are the whole basis of the solver’s
	// determinism: they are the only source of a CmpType weak-order tie (two
	// lifted parameters compare equal iff their names match; see gotypes.CmpType),
	// and a stable sort over the map-ordered demand/input/output slices would
	// otherwise be load-order-dependent. freshLiftedName guarantees uniqueness, so
	// a duplicate here means that guarantee (or rollbackLifts’ name release)
	// regressed; fail loud at the source rather than emit nondeterministic code.
	if .liftedNames[] {
		panic(fmt.Sprintf("plumb: lifted name %q reused; determinism relies on unique live lifted names", ))
	}
	 := types.NewTypeName(token.NoPos, nil, , nil)
	 := types.NewTypeParam(, nil)
	.SetConstraint(.Constraint())
	.liftedNames[] = true
	.liftedMeta = append(.liftedMeta, liftedParam{tp: , origin: , pos: .Pos})
	return 
}

// rollbackLifts undoes the speculative lifts appended since mark: it both
// truncates liftedMeta and releases the names those lifts reserved in liftedNames.
// Releasing the names is what lets a later successful lift reuse a name a failed
// attempt had taken (e.g. T rather than T2); liftedMeta and liftedNames must be
// rolled back together or the two go out of sync.
func ( *solver) ( int) {
	for ,  := range .liftedMeta[:] {
		delete(.liftedNames, .tp.Obj().Name())
	}
	.liftedMeta = .liftedMeta[:]
}

// freshLiftedName returns a valid identifier derived from base that collides
// with nothing the body references unqualified.
func ( *solver) ( string) string {
	if  == "" ||  == "_" {
		 = "T"
	}
	 := 
	for  := 2; .avoid[] || .liftedNames[]; ++ {
		 =  + strconv.Itoa()
	}
	return 
}

// orderedLifted returns the lifted parameters in deterministic header order:
// by provider position, then parameter declaration position.
func ( *solver) () []*types.TypeParam {
	 := slices.SortedStableFunc(slices.Values(.liftedMeta), func(,  liftedParam) int {
		return cmp.Or(
			diag.CmpPos(.pos, .pos),
			cmp.Compare(.origin.Obj().Pos(), .origin.Obj().Pos()),
		)
	})
	 := make([]*types.TypeParam, len())
	for ,  := range  {
		[] = .tp
	}
	return 
}