Generic Constraints
Reference index · Summary of this topic
A Go generic constraint becomes a C# where clause. Most type-set constraints lift to the matching golib/.NET interface — a []T element constraint to ISlice<T>, [N]E array-core to IArray<E>, map[K]V to IMap<K,V>, chan T to IChannel<T> — plus, for operator-bearing type sets, the System.Numerics operator interfaces (IAdditionOperators, IComparisonOperators, …) so the body’s +/</== on the type parameter compile. The Go built-in comparable emits no C# constraint at all (see The comparable constraint).
An array-core constraint ~[N]E lifts to IArray<E>
A type-set constraint whose core is an ARRAY — func polyAdd[T ~[256]fieldElement](a, b T) T (ML-KEM’s
ringElement/nttElement in crypto/internal/fips140/mlkem share the core [256]fieldElement) — must
map to where T : IArray<E>, NOT to the operator interfaces the general type-set path would produce. An
array is a comparable type in Go, so the operator-set resolver would lift IEqualityOperators<T, T,
bool>; the named-array generated wrapper (which the converter emits for ringElement etc.) does not
implement that interface, so every instantiation would fail CS0315, and the interface exposes no array
surface, so the body’s t[i] (CS0021), for i := range t (CS8130 on the index deconstruction), and
for _, x := range t (CS1579/CS8183) would have nothing to bind against. getArrayConstraintElem (in
constraintOperations.go, a branch of getGenericDefinition) detects a single-array-core type set,
extracts the element type, and emits where T : /* ~[N]E */ IArray<E>, new(). The array wrapper already
declares IArray<E>, ISupportMake<wrapper> (the go2cs-gen Array inherited-type template), whose
ref E this[nint] indexer and (index, value) enumeration (IArray<E>.GetEnumerator) supply exactly the indexing/ranging
surface the body needs, and the new() (appended by the same path) covers var f T/T{} construction.
(Guarded by
GenericArrayConstraint — two array-wrapper types over a shared
~[4]fieldElement core through a generic function that indexes, index-ranges, value-ranges, and
constructs the type parameter, values vs Go.)
Slicing a type-parameter array shares its storage
a[1:], a[lo:hi] and a[0:1:2] over a value of such a type parameter emit a[1..], a.slice(lo, hi) and
a.slice(0, 1, 2), the forms a concrete array takes. IArray<E> declares all four slicing members (the Range
indexer and slice(low), slice(low, high), slice(low, high, max)) as default members. array<E>, slice<E> and
every generated named-array wrapper implement them with their own public members, so the slice shares the
argument’s storage and a write through it reaches the array, as in Go. The parameter itself is the function’s own
copy (a = a.ΔCopy(); at entry, when the body slices it or takes an element’s address), so the caller’s array
does not change.
(Guarded by TypeParamArraySlice: seven slicing forms over ~[2]int and ~[3]int, named and unnamed type
arguments, values vs Go.)
A type set of COMPOSITE terms lifts nothing — the union survives only as a comment
The array-core entry above is one SHAPE of a wider rule; runtime/pprof’s
testProfileRecordNullPadding[T runtime.StackRecord | runtime.MemProfileRecord |
runtime.BlockProfileRecord] shows the rest — a union whose terms are all plain structs (five call
sites, each error CS0315 without the rule).
The root is what IEqualityOperators<T, T, bool> MEANS on each side. Go’s == works on any comparable
type, so listing Struct, Array, Pointer and Channel in the operator-set resolver’s
comparableOperatorTypes would lift that interface for them. But a C# where clause is a claim about
the type ARGUMENT implementing a BCL interface, not about an operator being available, and nothing on
the Go side of the corpus implements it: a converted struct, array<T>, ж<T> and channel<T> all
compare through Equals/AreEqual. Such a lifted clause would be unsatisfiable by construction, and
the diagnostic would name the concrete struct rather than the constraint that could not admit it.
Two rules, both in constraintOperations.go. The composite kinds are not in comparableOperatorTypes,
so the rule is stated once where the operator sets are defined instead of per constraint shape (the
array-core branch’s suppressLiftedConstraints applies the same rule to one shape; a union of named
array types takes no such branch). With no operator lift and no interface to name,
getGenericDefinition’s generic tail would emit the Go union text VERBATIM as a C# constraint list —
where T : runtime.StackRecord | runtime.MemProfileRecord | …, error CS1003, a syntax error rather
than a type error. So constraintTypeSetIsInexpressible closes it, asked LAST after every shape with a
real emission has been tried: a non-empty type set whose
operator set is empty emits the union as a breadcrumb comment plus the one constraint C# can still
express —
internal static void testProfileRecordNullPadding<T>(ж<testing.T> t, @string name, Func<slice<T>, (nint, bool)> fn)
where T : /* runtime.StackRecord | runtime.MemProfileRecord | runtime.BlockProfileRecord */ new()
— the same answer, for the same reason, the built-in comparable arm
reaches: Go’s own checker validated every instantiation before conversion, so the C# clause has nothing
left to enforce. new() is kept (unlike that arm) because a composite type set admits no pointer type
argument. A composite type set is a subset of no other operator set, so a lift for it would be the
comparable operators ALONE, with no arithmetic siblings. (Guarded by
Constraints — a struct-only recordA | recordB | recordC union through a generic function instantiated
at each term; without the rule it reproduces CS0315 at all three sites.)
The breadcrumb is spelled as Go writes the constraint: a foreign term by its package NAME
(runtime.StackRecord) and a term of the constraint’s own package bare, never by import path
(crypto/x509.parsedURI). A NAMED methodless union carries its union beside its name, so the reader
sees the type set at the declaration and the elided-constraint ratchet (repoguard) can tell it from a
lost constraint:
internal static T firstOf<T>(slice<T> p)
where T : /* RecordUnion = recordA | recordB | recordC */ new()
(constraintUnionText, guarded by TestNamedUnionConstraintBreadcrumbCarriesItsUnion and the
Constraints golden.)
A type set holding bool lifts no equality constraint
The same rule covers bool. System.Boolean implements no IEqualityOperators<T, T, bool>, and neither does the
struct the converter emits for a defined bool type, so a type set holding bool lifted a clause that no bool argument
satisfies: error CS0315 at every call, as at otel’s attribute/internal sliceElem
(bool | int64 | float64 | string). Bool is therefore not in comparableOperatorTypes either. Such a set lifts no
operator interface, and equality on the parameter routes through AreEqual, which it already did:
public static bool Eq<T>(T a, T b)
where T : /* sliceElem = bool | int64 | float64 | string */ new()
{ return AreEqual(a, b); }
A set without bool keeps every operator it had (int64 | float64 still lifts the arithmetic, equality and ordering
interfaces). !, &&, ||, conditions, constants and conversions on a ~bool parameter do not convert (Known issues).
(Guarded by UnionBoolConstraint: otel’s sliceElem and SliceValue, == and != over unions holding bool,
~bool alone and a defined bool type, values vs Go.)
A single-term pointer constraint [P *T] erases the parameter to ж<T>
A type parameter constrained to a single, non-tilde pointer term — go/types’ flat-copy helper
func clone[P *T, T any](p P) P { c := *p; return &c } (predicates.go) — cannot be modeled as a C#
type parameter: no C# constraint fixes a parameter to a specific constructed type, and ж<T>
implements no interface through which *p/&c could be expressed generically. The operator-lift
fallback would emit where P : /* *T */ IEqualityOperators<P, P, bool>, new() with the deref dropped
(c = p — CS0029 P→T) and the box mismatched (return Ꮡc — CS0029 ж
The Go spec makes the faithful lowering an identity, not an approximation: a non-tilde term’s type
set is a singleton, so P’s only permissible type argument is *T itself. The converter therefore
erases such a parameter (pointerCoreConstraint in constraintOperations.go): it leaves the
emitted <…> list and where clauses (a breadcrumb comment preserves the Go constraint), renders as
ж<T> everywhere it appears (a getAliasQualifiedTypeName arm beside the *types.Pointer arm), and the parameter
classification treats a p P exactly like a p *T (paramPointerType — deref alias, Ꮡ box naming),
so the entire existing pointer machinery applies unchanged:
internal static ж<T> clone<T>(ж<T> Ꮡp)
/* where P : *T (erased: P renders as ж<T>) */
{
ref var p = ref Ꮡp.DerefOrNull();
ref var c = ref heap<T>(p.ΔCopy(), out var Ꮡc);
return Ꮡc;
}
Call sites drop the erased position from any synthesized explicit type-argument list
(renderedTypeArgs, applied at convCallExpr’s two synthesis blocks and convSelectorExpr’s
method-group form): clone(asig) emits clone<ΔSignature>(asig), and a callee whose remaining
parameters make C# inference sufficient stays bare (setThrough(Ꮡn, 55)). An EXPLICITLY written Go
instantiation equally drops erased positions — full (setThrough[*int, int](…) →
setThrough<nint>(…)), partial (clone[*thing](…) → bare clone(…), the rest inferring), and the
function-VALUE form (fv := clone[*thing, thing] → var fv = clone<thing>;) — via
explicitTypeArgsAfterErasure in convIndexExpr/convIndexListExpr. A C# consumer calls the emitted
method naturally — T sits in a real parameter position, so inference works without spelling the
phantom P.
The pointer classification flips at every use shape, not just the deref/address pair: returning the
parameter WHOLE yields its box (return a → return Ꮡa;), passing it onward to another erased
callee — including self-recursion — supplies the box (cloneChain<T>(clone<T>(Ꮡp), …); the
interface-shaped argument arm carves out erased params exactly like instantiated pointers), copying
it into a local is a Go pointer copy (q := p → var q = Ꮡp;, writes through q land in the
caller’s referent), and a nil comparison takes the box form over the nil-deferring entry alias
(if p == nil → ref var p = ref Ꮡp.DerefOrNull(); … if (Ꮡp == nil) — a nil argument reaches the
guard instead of throwing at entry, e.g. orZero[*int, int](nil)). The NAMED constraint-interface
spellings — [P PtrOf[T]] and the embedded [P interface{ PtrOf[T] }], where
type PtrOf[T any] interface{ *T } — resolve to the identical singleton type set and erase
identically. The constraint interface’s own DECLARATION follows the existing constraint-interface
convention (partial interface PtrOf<T> { /* Type constraints: *T */ }): a pointer term is
a type-set term, not an embeddable interface (an interface inheriting ж<T> is CS0527), and a GENERIC
constraint interface carries its own <T> list, so the
arity-0 <ΔT> marker list and its generated operator machinery are both suppressed for it.
An erased parameter crossing into an interface is the pointer. Go’s interface holds the *T, so
at a REAL empty-interface boundary a P value takes the *T treatment: the box crosses under the
pointer’s typed-nil mark (p.OrTypedNil()), not the type-parameter arm (OrTypedNilOf), and with no
use of the value alias left the parameter takes its Go name. That holds for an argument (variadic
...any and append([]any{…}, p) included), a return, an assignment or var, a conversion any(p),
a comparison with an interface, a slice element, a struct field, a map key or value, and a channel
send. A TYPE-PARAMETER slot is not an interface, though go/types answers IsInterface for it: an
assignment to a P variable, append into a []P and an argument to func f[U any](u U) take
the pointer itself, the box with no mark, as a *T value does there. A LOCAL of type P (q := p)
holds the box already, so it renders as itself, never Ꮡq:
func anyType[T any, P interface{ *T }](p P) string { return typeOf(p) }
internal static @string anyType<T>(ж<T> p)
/* where P : *T (erased: P renders as ж<T>) */
{ return typeOf(p.OrTypedNil()); }
internal static @string viaLocalCopy<T>(ж<T> p)
/* where P : *T (erased: P renders as ж<T>) */
{
var q = p;
return typeOf(q.OrTypedNil()) + " "u8 + typeOf(idP<T>(q).OrTypedNil());
}
Why. The boundary sites asked whether the value’s Go type was a *types.Pointer, and an erased
P is a type parameter, so it took the type-parameter arm over the deref-aliased VALUE: the interface
held the struct (%T read main.ev where Go reads *main.ev, an assertion back to the pointer
failed, fmt.Sprint printed {1} where Go calls the pointer’s String), and the alias the render
forced dereferenced a nil P at entry. Each site now asks paramPointerType, which already answered
for the deref alias and box naming. A plain *T parameter converted, any(p), had the same fault
in loud form: its operand rendered the value alias under the pointer mark (CS0411); the conversion
now renders the box as an interface argument does. A method-carrying pointer constraint
(interface{ *T; fmt.Stringer }) is not erased (GO2CS2005), so no non-empty interface is reached.
(Guarded by PointerCoreConstraints, every site above and a nil P, and by
TestErasedPointerCoreIntoInterfaceIsTheBox.)
Erasure is deliberately gated to the identity case: function type parameters whose constraint
type-set is a single non-tilde pointer term. Declined shapes warn instead of silently mis-emitting —
an approximate ~*T admits named pointer types, which emit as /*ж<E>*/ wrapper classes
(not identity with ж<E>); pointer unions have no single identity; and erasing a generic named
type’s parameter would change its emitted arity at every use. None occurred in the standard library
at the census, where go/types’ clone was the only compiled occurrence of the pattern. (Guarded by
PointerCoreConstraints — clone/read/write/round-trip through [P *T] and the swapped-order
[T any, P *T], flat-copy independence verified, values vs Go.)
The pointer-method idiom [T any, PT interface{ *T; M() }] erases PT and calls its methods through a shape witness
Go’s idiom for “construct a T, then call a pointer-receiver method on it” pairs a type parameter with a second one
constrained to *T plus methods. Both spellings are common, the inline one and a named constraint:
type Setter[T any] interface {
*T
Set(string)
}
func New[T any, PT Setter[T]](s string) T {
var v T
PT(&v).Set(s)
return v
}
The *T term is a single non-tilde term, so PT’s only type argument is *T itself, exactly as for
[P *T]: PT is erased and renders as ж<T>.
The methods are what [P *T] does not have. C# checks a constraint nominally and ж<T> implements no interface of
the user’s, so the methods cannot be reached through a where clause on the erased parameter. The converter puts a
shape witness in PT’s place instead: a type parameter PTΔSetter : struct, SetterΔshape<ж<T>, T>, whose static
methods take the receiver as their first argument. PT(&v) converts a *T to PT, which is the identity once PT
is ж<T>, so it emits as the operand alone:
public static T New<T, PTΔSetter>(@string s)
/* where PT : Setter[T] (erased: PT renders as ж<T>) */
where PTΔSetter : struct, SetterΔshape<ж<T>, T>
{
ref var v = ref heap(builtin.GoZero<T>(), out var Ꮡv);
PTΔSetter.Set(Ꮡv, s);
return v.ΔCopy();
}
A call site passes the witness for the pointer the constraint names. One witness per pointer type serves every
instantiation, because the constraint’s own *T term is the argument, so its type arguments follow from the pointer
(New[Ref] emits New<Ref, RefжSetterΔshape>("1234567")). The witness structs and the witness interface are
emitted in package_info.cs:
public interface SetterΔshape<TΔself, T> { static abstract void Set(TΔself self, @string p0); }
…
public readonly partial struct RefжSetterΔshape : SetterΔshape<ж<Ref>, Ref> { public static void Set(ж<Ref> self, @string p0) => self.Set(p0); }
The inline spelling lifts its constraint to a named interface, <function>_<parameter> (FromStrings_PT, recorded
by [assembly: GoInlineConstraintLift]), and takes a witness over it in the same way. The same lowering serves a
value-receiver method beside a pointer one (*T’s method set holds both), two or more methods, a constraint that
embeds another such constraint, a generic type Box[T any, PT Setter[T]] (its witness parameter is a type
parameter of the struct), a generic function passed as a value (apply(New[Ref], "abc")), a PT parameter tested
against nil, and a deferred call or a goroutine started through PT, whose witness call is wrapped in a lambda.
A PT value is the box, so every place that wants a pointer takes the identifier itself: a *T parameter converted
to PT (PT(v) emits v, and the parameter needs no deref alias), a PT local passed to another generic, compared
(AreEqual(q, r)), boxed into an interface or asserted back (x._<ж<T>>()), and a PT parameter passed on with
explicit type arguments (rec[T, PT](n-1, p)), from a closure too. The same holds for any erased parameter, such as
[P *T]. The pointer term may be spelled through a generic alias (type Ptr[T any] = *T, interface{ Ptr[T]; M() }
or [P Ptr[T]]): it is the pointer, and converts exactly as *T does.
Four uses need a witness go2cs cannot place yet, and each is refused by name with GO2CS2013: a method value
taken through PT (f := p.Set), a PT converted to a non-empty interface, a pointer type argument that is not
to a non-generic named type of the package (New[Pair[int]], whose pointer is *Pair[int]), and PT passed on to
a generic function whose constraint is neither PT’s own constraint nor one that constraint embeds.
A function its own package never instantiates with a pointer (inspector.All in golang.org/x/tools, instantiated
only by its importers) has no witness, while PT is still erased to ж<T>, the box of a type parameter, which
implements no interface in C#. A PT converted to a non-empty interface there converts at run time, as
((any)(p).OrTypedNilOf())?._<Positioner>(): the box’s dynamic type is the caller’s ж<Item>, whose methods the
assertion binds from its own assembly, and a nil PT is Go’s non-nil interface over a nil pointer. A method called or
taken as a value through PT there (n.Pos()) is refused: it is read off the dereferenced T, which has none of the
constraint’s methods in C#. Converting PT to the interface and calling the method on the interface value is the
form that converts. Instantiating the function in its own package with a pointer to one of the package’s non-generic
named types gives it a witness for calls from that package; a call from another package is refused until a witness
can cross the package boundary. A deferred call
through PT evaluates its receiver when the deferred call runs, not at the defer statement; the receiver is the
ж<T> the body built, so the two agree unless the body reassigns that variable after the defer.
Guarded by the PointerMethodConstraint behavioral test (both spellings and every shape above, values against Go),
the UnwitnessedPointerMethodNearMiss behavioral test (the shapes beside the unwitnessed refusals that do convert,
the interface conversions among them, from the declaring package and from an importing one, values against Go), and by
TestPointerMethodCoreRecognizesTheIdiom,
TestPointerMethodIdiomIsSelectedAndRefusedOnlyWhereStageOneStops (the four refusals, by their reasons),
TestUnwitnessedPointerMethodConvertsToInterface (the conversion’s emission, and the method call and the method value
refused, where no witness is emitted) and
TestPointerMethodIdiomHelperShapesEmitTheBox (each helper shape’s emitted box) in
pointerMethodConstraint_test.go.
A generic type’s pointer-core parameter that no witness carries is cast through object
A generic NAMED type erases its pointer-core parameter only where a shape witness is placed, and stage 1 places one
only where the type’s own package instantiates it at a pointer. zap’s objectValues[T any, P ObjectMarshalerPtr[T]]
is the common shape that falls outside: the concrete pointer arrives from importers, through an exported constructor
that returns an interface. Its P stays a C# type parameter, and its where clause is dropped with the pointer term,
so in C# P reaches nothing. In Go its type set is exactly {*T}, so in every instantiation P is ж<T>, and the
body’s two value uses convert on that fact:
func (os objectValues[T, P]) MarshalArray(enc *Encoder) error {
for i := range os {
var p P = &os[i]
if err := enc.AppendObject(p); err != nil {
return err
}
}
return nil
}
internal static error MarshalArray⁔<T, P>(/*this*/ objectValues<T, P> os, ж<Encoder> enc) {
foreach (var i in range(len(os))) {
P p = (P)(object)(Ꮡ(os, i));
var err = enc.AppendObject(((any)(p).OrTypedNilOf())?._<Marshaler>()); if (err != nil) {
return err;
}
}
return default;
}
- A
*Tbound forP, whether declared, assigned, returned, stored in a field or converted withP(&x), is cast throughobject. The cast cannot fail and changes no reference. It is applied once wherever two routes meet the same value. A methodless[P *T]on a generic type takes the same cast. - A
Pbound for a non-empty interface takes the run-time conversion an assertion uses. It readsж<T>’s method set when it runs, so the importer’s pointer-receiver methods are found there. Go’s conversion cannot fail, since the constraint names the interface. - The zero
Pis a nil*T.OrTypedNilOfgives it the canonical typed nil, so as the interface it is Go’s non-nil interface over a nil pointer, and its method runs with a nil receiver.
The run-time conversion binds through the same shells a type assertion builds, and it holds under Native AOT: the row publishes AOT and prints Go’s output.
A method CALL through such a P still needs a witness and is not converted. An importer can instantiate the type at
a pointer when the constraint is spelled inline, as the row does. Through a named constraint, another package’s pointer
instantiation is GO2CS2005’s until stage 2 of the witness. zap’s own package converts either way, since nothing in it
instantiates at a pointer.
Guarded by the PointerCoreTypeDynamic behavioral test (two packages: element pointers that alias the importer’s
slice, the interface argument, a P return, P(&x), the typed-nil interface, any, and a [P *T] holder; values
against Go) and by TestUnerasedPointerCoreValuesConvert in pointerCoreTypeDynamic_test.go (zap’s named spelling,
each value use’s emission, and no double cast).
An integer named-numeric wrapper implements the integer operator interfaces
A [GoType num:] wrapper (type stringID uint64) declares the common numeric operator
interfaces so it can serve a cmp.Ordered-shaped constraint (IAddition/ISubtraction/
IMultiply/IDivision/IEquality/IComparison/IIncrement/IDecrementOperators), and for an
integer underlying also the integer-only three — IModulusOperators, IBitwiseOperators,
IShiftOperators<T, int, T> — whose operators (%, &|^~, <<, >>) are kind-gated. Without
them a named integer type could not satisfy a converter-emitted ~integer operator constraint:
internal/trace’s type dataTable[EI ~uint64, E any] instantiated with type stringID uint64 is
CS0315 on exactly those three interfaces. InheritedTypeTemplate declares the three integer
interfaces for an integer underlying (float/complex keep only the common set), on the same kind-gate
NumericTypeTemplate uses for the operators. IShiftOperators additionally requires
operator >>> (unsigned right shift), which the integer operator block carries; Go emits no >>>, but
the member is needed to satisfy the interface. Guarded by NamedNumericOperatorConstraint (a generic
mix[K ~uint64 | ~int32] applying modulus/bitwise/both-shifts on the type parameter, instantiated
with a named uint64 and a named int32, values vs Go).
A named-numeric wrapper is IComparable<T> as well as ordered by operators
Ordering has two surfaces in .NET and the wrapper carries both. IComparisonOperators<T,T,bool>
(above) serves a constraint lifted from cmp.Ordered; IComparable<T> is what the BCL’s own
ordering binds — Array/List.Sort, SortedSet<T>, Comparer<T>.Default — and, decisively for
converted code, what golib’s N-argument min/max are constrained on. (The two-argument forms take
IComparisonOperators, because a type parameter constrained by cmp.Ordered has no
IComparable<T> conversion; the params ReadOnlySpan<T> forms cannot, since a span element must
compare through a member, not an operator.) Without IComparable<T> a named numeric would bind
min(a, b) and fail min(a, b, c, d): min(a-got, got-a, a-got+q, got-a+q) over
crypto/internal/fips140/mlkem’s type fieldElement uint16 (its field_test.go) would be CS0315, “no
boxing conversion from fieldElement to System.IComparable<fieldElement>”. InheritedTypeTemplate
declares IComparable<T> on the
same kind-gate as IComparisonOperators — every numeric kind except complex, which Go orders no
more than C# does — and NumericTypeTemplate emits its single member inside the same gated block:
public int CompareTo(fieldElement other) => m_value.CompareTo(other.m_value);
Forwarding to the underlying value’s CompareTo, rather than writing the comparison out of the
wrapper’s own </>, is what keeps a named float on the BCL total order (NaN below everything) —
which is what makes min yield NaN when any argument is NaN, as Go’s does. Every underlying a
[GoType num:] wrapper can name satisfies it: the aliases are BCL primitives, uintptr is a golib
struct that declares IComparable<uintptr> itself, and a wrapper over another wrapper picks up the
member this template gives it. The wrapper is IEquatable<T> and ordered,
matching the golib uintptr and @string structs, which are both. (Guarded by the
MinMaxBuiltin behavioral test — min/max at two and four arguments over named unsigned,
floating and signed underlyings, values vs Go; without IComparable<T> it is CS0315 across the three
kinds.)
An untyped constant argument to min/max takes the call’s type
Go converts an untyped constant argument of min/max to the call’s type: the type of its typed
arguments, or the constant’s default type when every argument is constant. golib’s min/max are
generic, so C# infers T from the arguments as emitted, and a bare integer literal is a C# int. The
converter therefore casts each untyped-constant argument to the call’s type wherever the bare form
would infer a different T:
x := max(u8, 1)
x += 100
var x = max(u8, (uint8)1);
x += 100;
Left bare, max(u8, 1) infers T = int: %T prints int32, the value boxes as the wrong type in an
interface, and x += 100 gives 300 where Go’s uint8 wraps to 44. With uint32, uint64, uint or
uintptr there is no implicit conversion from int at all, so the bare call does not compile, and
min(s, "a") over strings binds neither @string nor the literal’s UTF-8 span. The cast is emitted
for every sized or unsigned integer type, uintptr, string ((@string)("a"u8)), every named type
((fieldElement)1), and an all-constant int call, whose literals would infer int32 where Go’s
default type is int: max((nint)1, (nint)2).
The bare form is kept wherever it already infers the call’s type, so that output reads as the Go does:
the float types (the literal carries the context’s F or D suffix), int32 and rune (a C#
integer literal is an int), and int or int64 beside a typed argument when every constant fits in
an int, which then widens to nint or long. The same arm casts a NAMED untyped constant argument,
which renders as its UntypedInt static (see
Named Numeric Types and Constant Contexts).
Guarded by: MinMaxBuiltin (each shape prints %T and %v against Go: int8 through uint64,
uintptr, int and uint, float32 and float64, a mixed float/int constant, strings, the three named
kinds, all-constant calls, and a later wrap in uint8).
Lifted shift constraint uses the BCL shape IShiftOperators<T, int, T>
The lifted Integer operator set constrains shifts as IShiftOperators<T, int, T> — the shift count is int, not the type parameter. Every BCL binary integer implements exactly that shape (IShiftOperators<TSelf, int, TSelf>); only C# int itself happens to also satisfy the self-typed form, so a self-typed constraint would make every non-int instantiation fail (CS0315 — strconv’s bsearch[S ~[]E, E ~uint16 | ~uint32] on ushort/uint). The shape is also exactly what emitted bodies need: the converter coerces every shift count to int (x << (int)(k)), so a generic body can only ever perform T << int. The generated named-constraint interface template (Integer in go2cs-gen) and its dynamic-conversion placeholder shift operators use the same int-count shape, keeping the two emitters consistent. (Guarded by the GenericTypeInference extensions bsearchLike/halve — ~uint16 | ~uint32 instantiations with a shift on the type parameter, values vs Go.)
Builtins over constrained slice type parameters
golib’s builtins carry interface-typed overloads so a value held as a constrained type parameter (S ~[]E, boxed to its ISlice<E> constraint) binds directly: copy(ISlice<T1> dst, ISlice<T2> src) (plus an ISlice<byte>/@string form), clear(ISlice<T> s), and two-argument min/max constrained on IComparisonOperators (Go’s cmp.Ordered lifts to operator interfaces; a constrained E has no IComparable<E> conversion). All four min/max overloads PROPAGATE NaN, which is Go’s own spec rule — if any argument is a NaN the result is a NaN — and which neither natural C# spelling gives for free: the operator form x < y ? x : y answers the NON-NaN side whenever the NaN sits on the left, because every C# comparison involving a NaN is false; and the params form’s IComparable<T> total order sorts NaN BELOW everything, which happens to be Go’s answer for min and is the OPPOSITE of Go’s answer for max. Both test explicitly, through one shared per-T fact (builtin.OrderedFacts<T>) that classifies the floating kinds — the two BCL primitives, and the generated single-field /*num:floatNN*/ wrapper a NAMED Go float becomes, recognized by walking that one field so a wrapper-over-a-wrapper resolves for free. The gate is a static readonly per closed T, so it folds at JIT time and no integer instantiation pays for it, and the fact carries a same-width reinterpret rather than an operator because CompareTo/Equals cannot see a NaN at all (double.NaN.CompareTo(double.NaN) is 0 and double.NaN.Equals(double.NaN) is true, both by BCL design). Measured by slices’ TestMinMaxNaNs, which replaces each element of a float64 slice with NaN in turn and requires slices.Min AND slices.Max to propagate it; guarded by GolibTests.OrderedMinMaxNaNTests, which holds all four overloads including a named-float wrapper. The box wraps the same backing array, so interface writes land in the caller’s storage — copy/clear into an S are true write-throughs (span windows, memmove semantics for overlap). Overload resolution keeps concrete calls on the exact slice<T> overloads (an exact parameter beats a boxing conversion), so nothing outside generic bodies changes. (Guarded by the GenericTypeInference extension CopyClearMinMax — copy into and clear through constrained values, write-through verified by value vs Go.)
S-preserving sub-slice and append. Go’s sub-slice of a named slice type yields the same named type sharing the same backing — pdqsort’s recursion depends on it (pdqsort(s[:mid]) with s S). The ISliceWrap<TSelf, T> static-abstract factory (TSelf Wrap(in slice<T> source)) supplies the non-copying reconstruction: slice<T> implements it as identity, every generated named-slice wrapper wraps the window in its own type, and the ~[]E where-clause carries it (ISlice<E>, ISupportMake<S>, ISliceWrap<S, E>). A sub-slice of a constrained type parameter emits golib’s subslice<S, E>(s, lo, hi) (type arguments explicit — E is constraint-only) which routes S.Wrap(window.Reslice(…)); the new slice<T>(ISlice<T> view) constructor SHARES storage (unboxes a slice<T>, reconstructs any other implementer from its source array and window). append on a constrained value binds golib’s append<S, T>(S, params ReadOnlySpan<T>) (S from the first argument, T from the span — fully inferrable) and wraps the result back to S; its body routes to the core slice<T>.Append directly, since a recursive append(…) call would resolve back to itself (slice<T> satisfies the constraints). The named-slice WRAPPER template’s sub-slice members likewise route through the wrapped m_value (sharing); routing them through ToSpan() would hand out detached copies, a silent write-through divergence for named slice types generally. (Guarded by the GenericTypeInference extensions SumHalves — recursion over sub-slices of S with a write through the deepest view, verified against the caller’s array — and AppendKeep.)
No bound of a constrained sub-slice is a SENTINEL. An omitted LOW is emitted as the 0 it means (Go’s s[:h] is s[0:h], so no overload is needed), and an omitted HIGH selects a two-argument subslice<S, E>(s, low) overload. With -1 standing for “omitted”, s[i:] and s[i:-1] would be the identical call at the boundary, so no golib logic could separate them, and clamping a negative low to 0 would make slices.Insert(s, -1, …) and slices.Replace(s, -1, 2, …) — whose bodies OPEN with _ = s[i:] and _ = s[i:j] as their bounds check — silently succeed where Go panics. subslice3 needs no companion — Go’s grammar requires the high bound in a full slice expression — and all three route slice<T>.Reslice directly rather than the slice() extension, whose own -1 defaulting convention would re-open the collision one layer down. subslice is emitted only for type-parameter receivers. Residual: the ORDINARY (non-type-parameter) path emits s[Low..], whose int→Index conversion throws ArgumentOutOfRangeException for a negative bound — a .NET exception, not a Go panic, so it is neither recover-able nor contained the way RuntimeErrorPanic.SliceBoundsOutOfRange is; and SliceExtensions.slice’s -1 default collides at exactly -1 for the three-index form. (Measured by slices’ TestInsertPanics and TestReplacePanics; guarded by GolibTests.ConstrainedSubsliceBoundsTests, which holds the negative, out-of-range, valid and backing-shared cases together.)
Every generated named-slice wrapper also implements the non-generic IArray surface explicitly. The public typed Source remains T[] for the concrete wrapper, but the interface member is emitted as Array IArray.Source => ((IArray)m_value).Source!;, matching golib’s IArray.Source contract and keeping len(IArray), element-address helpers, and interface-typed builtins bound to the wrapper. Pointer elements use the same form, e.g. type queue []*item emits ISlice<ж<item>> plus the explicit Array IArray.Source member. (Guarded by NamedSlicePointerElements.)
S where []E is expected. Go assignability lets a named-slice-typed value pass where the unnamed []E is expected (rotateRight(s[m:i], …), pdqsortOrdered(x, …)); the converter materializes such an argument through the SHARING slice<T>(ISlice<T>) constructor — pdqsortOrdered(new slice<E>(x), …) — a cast cannot apply (interface-constrained source; C# forbids user conversions from interfaces). The constructor unboxes a boxed slice<T> directly and otherwise takes the implementer’s full-window interface sub-slice, which every golib implementer returns as a boxed shared slice<T> — NOT Source, which materializes a detached copy (caught by the write-through gate: the helper’s write must land in the caller’s array). The 3-index form on a constrained value emits subslice3<S, E>, and a constrained spread (append(s, v.ꓸꓸꓸ) — a Span<E>) binds an exact params Span<T> twin of the constrained append (betterness otherwise picked the legacy params T[] candidate with T = Span<E>, a ref struct as type argument — CS9244). (Guarded by the GenericTypeInference extension PassSlice — S passed to a concrete []E helper, write-through verified by value vs Go — and by the ConstrainedSliceParamInPlace behavioral test, which drives a full in-place mutation through the materialized slice<E> — an element reversal and a real insertion sort mirroring slices.Sort/SortStableFunc and internal/fmtsort’s make+append-built SortedMap — over plain, named, and []string sequences, asserting the caller observes the reordering. A detached copy would leave the caller’s slice untouched.)
Explicit []E(x) conversion of a ~[]E type parameter. The explicit twin of the assignability case above — Go that spells out the slice conversion (reverse([]E(x)), x of type S ~[]E) rather than relying on assignability — takes a different converter path. isTypeConversion’s *ast.ArrayType arm rejects it (a type parameter’s Underlying() is its constraint interface, not []E, so the identical-underlyings gate fails), and the general call assembly would render slice<E>(x) — the golib array-only builtin slice<T>(T[]), which the ISlice<E>-typed source S cannot satisfy (CS1503). The converter therefore intercepts this shape in the general call path (mirroring the sibling string|[]byte-union []byte(x) special case at convCallExpr.go): when the conversion target is a slice-type literal and the sole argument is a *types.TypeParam with a ~[]E slice core (typeParamSliceCore — the same recognizer the implicit path uses), it emits the SHARING new slice<E>(x) constructor, so explicit and implicit ~[]E→[]E conversions land identically on the sharing ctor and preserve Go’s slice-conversion aliasing. Genuine conversions are untouched: named-slice casts ([]CaseRange(special)) take the isTypeConversion cast path; string/nil sources are not type parameters; the string|[]byte union is handled by the block just above (typeParamSliceCore is nil for it). (Guarded by the ConstrainedSliceParamInPlace behavioral test’s explicitReverseSeq case — reverse([]E(x)) over plain and named ~[]E sources.)
An untyped-int literal in a ~[]E-locked type-parameter slot is cast to the resolved element type. A bare untyped-integer literal passed where the parameter is the element type parameter E of a sibling ~[]E-constrained type parameter — Index[S ~[]E, E comparable](s S, v E) called Index(s, 2), or the variadic element of Insert[S ~[]E, E any](s S, i int, v ...E) called Insert(b, len(b)-1, 0) (the slices shape) — drives C# generic inference from the literal’s own C# type. Go infers E from S’s core type ([]int → E = Go int → nint), but C# has no analogue for ~[]E core-type inference: the emitted where S : ISlice<E> does not flow S’s concrete element to E, so C# infers E SOLELY from the value literal. A bare C# int literal is System.Int32, so Index(s, 2) with s []int would make C# infer E=int, and slice<nint> would then fail the ~[]int constraint — CS0315 (no boxing conversion slice<nint>→ISlice<int>), CS0411 (inference failed), or CS1503 (arg conversion). go/types has already resolved the literal to E’s instantiation (Info.Types[lit].Type — int for the []int caller, byte for a []byte caller, int64→long, uint→nuint, …), so convCallExpr emits the literal AT that C# type via the shared castArgToType plumbing: Index(s, (nint)2), Insert(b, len(b) - 1, (byte)0). The sibling-lock gate (typeParamIsSliceElementOfSibling) is what keeps the footprint minimal and correct: the cast fires ONLY when the parameter’s type parameter is the slice-element of another type parameter’s ~[]E constraint — the one shape C# cannot infer. Everything C# already infers correctly is left with its bare literal: a freely-inferred type parameter (First[T any](v ...T) — T=int32 satisfies any, and the value is identical), one determined directly by another argument (setThrough[P *T, T any](p P, v T) — C# infers T from the pointer), and an explicitly-instantiated call (NewOption[nint](42) — the type argument is already pinned). A resolved int32/rune kind is skipped even inside the gate (a bare int literal already IS System.Int32 — the []int32 element case stays a plain literal), and a value convBasicLit already casts ((nint)…L for an out-of-int32 constant) is not double-wrapped (the wholeExprIsCastOfType skip in convExprList). The literal-constant test reuses isUntypedNumericConstArg, the same recognizer the append-element and narrow-int casts key off, so a tightened local const — already declared at its concrete type — is excluded. The whole slices-package Index/Insert/Replace/Contains-family value-argument seam depends on it. (Guarded by the GenericUntypedIntArg behavioral test — Index/appendAll over []int, a named numbers []int, []byte, and []int32 element types with bare int-literal args; the []int32 case proves the no-cast arm.)
Range-over-func on named/generic Seq types, in the foreach form. Most range-over-func loops convert to a call of the iterator (A range-over-func loop converts to a call of its iterator); this rule is the emitted form of a loop that rule keeps. Such a for v := range seq (and the two-value for k, v := range seq2) on an iter.Seq[E]-shaped value emits through golib’s yield-adapting range() overloads. Three pieces make the named/generic form work: detection unwraps the type’s Underlying() (a defined or instantiated func type is a Named, not a bare Signature); a NAMED func type renders as a C# delegate, which has no conversion to the overloads’ Action<Func<…>> parameter — its method GROUP does, so the emission appends .Invoke; and because C# cannot infer a type parameter from a method group’s parameters, the element types are spelled out from the yield signature: foreach (var v in range<nint>(count(3).Invoke)). break inside the body ends the foreach, which cancels the adapter’s producer — the yield function receives false, matching Go’s semantics; a two-value range<K, V> overload adapts pair-yields onto the tuple machinery. An adjacent gate: a call’s result being a generic instantiation adds explicit type arguments only for conversions and GENERIC callees (NewOption<nint>(42) — an untyped-const arg would infer C# int where Go infers nint), never for a plain function returning a generic named type (countdown<nint>(5) is CS0308). (Guarded by RangeFuncLoweredCaptures — keptDefer and keptPark range a named Seq[V] in the foreach form, values vs Go; the GenericTypeInference extensions — a generic Seq[V] ranged with break and a two-value KVSeq[K, V] — guard the same named types in the call form.)
An EXPLICITLY-instantiated generic function through a package selector renders its type arguments once. Go’s pkg.Func[T](…) is an IndexExpr (or IndexListExpr for pkg.Func[K, V]) whose base X is the selector pkg.Func. convIndexExpr/convIndexListExpr renders the [T] as <T> itself. But the base is also a generic-function value, and convSelectorExpr spells a generic function’s inferred type arguments when it appears as a method-group value (the slices.SortFunc(all, slices.Compare) path, needed because C# can’t infer a method group’s type parameters), so appending there too would produce pkg.Func<T><T>() — CS1525 (reflect.TypeFor[X]() → invalid expression term), CS0119 (a plain-return generic like saferio.SliceCap[T]), or CS8124 (<T>() parsed as a one-element tuple), across encoding/gob, xml, asn1, json, text/template, database/sql/driver, debug/macho·pe·elf, and unique. The index expression therefore converts its base with a suppressGenericTypeArgs context flag, so convSelectorExpr skips the value-path append when it is the base of an explicit instantiation (the standalone method-group-value case carries no flag and appends). A local generic function (Func[T](), base is an Ident not a selector) never hit this, since only convSelectorExpr appends. (Guarded by the CrossPkgUser extension — CrossPkgLib.Wrap[int](5) (IndexExpr) and CrossPkgLib.Pair[string, int](…) (IndexListExpr), both rendering single type-argument lists, output vs Go.)
A ~chan E type parameter takes the IChannel<E> surface
A type parameter whose type set is a channel ([C ~chan E, E any], or a named constraint over one such as
interface{ ~chan E }) is constrained to IChannel<E>, ISupportMake<C>, as the plain chan E constraint is. Its
operations cannot use the forms a concrete channel takes, which need a channel<T> (or a named-channel wrapper) and do
not bind a type parameter, so each goes through the interface: a send is c.Send(v), a receive c.Receive() and the
comma-ok receive c.Receive(ꟷ), close(c) is c.Close(), and range c names the element type
(foreach (E v in c)), since IChannel enumerates as the non-generic IEnumerable. len, cap and make(C, n) bind
golib’s IChannel and ISupportMake overloads unchanged.
public static E Recv<C, E>(C c)
where C : /* ~chan E */ IChannel<E>, ISupportMake<C>, new()
{ return c.Receive(); }
public static (E, bool) RecvOk<C, E>(C c)
where C : /* ~chan E */ IChannel<E>, ISupportMake<C>, new()
{
var (v, ok) = c.Receive(ꟷ);
return (v, ok);
}
The typed receive and its comma-ok twin are default members of IChannel<T>. channel<T> and every generated
named-channel wrapper implement them with their own public Receive() and Receive(bool); the defaults are a fallback
through the untyped ChanRecv, and they are defaults rather than abstract members so the interface stays additive.
The channel core is found by typeParamChanCore, the twin of typeParamSliceCore and typeParamMapCore, reading the
type set through its named terms. (Guarded by ApproxChanConstraint: send, receive, comma-ok, len/cap, close
A comparison with nil takes golib’s one nilness rule, GoReflect.IsNilGoValue(c), as a pointer-term type parameter’s
does. A select over such a parameter and a conversion to the plain channel type do not convert (Known issues). through a named constraint and deferred and spawned, range, make(C, n) a fan-in and nil comparisons, over a defined channel type and chan string, values
vs Go.)
A type set is read through its named terms
Every question the converter asks of a type parameter’s set (is it integer, numeric with a float, string, string or []byte, a slice, a map) is answered from the set’s terms as Go forms them. A term that is itself a named constraint, as in golang.org/x/exp/constraints (type Integer interface{ Signed | Unsigned }, type Number interface{ Integer | Float }), or an embedded one (interface{ Ints }), gives its own terms; comparable and a method-only interface give none. So Number reads as a numeric set with a float, exactly as ~int | ~float64 does:
type Number interface{ Integer | Float }
func isZero[T Number](v T) bool { return v == 0 }
type Ints2 interface{ Ints }
func head[S Ints2](s S) int { return s[0] }
internal static bool isZero<T>(T v)
where T : /* Number */ IAdditionOperators<T, T, T>, ISubtractionOperators<T, T, T>, IMultiplyOperators<T, T, T>, IDivisionOperators<T, T, T>, IIncrementOperators<T>, IDecrementOperators<T>, IUnaryNegationOperators<T, T>, IEqualityOperators<T, T, bool>, IComparisonOperators<T, T, bool>, new()
{ return AreEqual(v, builtin.ConvertToType<T>(0)); }
…
internal static nint head<S>(S s)
where S : /* Ints2 */ ISlice<nint>, ISupportMake<S>, ISliceWrap<S, nint>, new()
{ return s[0]; }
Without it the comparison bound AreEqual(object, object), whose dynamic-type check answered false where Go answers true, and the set’s literals, conversions, slices and maps did not compile.
A conversion out of a set that admits a float, int(v) or U(v) between two numeric type parameters, depends on the source’s kind, which only the instantiation knows: a float truncates toward zero, while an integer must keep every bit a route through float64 would lose. Both, and a conversion to a NAMED numeric type such as time.Duration(v), go through golib’s builtin.ConvertNumeric<TTo, TFrom>(v), which reads the source by its kind once per type: the built-in kinds by type, and a defined numeric type by what its go2cs-gen wrapper’s own conversions do. A variable converted into such a set, T(n), takes ConvertToType<T> with the operand widened to long, ulong or double; a uintptr operand widens to ulong through its implicit nuint operator, as a narrow unsigned one does, since a plain cast of it to the type parameter does not compile (CS0030).
Guarded by NestedConstraint (x/exp’s Integer, Ordered and Float and a user Number, at int, int8, uint64, float32, float64, a defined int and a defined float; slice and map sets and a string-or-bytes set through named terms; int(v), int8(v) and U(v) across the instantiations, including an int64 beyond 2^53; time.Duration(v), Count(v) and Celsius(v); output-compared against go run) and by TypeParamNumericConversion (an int, a named float, a named integer, a uintptr, a float32 and a uint64 converted into a float-admitting set; U(x) between two such parameters at every pairing of sign, width and kind; conversions out to int, uint8, float32 and float64; output-compared against go run).
Integer type-parameter conversions route through golib (the E(100) family)
C# has no cast to or from a type parameter, so the Go conversions in rand.N[Int intType] — Int(x), uint64(n) — and an untyped constant compared against the parameter (n <= 0, which Go types AS Int but C# leaves as int, unacceptable to the lifted IComparisonOperators<Int, Int, bool>) have no direct C# form (CS0030/CS0019). Three coordinated pieces, gated on a constraint whose every type-set term has an integer underlying (typeParamIsInteger): a conversion to the parameter emits golib’s runtime-typed ConvertToType<Int>(…) (typeof-dispatch that JIT-folds to a single branch per instantiation; signed kinds sign-extend, unsigned zero-extend — Go’s exact conversion semantics; a /*num:**/ wrapper instantiation goes through the IGoNumericWrapper<T> members go2cs-gen gives it, below); a conversion from the parameter to a basic integer emits ConvertToUInt64<Int>(n) (plus a plain numeric cast when the target is not uint64); and a constant operand of a binary op against the parameter materializes via ConvertToType<Int>(0) — except a SHIFT count, which Go types independently and the emission already coerces to int. Result: if (n <= ConvertToType<Int>(0)) … return ConvertToType<Int>(ConvertToUInt64<Int>(n) / 2);. (Guarded by the GenericTypeInference extension halveN — ~int32 | ~int64 with the compare, both conversions, and a negative value proving sign-extension, values vs Go; math/rand/v2’s N takes this path.)
A float converted to an integer type parameter takes gc’s answer for the process’s architecture
T(value.(float64)) with T ~int32 emits the same
ConvertToType<T>(value._<float64>()), and golib’s double overload decides it. An in-range value truncates
toward zero, as Go’s float-to-integer conversion does. An out-of-range value or NaN is implementation-defined in
Go, and gc’s answer differs by GOARCH, so golib gives the answer for the architecture the process runs on:
- amd64 (and any architecture not measured): the 64-bit kinds read CVTTSD2SQ’s “integer indefinite”
(
long.MinValue), int32 reads CVTTSD2SL’s (int.MinValue), and the unsigned 64-bit kinds take gc’s split at 2^63. - arm64: FCVTZS and FCVTZU saturate and NaN reads 0. int64 and int32 clamp, and the unsigned 64-bit kinds clamp
to [0, MaxUint64], so
uint64(-1.9)is 0 on arm64 and MaxUint64 on amd64.
On both, the 8- and 16-bit kinds keep the low bits of the int32 result and uint32 the low bits of the int64 one,
because gc converts them through those widths. A C# cast would saturate the narrow kinds, which matches neither.
A float32 argument widens to double exactly and converts the same, and a named integer wrapper
(type count int32) converts to its underlying kind first. The integer overloads keep every integer argument,
because long and ulong are better conversion targets than double. Guarded by the TypeParamFloatConversion
extension (in-range values only, which agree on every architecture, over every integer kind and a named wrapper,
values vs Go) and GolibTests’ TypeParamFloatConversionTests: gc’s amd64 table (38 values over 11 kinds), both
architectures’ tables (15 out-of-range values each), the host’s own architecture through ConvertToType<T>, and the
binding and wrapper arms.
Any constant Go types as the parameter is materialized, once, at its top-most node
Go gives an untyped constant the type of the type parameter it meets, not only as a binary operand: return 1
in a function returning T, var x T = 1 << n, an argument pair(x, 2), an element of []T{1, 2}, and the
left operand of 1 << n, which takes the shift’s type. C# leaves each one an int literal (CS0029, CS0030 or
CS0019), so convExpr wraps every constant expression whose type is a numeric type parameter in
ConvertToType<T>(…), at the top-most constant node only: return 1 << 3 is ConvertToType<T>(1 << 3), never
a wrap inside a wrap. The shift case then binds golib’s generic guard, ConvertToType<T>(1).Lsh(n)
(.Lsh((int64)(n)) for a signed count), so a count at or past the type argument’s width yields 0 and a negative
count panics, as in Go; a bare 1 << (int)(n) on T would also have been masked by .NET’s IShiftOperators.
The binary-operand wrap above, the per-argument cast and the bitwise compound right-side cast step aside for an
operand that is already T. Guarded by TypeParamUntypedConst (fifteen arms over ~uint8 | ~uint32 | ~int8 |
~int64, values vs Go) and typeParamUntypedConst_test.go.
“Numeric” is typeParamIsRealNumeric: every term of the type set has an integer or float underlying. A set that
mixes the two, or holds only floats, takes the same wraps, because golib’s ConvertToType<T> has a long, a
ulong and a double overload, one for every constant such a set admits: v * 3 with T ~int32 | ~float64 is
v * builtin.ConvertToType<T>(3), v * 0.5 with T ~float32 | ~float64 is v * builtin.ConvertToType<T>(0.5D).
A complex term excludes the set, since ConvertToType has no complex overload. Equality takes the generic
AreEqual<T> once the constant is a T, so runtime’s fmin, whose x != 0 was !AreEqual(x, 0) (the object
overload, a boxed double against a boxed int), is now
!AreEqual(x, builtin.ConvertToType<F>(0)). A var x T = 2
whose T is numeric declares a T: isInterface answers true for a type parameter (its underlying type is its
constraint), and the declaration arm used to read that as the empty interface and declare any x. A ~string
parameter keeps that emission until its constant has a T materialization. Guarded by the mixed and float arms
of NamedNumericOperatorConstraint (an arithmetic operand on either side, a comparison, a return, a var, an
argument and -0 != 0, instantiated with named int32 and float64 types and float32, values vs Go).
Inside the wrap a constant keeps its Go spelling where C# computes the same value: a literal (3, 0.5D), a
named constant other than an untyped float one, and an operator form whose value fits int32 (1 << 3; a
subtree whose own value leaves int32 is untyped, so overflowingConstLiteral has already folded it, and
1 << 40 >> 20 is 1099511627776L >> 20). An operator form past int32 is FOLDED to Go’s exact value instead,
because C# would compute it in int: v + 1<<40 is v + builtin.ConvertToType<T>(1099511627776L), where the
operator form masks the shift count to 5 bits and reads 256, and 1 << 31 is 2147483648L, not -2147483648. A
float operator form folds too, at any value, an integral one to its integer (1 << 40 * 1.5 is 1649267441664L,
and (0.1 + 0.2) * 10 is 3, which a double computes as 3.0000000000000004). A value that fits no 64-bit integer
belongs to a float set only, and folds as a double with its exact digits: v / (1 << 64) is
v / builtin.ConvertToType<T>(18446744073709551616D), where C# would read the shift as 1 << 0. So does a literal
or a named constant past 64 bits, which otherwise keep their form: no golib overload takes a GoBigConst and a C#
integer literal that large does not compile (CS1021), so return big with big = 1 << 70 is
builtin.ConvertToType<T>(/* big */ 1180591620717411303424D). A named UNTYPED FLOAT constant folds too, to its
double: it renders as golib’s UntypedFloat, which converts implicitly to long as well as to double, and C#
overload resolution prefers ConvertToType<T>(long) for it, so return quarter with quarter = 0.25 would read 0 at a
float64 T. It is builtin.ConvertToType<T>(/* quarter */ 0.25D), returned, converted (T(quarter)) or an
operand alike. (A golib UntypedFloat overload cannot cure it: long and UntypedFloat convert implicitly to each
other, so every ConvertToType<T> of an int would become ambiguous.) A named untyped INTEGER constant past int64
folds for the same reason: it binds the long overload with its bit pattern, which converts signed, so
return ubig with ubig = 1<<63 + 1<<39 + 1 would read negative at a float64 T; it is
builtin.ConvertToType<T>(/* ubig */ 9223372586610589697UL), as the literal already was. Where the set holds a float32 kind
(~float32, float32) and the constant’s float32 value, rounded once from the exact constant, differs from the
float32 rounding of its double, the call carries both: return tie with tie = 1 + 2^-24 + 2^-60 is
builtin.ConvertToType<T>(/* tie */ 1.0000000596046448D, 1.0000001F), because the double is the float32 tie
1 + 2^-24, which rounds to 1, where Go gives 1 + 2^-23. golib’s two-value overload hands a float32-kind T, a
type defined over float32 included, the float32 value, and any other kind the double. An INTEGER constant takes the
integer two-value form where its set mixes an integer kind with float32: return 1<<60 + 1<<36 + 1 in a function
over ~int64 | ~float32 is builtin.ConvertToType<T>(1152921573326323713L, 1.1529216e+18F), so the integer kind
takes the exact long (or ulong) and never a double, which would read ...712, and the float32 kind Go’s
single rounding, where the double would round to even at the tie it lands on. The two-value overloads are hidden
([EditorBrowsable(Never)]) and carry no public-surface row. A constant
CONVERTED to a numeric type parameter, T(9) or T(0.5), takes the same ConvertToType<T> (a cast is CS0030),
whichever kinds the set holds. Guarded by NamedNumericOperatorConstraint’s shifted, signBit, narrowed,
nine, isZero, halved, unit, scaledUp, below and tripled arms (named int64 and float64 types, values vs Go) and typeParamUntypedConst_test.go; the past-64-bit and float32 cases by
Float32AndWideConstants (tieAs, onlyF32, bigAs and litAs at float32, float64 and a type defined over
float32; mixed and mixedU at their integer kind and at float32; quarterAs, quarterConv and plusQuarter
at float64 and float32; ubigAs and plusUbig at uint64 and float64, values vs Go).
A named-wide-integer or type-parameter slice index casts to nint
Go permits any integer type as a slice/array index, converting it to int for the access. The C#
slice<E>/array<E> indexer takes nint, and the existing wide-basic index cast already routed
uint/uint32/uint64/uintptr/int64 through (nint). Two more index kinds need it, both from
internal/trace’s dataTable:
- a named type over signed
int64—type ProcID int64indexingspans[procID](CS1503). There is nothis[long]overload,int64→nintdoes not narrow implicitly, andint64→ulong(which would bindthis[ulong]) is a signed→unsigned conversion — so it has no bare path.(nint)(procID)composes as one user conversion (named→long) plus one built-in (long→nint). Every other kind is deliberately excluded — no churn, and casting some would even break: an unsigned named type (type kindT uint/uint32/uint64) binds the golibthis[ulong]overload bare, and a nuint-backed wrapper (uint/uintptr) is CS0030 under a(nint)cast; anint/int32/nintunderlying narrows implicitly (type rank intstays bare). So only signedint64both needs and accepts the cast; - a numeric type parameter —
dataTable[EI ~uint64]doingd.dense[id]. A constrained type parameter has no C# cast at all, so it routes through golib’sConvertToUInt64<K>bridge (theE(100)integer-type-param family above) and then narrows:d.dense[(nint)(ConvertToUInt64<EI>(id))](an arithmetic indexid/8is stillEI, so it wraps the same way).
The companion shift-count case —
1 << (id % 8) where the count is a numeric type parameter, coerced to int by intCastOperand
(the same coercion the shift-width machinery uses) — routes through the same bridge, with the constant
operand materialized as the type parameter:
(uint8)1 << (int)(builtin.ConvertToUInt64<EI>((id % builtin.ConvertToType<EI>(8)))) (a bare (int)(EI) is CS0030).
Guarded by NamedNumericSliceIndex (a generic lookup[K ~uint64]
indexing by the type parameter and an arithmetic result, a pick indexing by a named int64, a
num:nint rank index that must stay bare, and a bitset[K ~uint64] shifting by the type parameter,
values vs Go).
Method-set interface constraints bind the interface directly; pointer instantiations project through the adapter
A type parameter constrained by a regular method-set interface (a pure method set, no type-term
unions — go/ast’s walkList[N Node](v Visitor, list []N)) emits where N : Node against the
arity-0 emitted interface. Only union+method constraint interfaces take the generic CRTP form
(ConstraintTest1<ΔT>); a phantom Node<N>, new() here would be doubly wrong: Node is emitted
arity-0 (CS0308), and the instantiation may itself be an
interface (walkList takes N=Stmt/Expr/Spec/Decl), which can never satisfy new(). The
interface-typed and interface-inheriting instantiations then satisfy the constraint natively
(Stmt : Node is emitted inheritance).
A pointer instantiation (walkList(v, n.Names) with N=*Ident) cannot: the ж<Ident> box
does not implement the interface — its generated pointer adapter does. The call site (the last line
below) projects the slice element-wise through the adapter, instantiating N as the interface itself:
internal static void walkList<N>(Visitor v, slice<N> list)
where N : Node
{
foreach (var (_, vᴛ1) in range(list)) {
var node = vᴛ1.ΔCopy();
Walk(v, node);
}
}
…
walkList(v, builtin.widen<ж<Ident>, Node>(n.Names, elemᴛ1 => new IdentжNode(elemᴛ1)));
golib’s widen<T, TWide>(slice<T>, Func<T, TWide>) copies the slice element by element into a new
backing array. Each element is an adapter over the original box, so method calls through the
projected slice act on the real objects. The new slice does not share the caller’s backing array, so
the callee’s writes to the slice itself — an element assignment, an append into its capacity, a sort
— would land on the copy. go2cs therefore converts the call only when the callee is proven to read the
slice and nothing more: ranging over it, len, cap, a nil comparison, reading an element, using it
as copy’s source or append’s spread source, or handing it to a declared function that is itself
proven to read it. Any other use refuses the package before a file is written, with
GO2CS2006 naming the call and the callee’s first unproven use
(sliceWideningWriteRefusals.go). convertToInterfaceType supplies the adapter reference and the
GoImplement recording, exactly as at scalar *T→iface call sites. (Guarded by
GenericInterfaceConstraint — pointer, interface, and embedded-interface instantiations of a
method-set-constrained generic, calling a constraint method on the parameter and widening
walkList-style, values vs Go — and by TestSliceWideningVerdictPerShape, one call per proven read and
per refused write.)
An anonymous inline constraint carrying methods — func Show[T interface{ Name() string }],
type Box[T interface{ Name() string }] struct — is lifted to a named interface, exactly as an
anonymous parameter interface is, and named <declaration>_<parameter> (Show_T, Box_T;
liftTypeParamConstraints). Unlifted, the constraint would be named by its Go text, and
where T : interface{Name() string} is a C# syntax cascade that takes the whole package down; lifted,
it takes the same arms a named constraint takes. The lift is public when the declaration
is exported (CS0703 otherwise). A constraint that names the declaration’s own parameters
([T interface{ Less(T) bool }]) lifts generic over the ones it captures (Min_T<T>,
liftCapturedTypeParams), and each use composes its own arguments, so a receiver that renames the
parameter still binds. A method of a generic type reaches the constraint as its receiver’s own copy, a
different object and possibly in another file: a lift capturing no parameter is found by its signature
in the package registry, and one capturing a parameter, which is never registered by signature, by its
DECLARATION (constraintLiftKey: the type and the parameter’s index). Operator-only type
sets carry no methods and are not lifted.
A value instantiation of a method-set constraint that names no type parameter — named or lifted,
slot[T nameI] at tagged — is recorded at the instantiation (recordValueConstraintImplementation,
on constraintProxyFor’s per-instantiation path), not only where a call converts an argument to the
type parameter, since a generic TYPE instantiated at a value type with no such call would be CS0315. A
FUNCTION’s clause is emitted only where its body needs it (methodSetConstraintNeeded), and the
record follows the clause: errors.AsType[E error] drops it, so AsType[alert] records nothing. An
instantiated GENERIC constraint is left to recordClosedConstraintImplementation, below.
A value instantiation of a generic method-set constraint — Sum[A Adder[A]] at vec —
satisfies where A : Adder<A> only as vec : Adder<vec>, a base go2cs-gen declares from a
[assembly: GoImplement<vec, Adder<vec>>] record. A non-generic constraint (where T : Namer) gets
its record where the argument converts to the type parameter, but that site sees the constraint over
the callee’s own parameter, Adder[A], which no concrete type implements, so that site alone would
never record the generic form and every such instantiation would be CS0315. recordClosedConstraintImplementation, on
the per-instantiation path every form reaches (constraintProxyFor), closes the constraint over the
argument and records it, once per pair: rendering a self-referential closed form re-enters the same
path, so the once-guard is also what ends that recursion. This is
the shape Go 1.26’s self-referential constraints are written in, and an Adder[A any] constraint takes
the same path. (Guarded by SelfRefGenerics, output-compared vs Go.)
A constraint naming ANOTHER type parameter of the same declaration ([K any, T into[K]]) is closed over
the whole instantiation instead, where every argument is known (recordInstantiatedConstraint, called
beside the per-argument record at both instantiation forms): apply(Name("abc"), 2) infers K = int,
and where T : into<K> is checked as Name : into<nint>, so the record is
GoImplement<Name, into<nint>>. A generic type instantiated the same way (holder[int, Name]) is
recorded the same way. The other parameter must be a bare argument of the constraint; one nested inside
another type (into[[]K]) is not closed and writes no record.
type into[K any] interface {
Into(K) K
}
type Name string
func (n Name) Into(k int) int { return len(n) + k }
type Pair struct{ a, b string }
func (p Pair) Into(k string) string { return p.a + k + p.b }
func apply[K any, T into[K]](v T, k K) K {
return v.Into(k)
}
internal static K apply<K, T>(T v, K k)
where T : into<K>
{
return v.Into(k);
}
[assembly: GoImplement<Name, into<nint>>]
[assembly: GoImplement<Pair, into<@string>>]
Guarded by GenericConstraintRecord (a function and a generic type over into[K], beside the
self-referential lesser[T], output-compared against go run).
An INLINE constraint naming its own parameter (type Ord[T interface{ Less(T) bool }], lifted to
Ord_T<T>) is closed over each instantiation exactly as the named Lesser[T] is: inlineConstraintNamed
presents the lift as a synthetic generic interface (Ord_T[T], its method set rewritten over fresh
parameters by substituteTypeParams, since go/types offers no substitution for an anonymous interface
over a parameter bound elsewhere), and recordClosedConstraintImplementation and constraintProxyFor
read it through effectiveConstraint. Ord[age] records GoImplement<age, Ord_T<age>>, Ord[*node]
takes the proxy nodeжOrd_T, and Ord[wrap[string]] records the closed pair the generator re-opens
over wrap<E> (below). The lift’s name is assigned by the declaration’s own visit, so an instantiation
in a file visited first DEFERS: its record replays after the file-visit barrier
(replayDeferredConstraintRecords, before any metadata file is written), and a proxy name carries the
deferred marker the barrier resolves. A constraint declared in ANOTHER package takes the lift name
its package PUBLISHED: the lift is generic over the parameter it captures, so it is published by
DECLARATION, never by signature, as [assembly: GoInlineConstraintLift("Ord", 0, "Ord_T")] (the
owning type or function’s Go name, the type parameter’s index, the lift). An importer finds the
declaration by the type parameter itself in its package’s scope and reads the record
(importedInlineConstraintLift), so lib.Ord[age] records GoImplement<age, lib.Ord_T<age>> and
lib.Ord[*node] takes the lift’s generic proxy (below). A package that published none (an older
conversion) leaves the anonymous form.
A GENERIC value type implementing a generic interface at its own instantiation
(var l Lesser[wrap[string]] = wrap[string]{1}, or the constraint above at wrap[string]) is recorded
CLOSED (an attribute’s type arguments cannot be open), and a partial can extend only the open
definition, so a closed interface there — partial struct wrap<E> : Lesser<wrap<@string>> — would
forward a call that cannot bind (CS1929). go2cs-gen therefore RE-OPENS the interface over the target’s
own parameters (wrap<E> : Lesser<wrap<E>>), at any depth of nesting (ReopenInterfaceOverTarget),
validated member by member against the target’s Go methods, so every closed record of wrap shares
one partial and an argument that only coincides with the target’s (a namer[E] whose Get returns
string) keeps the closed form. (GenTests GenericValueInterfaceReopenTests.)
A same-package generic whose interface method names its OWN type parameter satisfies a NON-generic interface for
one instantiation only (Stack[T]’s Push(T) against Push(int)). No re-open applies, so that pair takes a
CLOSED CARRIER: an adapter class written for the one instantiation, named from the type’s bare name and
recorded with Closed = true. The interface may be named or ANONYMOUS: var p interface{ Push(int) } =
&Stack[int]{} lifts the interface to a named C# interface of the package (main_p), which is never generic,
so it is carried the same way, new Stackжmain_p(Ꮡ(new Stack<nint>())). (Guarded by GenericImplCarrierShapes,
pointer and value forms beside a named interface, output-compared against go run.)
The POINTER form takes the same re-open. var a Source[*Node] = &sliceSource[*Node]{…} beside
var b Source[int] = &sliceSource[int]{…} records two closed pointer pairs, and the local generic
pointer adapter is ONE class over the type’s open parameters that each cast site instantiates
(new sliceSourceжSource<ж<Node>>(box)). Until 2026-11-29 the adapter kept the record’s CLOSED
interface (sliceSourceжSource<T> : Source<ж<Node>>), so a forward returning T could not convert
(CS0029, one instantiation alone) and the second record minted the same class again under its own
key (CS0102). The interface is now re-opened over the type’s parameters
(sliceSourceжSource<T> : Source<T>), validated against the POINTER method set: a method the
converter emits as this ref sliceSource<T>, or one declared over the box itself
(this ж<boxed<T>>), counts, and an argument that only coincides keeps the closed interface. A
record marked Closed (the carrier that keeps a per-instantiation adapter) is not re-opened. A
generic over an interface over a COMPOSITE of its parameter re-opens the same way
(chunker[T] implementing Source[[]T] takes chunkerжSource<T> : Source<slice<T>>). An argument
equal to more than one of the type’s arguments (pair[int, int], whose Next returns V) is tried
at each of their parameters, and the mapping the type’s own methods validate is taken
(pairжSource<K, V> : Source<V>). (Guarded by GenericPtrIfaceAdapter, output-compared vs
go run, and GenTests GenericPointerInterfaceReopenTests, red at the old generator with CS0102 and
CS0029, and for the equal arguments with CS0695.)
Three pointer shapes keep the closed interface and do not build. A type whose interface methods are all
PROMOTED from an embedded generic struct (derived[T]{ sliceSource[T]; tag string }): the promoted
One pointer shape keeps the closed interface and does not build: a type whose interface methods are all
PROMOTED from an embedded generic struct (derived[T]{ sliceSource[T]; tag string }). The promoted
methods are written by the struct-embedding generator, whose output this generator cannot see, so the
re-open finds no method of the type’s own and two records mint one adapter twice (CS0102). And a type
whose methods all come from an embedded interface over its own parameters (gy[T]{ Source[T] }): the
converter cannot record that promotion and warns by name, and the value partial forwards to methods
the type does not declare (CS1929) while the pointer adapter forwards a T (CS0029). And an interface that
needs its one argument at two of the type’s parameters at once (Pair[T] with First() T and Second() T, met
by both[K, V], whose First returns K and whose Second returns V, at both[int, int]): every mapping
fails one of the two methods, so the adapter forwards a result it cannot convert (CS0029). C# has no constraint
that says K equals V, so this shape needs an adapter per instantiation, not another mapping.
re-open finds no method of the type’s own and two records mint one adapter twice (CS0102).
A type whose methods all come from an embedded INTERFACE over its own parameters (Wrapper[T]{ Getter[T] },
gy[T]{ Source[T] }) re-opens. No record can name that promotion (typeof(Getter<T>) leaves T unbound), so
the converter writes the promoted methods as the struct’s own value methods instead,
public static T Get<T>(this Wrapper<T> recvᴛ) => recvᴛ.Getter.Get();, and those are Go methods over the open
definition that the re-open validates. The conversions’ closed records (GoImplement<Wrapper<nint>,
Getter<nint>>, value and pointer) then take partial struct Wrapper<T> : Getter<T> and the adapter
WrapperжGetter<T> : Getter<T>. (Guarded by GenericImplCarrierShapes: one and two type parameters, the
value stored by value and by pointer, and a type assertion back, output-compared against go run.)
A constraint with type TERMS beside its methods (interface{ ~int; String() string }) takes its
implementer as a type argument instead: see
A constraint with a type term and methods takes its implementer as a type argument.
A method expression on a type parameter is a lambda over its receiver p0
Go’s T.String, with T a type parameter constrained by a method-set interface, is a function value taking the
receiver as its first argument. C# has no method group for a type parameter’s interface method, so the expression
emits a typed delegate over a lambda whose first parameter p0 is the receiver and whose remaining parameters are
the method’s own; a direct call T.String(x) invokes that delegate:
func Apply[T fmt.Stringer](x T) string { return T.String(x) }
public static @string Apply<T>(T x)
where T : fmt.Stringer
{ return ((Func<T, @string>)((p0) => p0.String()))(x); }
The call through p0 binds against the constraint where T : fmt.Stringer, so it reaches the same method a call
through a T value reaches. Inside a declaration that carries a SHAPE WITNESS for the parameter, T has no such
constraint: a generic type instantiated at a pointer argument (Bag[*Ref], where only *Ref has String) takes an
extra witness parameter, Bag<T, TΔStringer> where TΔStringer : struct, StringerΔshape<T>, and its methods call
through the witness. The method expression does the same:
internal static @string First⁔<T, TΔStringer>(/*this*/ Bag<T, TΔStringer> b)
where TΔStringer : struct, StringerΔshape<T>
{ return ((Func<T, @string>)((p0) => TΔStringer.String(p0)))(b.xs[0]); }
Guarded by the TypeParamMethodExpression behavioral test (a method value and a direct call on a type parameter,
a method with its own argument, value, named and pointer type arguments with an explicit interface argument, and a
generic type’s methods at a pointer argument with a witness and at a value argument without one, output-compared
vs Go).
A type-parameter value converted to an interface its constraint does not reach goes through any
C# checks a constraint nominally, so a value of type T converts to an interface only when T’s constraint
is that interface, embeds it, or inherits it as a structural base. Go converts whenever the method set
suffices, so a constraint that spells the methods itself (T interface{ String() string } handed to a
fmt.Stringer), or one with more methods than the target, has no C# conversion at all. The converter routes
such a value through any and golib’s run-time interface assert, which binds the dynamic type’s methods from
its own assembly, exactly as any(v).(fmt.Stringer) does:
func show[T interface{ String() string }](v T) string {
var s fmt.Stringer = v
return s.String()
}
internal static @string show<T>(T v)
where T : show_T
{
v = v.ΔCopy();
fmt.Stringer s = ((any)(v).OrTypedNilOf())?._<fmt.Stringer>();
The explicit conversion takes the same route, so fmt.Stringer(v) and var s fmt.Stringer = v agree:
b.WriteString(fmt.Stringer(v).String())
Ꮡb.WriteString(((fmt.Stringer)((any)(v).OrTypedNilOf())?._<fmt.Stringer>()).String());
A constraint that embeds the interface keeps the plain conversion:
func control[T embeds](v T) namer { return v }
internal static namer control<T>(T v)
where T : embeds
{
v = v.ΔCopy();
return v;
}
Why: no [GoImplement] record can serve, because the generic body never sees the dynamic type, and an
inheritance edge on the constraint would invent a relation the Go tree lacks. The run-time assert answers the
question Go answers, from the type argument the call actually supplied, including one declared in another
assembly. OrTypedNilOf() keeps a nil pointer’s dynamic type, and ?. keeps a null as Go’s nil interface (an
interface type argument holding nil converts to nil in Go too). A conversion whose target is itself a type parameter
(U(v)) is not an interface conversion and keeps its own route. A POINTER type argument arrives as the
constraint proxy, which holds a nil pointer as ж<T>.NilBox (as the ordinary pointer adapter does), so
ret[*P](nil) is a non-nil interface holding (*P)(nil), as in Go. The ZERO value of such a type argument is not
covered: see Known issues.
Limit: the assert costs a type test and, the first time per dynamic type and interface, a binder lookup that golib caches. Only the sites the constraint does not reach pay it.
Guarded by: TypeParamInterfaceConversion (every conversion site Go has: assignment, explicit conversion,
return, argument, slice, map, channel and struct-field elements, a multi-value return, a generic type’s method,
error as the target, a pointer type argument and a typed nil one, an interface type argument nil and not, with the
embedding and structural-base constraints as controls), output-compared vs Go; TestTypeParamTargetConversionKeepsItsNumericRoute
(U(v) keeps builtin.ConvertToType<U>).
A SELF-REFERENTIAL generic method-set constraint uses a box-wrapping proxy as the type argument
The widen-to-the-interface escape above works only when the constraint interface is non-generic
(N=Node, so N can be Node). crypto/elliptic’s nistCurve[Point nistPoint[Point]] — where
nistPoint[T] is a generic, self-referential method-set interface (Add(T,T) T,
SetBytes([]byte) (T,error), …) — cannot: you can’t substitute Point = nistPoint<Point> (infinite
regress), and the golib box ж<P224Point> cannot nominally implement nistPoint<ж<P224Point>>
(it is a sealed golib type in another assembly, and Go’s structural satisfaction has no C# analog).
Four coordinated pieces make it convert and dispatch:
-
The constraint interface is emitted GENERIC. A method-set interface whose own Go type parameter is used in its member signatures carries its
<T>(and constraints) in C#, exactly like a generic struct —partial interface nistPoint<T> { … T Add(T, T); (T, error) SetBytes(slice<byte> _); }. Without it the declaration is arity-0 yet the constraint that references it spells the arity-1where Point : nistPoint<Point>(CS0308) and every bareTis undefined (CS0246). (Go’s operator-only constraint interfaces are arity-0 in Go, so this is disjoint from the<ΔT>operator machinery.) -
One GENERIC adapter class implements the outer interface:
nistCurveжCurve<Point> : Curve, IжAdapter where Point : nistPoint<Point>wrappingж<nistCurve<Point>>— NOT a class per instantiation. The converter’sGoImplementrecords are per-instantiation but all resolve to the open form here, soImplementGeneratorde-dups on the open(struct, interface)pair and forwards its type parameters and the struct’s own constraint (GetGenericConstraintClause). The converter composes the reference name+args separately (nistCurveжCurve<…>, base+ж+iface, then the closed args) so the type arguments do not bake into the identifier (nistCurve<…>жCurveis CS1526). -
A self-referential PROXY stands in for the type argument. For each concrete pointer type used to instantiate the generic (
nistCurve[*P224Point]), the converter renders the type argument as a generated proxyP224PointжnistPoint(element-simple+ж+iface-simple) instead of the boxж<P224Point>, and records[assembly: GoImplement<P224Point, nistPoint<P224Point>>(ConstraintProxy = true)](the interface’s own argument is a placeholder).ImplementGenerator.EmitConstraintProxyemits:internal sealed class P224PointжnistPoint : nistPoint<P224PointжnistPoint>, IжAdapter { private readonly ж<P224Point> m_box; public P224PointжnistPoint(ж<P224Point> box) => m_box = box; public static implicit operator P224PointжnistPoint(ж<P224Point> box) => new(box); public static implicit operator ж<P224Point>(P224PointжnistPoint proxy) => proxy.m_box; // T rewritten to the proxy itself; the implicit conversions marshal every T-boundary: P224PointжnistPoint nistPoint<P224PointжnistPoint>.Add(P224PointжnistPoint a, P224PointжnistPoint b) => m_box.Add(a, b); (P224PointжnistPoint, error) nistPoint<P224PointжnistPoint>.SetBytes(slice<byte> b) => m_box.SetBytes(b); // … }The proxy implements the interface over itself, so
Point = P224PointжnistPointsatisfieswhere Point : nistPoint<Point>(CS0311 otherwise) and resolves everyp.Add(…)/newPoint().SetBytes(…)call insidenistCurve’s body. The implicitж<P224Point>↔proxy conversions do all the T-boundary marshalling automatically: each forwarder is a barem_box.M(args)(arguments unwrap to the box on the way in, results rewrap to the proxy on the way out — including element-wise inside a(T, error)tuple), and a value flowing into aPoint-typed position (base: Ꮡ(new P224Point(…))) converts implicitly at the site. The proxy forwards to the element’s exportedж-extensions even cross-assembly (m_box.SetBytesbinds nistec’s extension from crypto/elliptic). -
A
func()-typed field’s method-group initializer is re-wrapped as a lambda.nistCurve’snewPoint func() PointbecomesFunc<P224PointжnistPoint>, but a method group (newPoint: nistec.NewP224Point, returningж<P224Point>) cannot convert to it — a C# method-group conversion does not apply the user-defined implicit operator (CS0407). Inside a constraint-proxy composite the converter re-wraps such an initializer as a lambda,newPoint: () => nistec.NewP224Point(), whose return position does apply the conversion. -
The proxy forwards the WHOLE method set, embedded interfaces included. A Go constraint interface may embed others, and Go embedding emits as C# interface inheritance — so the members a proxy must forward are not the ones the constraint DECLARES but the ones its method set CONTAINS. Walking
GetMembers()only would forward the declared half, and any member reached through an embed would be absent: the proxy would not implement its own interface (CS0535, one per inherited member).net/httpshows it at full size; itsclientserver_test.godeclarestype TBRun[T any] interface { testing.TB Run(string, func(T)) bool }giving proxies for
*testing.Tand*testing.Bthat must forwardRunand every member of the embeddedtesting.TB.AllInterfacesis transitive, so a two-level embed needs no recursion of the emitter’s own.Each member is qualified by its own declaring interface, not by the constraint: C# explicit interface implementation must name the interface that declares the member, so
void Derived.M()is CS0539 whenMcomes fromBase. A generic embed is closed over the proxy exactly as the constraint itself is (Bar<T>embedded inFoo<T>forwards asBar<proxy>.M), reusing the sameRenderWithProxysubstitution. The constraint’s OWN members are emitted first in declaration order, so a proxy that embeds nothing forwards exactly its declared members in order; the embedded sets follow, ordered by rendered reference so the emission does not depend on the orderAllInterfaceshappens to report. The interface-ADAPTER path beside it walks base interfaces the same way.
(Guarded by GenericPointerInterfaceImpl — a self-referential curve[Point point[Point]] implementing
Curve via pointer receiver, instantiated two ways, with a newPoint func() Point field and a
(T, error)-returning constraint method, values vs Go — and by ConstraintProxyEmbeddedInterface for
the method-set walk: a constraint embedding Middle embedding Base, so one member arrives one level
deep and another two, recorded from two instantiation sites to exercise the per-pair de-duplication,
output-compared vs go run. Reverting the walk reproduces CS0535 on Middle.Size() and Base.Name()
— both levels. Embedding the constrained generic is
the next section.)
- An interface that constrains ITSELF takes the proxy as the record’s placeholder. Go 1.26 lets
a generic type name itself in its own type-parameter list —
type GraphNode[N GraphNode[N]] interface { Next() N; Owner() *Graph[N] }, andtype Graph[N GraphNode[N]] struct, a cycle that earlier Go versions reject as “invalid recursive type”. The interface declaration carrieswhere N : GraphNode<N>, and C# enforces it on an attribute’s type arguments too, so the element placeholder inGoImplement<gnode, GraphNode<gnode>>would be CS0315 on the record itself. For such an interface (interfaceConstrainsItself) the placeholder is the proxy,GraphNode<gnodeжGraphNode>: it implements the interface over itself, so it satisfies the bound by construction, and the generator reads only the interface’s open definition. Every other record keeps the element. The proxy is generated into the class that carries the record, so the placeholder is named bare. (Guarded bySelfRefGenerics: the cycle at a pointer instantiation, values vs Go.)
A FOREIGN interface’s proxy is the interface package’s: concrete when it published one, else its generic proxy
The proxy is the type argument, so it is part of the instantiated type’s identity: lib.Ord2[*elem.Node]
is ONE C# type only if every package that names it names ONE proxy. So no package ever declares a
proxy for an interface another package declares; a second importer would declare a second one. The
choice is made in foreignConstraintProxy, by two rules:
- A concrete proxy the interface’s package PUBLISHED for the element wins. That package records the
pair wherever it closes the constraint over the element, which it can do only for an element it
imports, and every instantiator imports it, so every importer reads the same record
(
publishedConstraintProxyElements, from whichever sourceloadImportedTypeAliasesreads, applying nothing). std’s crypto/ecdh and crypto/ecdsa namefips140/ecdh’s andfips140/ecdsa’sP256PointжPointthis way. The element’s package cannot publish one for a foreign interface: that would be the consumer-local proxy this rule forbids. Two published proxies of one name (two elements calledNode) are refused by name, leaving the box. - Otherwise the interface package’s GENERIC proxy, closed over the element:
lib.Ord2<lib.жLesser<elem.Node>>. go2cs-gen publishesжLesser<E> : Lesser<жLesser<E>>beside every interface a package declares and closes over the parameter it constrains (where T : Lesser<T>, named or an inline lift;ConstraintProxyGenerator, driven by the constraint clauses, so the converter writes no record for it). It cannot forward by itself, because the element’s Go methods are extension methods overж<Node>and an extension method does not bind through a type parameter. So each forwarder calls a static slot typed over the box, and every package that instantiates the constraint records the pair,GoImplement<elem.Node, lib.Lesser<elem.Node>>(ConstraintProxy = true), which for a FOREIGN interface the generator turns into a[ModuleInitializer]binding the slots (lib.жLesser<elem.Node>.ᴛLess = static (recv, ᴛ0) => recv.Less(ᴛ0);). A module initializer runs before any code of its assembly, so no value of the type exists unbound, including one made by reflection. Every binder binds the same methods, so a second changes nothing. A slot nobody bound panics naming the proxy, the element, the interface and the method.
Cost: a generic-proxy forwarder makes one delegate call per forwarded method, where a concrete proxy calls the extension directly. The standard library pays nothing, since all of its foreign-interface sites take rule 1.
A WRITTEN instantiation in a type position, such as a type assertion or a conversion, renders through
the resolved type whenever an argument takes a proxy (proxiedTypeInstantiation, gated to that case).
The AST walk would render the box, lib.Ord2<ж<elem.Node>>, which is another type. To reflection, a proxy
is the pointer it stands for: %T prints lib.Ord2[*ForeignConstraintProxy/elem.Node], and
reflect.Set of a *elem.Node into the proxy-typed field stores it wrapped. (Guarded by
ForeignConstraintProxy: two, three and four packages, named and inline, b1’s value received,
assigned, boxed, asserted, %T and reflect.TypeOf compared, and a reflect-created value filled
through reflect, output-compared vs Go. Also GenTests ForeignConstraintProxyTests, which executes
separately built assemblies.)
What a proxy forwards, and the instantiations go2cs refuses
A proxy is a separate C# object per pointer, wrapping the element’s box. So each forwarder marshals every
position of a constraint method that mentions the self parameter N, in both proxy kinds
(MarshalAcrossProxy in go2cs-gen):
- The whole
N, or a tuple of such values: the proxy’s implicit conversions, as above. - A func, at any depth, named or not (
Next() func() N,Each(func(func(N) bool) bool),Path() iter.Seq[N]): re-wrapped in a lambda over the target’s own parameter types, and a nil func stays nil (ᴛNext(m_box) is { } ᴛf0 ? new Func<жFlow<E>>(() => ᴛf0()) : null). - A Go array (
Triple() [3]N): copied element by element, which is exact, because Go copies arrays too. - A type that names
Nonly as a type argument whose parameter is itself self-constrained (Owner() *Graph[N]withGraph[N GraphNode[N]]) is the same C# type on both sides and passes as written.
Every other position SHARES the element’s storage in Go: a []N, map[K]N, chan N or *N, or a generic
type carrying N through a parameter that is not self-constrained (List[N]). A slice of proxies is a
different array from the slice of boxes the element holds, so a forwarded copy would compile and silently
break Go’s aliasing. The converter therefore refuses, before any file is written, every instantiation that
would name such a proxy (refuseUnforwardableConstraintProxies, error GO2CS2002, naming the site, the
interface and each method). An instantiation over a VALUE element needs no proxy and converts, and its
Children() slice aliases the element’s backing array exactly as in Go. Because go2cs-gen publishes the
generic proxy for every self-referential constraint, it also gives such a method no slot and makes its
forwarder throw the same refusal. The interface’s own package still compiles: without the slot rule, a single
Children() []N would break that package’s build (CS0029), even when only value elements used it. By
default the refusal is fatal to the conversion; -warn GO2CS2002 downgrades it to a warning, and each refused site
then panics with the refusal text when it is reached. The converted standard library reaches no refused shape, and
no behavioral fixture does either.
Two exact designs remain open, and nothing in the corpus needs them: a per-element copy of the generic function
in which N is the box itself (monomorphization), or static-abstract witness types in place of the proxy.
Both are larger and read less like the Go source. (Guarded by the converter’s constraintProxyForwarding_test.go,
GenTests ProxyContainerForwardingTests, which executes both proxy kinds, and behavioral
ProxyContainerForwarding, where a value element’s shared Children() slice and a pointer element’s func
and array results are output-compared with Go.)
A func LITERAL at a proxied delegate position declares its parameters AT the proxy
Item 4 above moves a METHOD GROUP’s T-boundary into a lambda body, because a method-group conversion
will not apply the user-defined conversion. A func literal meets the same wall one position further
in and cannot take that remedy — it already is the lambda, and it renders its own parameter list
from the Go signature. At T = ImplжConstrained a func(t T, mode int) argument emits
(ж<Impl> t, nint mode) => … against a delegate requiring Action<ImplжConstrained, nint>, and
C# applies no user-defined conversion at a parameter DECLARATION: CS1678 + CS1661, one pair per
call site. net/http’s suite is written on this shape throughout —
run[T TBRun[T]](t T, f func(t T, mode testMode), opts ...any).
The remedy is the same principle: move the conversion to a position C# performs it. The parameter is declared at the proxy under a synthesized name and the body opens with the natural-typed alias:
run<TжTBRun>(Ꮡt, (TжTBRun tΔ1Δp, testMode mode) => {
var tΔ1 = (ж<testing.T>)tΔ1Δp; // the proxy's own implicit operator
… // body unchanged, byte for byte
});
Everything after that line is the body exactly as it would have been emitted, so no member access, capture, or nested literal inside it renders differently. Declaring the parameter at the proxy and letting the body use it directly would not work: the forwarders are explicit interface implementations, reachable through a type parameter’s constraint but not by member lookup on the concrete proxy type.
Restricted to a parameter whose type is the proxied type parameter exactly. One that merely
MENTIONS it ([]T, map[K]T, func(T)) is excluded — slice<ImplжConstrained> and
slice<ж<Impl>> are distinct instantiations with no conversion between them, so no single assignment
could stand in the prologue, and guessing would trade a clear diagnostic for a wrong one.
⚠ Both halves key on the RENDERED name. A literal’s signature is generated from synthesized vars
carrying the shadow-renamed name, so keying the proxy map on the Go name misses every renamed
parameter — and misses it asymmetrically, because the body prologue reads the same map from the
AST, where the Go name is present: the prologue would be emitted while the declaration stayed at its
natural type, producing a local with the same name as the parameter beside it. net/http is entirely
the renamed case (its inner t shadows the outer), so the guard
carries both spellings and the un-renamed one alone would have passed over the real defect.
The anchored adapter REFERENCE keeps the shadow marker
The -tests metadata-anchored resolution composes the adapter class reference a cast site will use
(anchoredAdapterMemberName) while go2cs-gen composes the class it emits. The two must agree
character for character, including the shadow marker: the generator names a local adapter
from adapterBaseName — the C# type name verbatim, Δhandler — and a foreign one from
GetSimpleName(structName), neither of which strips it, so the reference side keeps it too.
net/http’s internal test variant declares type handler struct{ i int } (server_test.go),
shadow-renamed to Δhandler, so the generator mints ΔhandlerжΔHandler, and a stripped reference,
handlerжΔHandler, would name a class that is never emitted (CS0426).
The rule: the marker belongs to the C# IDENTITY of the type, not to a rendering
convention. adapterStructKey strips it for GROUPING, which is right — a collision
group must not depend on which side got renamed — but that key must not double as the emitted name.
Only the -tests anchored path composes the reference this way: a production conversion resolves
through adapterResolvedName, which never strips. A mismatch here reads like an adapter minted for one
test variant and referenced from the other, and it is not — the record is bridge-anchored in
package_info_internal_test.cs and the class is minted in http_internal_test_package, exactly where
the reference looks for it. Only the NAME can differ.
⚠ One adjacent surface is deliberately NOT addressed and is worth naming, since a reader meeting it
will otherwise read it as this defect: a T RETURNED out of a constrained generic into concrete code
arrives as the proxy TYPE, whose forwarders are explicit interface implementations and so are
unreachable by member lookup there (r := second(p); r.Name() — CS1929, resolving instead to the
element’s own extension whose receiver it cannot satisfy). The proxy carries an implicit conversion
back to ж<element>, but C# does not apply a user conversion during member lookup. Nothing in the
corpus or in net/http reaches it; the guard’s second builds its result inside the generic context
on purpose.
A struct embedding the constrained generic promotes its members — three residual crypto-curve fixes
crypto/elliptic’s p256Curve struct { nistCurve[*nistec.P256Point] } (Go 1.24’s source; from Go 1.25
p256 is a *nistCurve[*nistec.P256Point] itself) — a non-generic struct embedding a concrete
instantiation of the self-referential-constrained generic above — must PROMOTE
nistCurve’s internal fields (newPoint, params) and methods (Add/Double/Params/ScalarMult/…)
onto p256Curve, exactly as an embed of a plain struct does, so p256.params = … binds and the generated
p256Curve→Curve interface adapter can forward curve.Add(…) to the promoted shim. Because the type
argument is the box-wrapping proxy of the previous section, its rendered name embeds the marker glyph
ж (nistCurve<P256PointжnistPoint>) — the thread that runs through the rules the
crypto-curve family (elliptic, ecdh, nistec) needs to compile:
-
The proxy marker glyph
жis not a pointer prefix. The generator’s simple-name / underlying-name helpers (GetSimpleName,GetUnderlyingTypeName) match the pointer prefix ofж<T>as the two-characterж<, so a marker embedded mid-identifier (P256PointжnistPoint) is left intact. Scanning for a bareжwould mis-slice an embed typednistCurve<P256PointжnistPoint>into garbage (its simple nameoint.Value, its underlying name an unresolvable string), and the embed would promote nothing (CS1061 onparams, CS1929/CS1501 on every forwarded method). -
A generic-instantiation embed resolves to its declaration and substitutes its type arguments. An embed of a generic INSTANTIATION (
nistCurve<P256PointжnistPoint>) resolves to the generic DECLARATION (nistCurve<Point>) by base-name + arity (FindStructDeclaration— an instantiation can never string-match a declaration that carries its type PARAMETERS), and a generic struct’s extension methods match on the type-parameter-bearing receiver (nistCurve<Point>, not the barenistCurve). The promoted field and method signatures are harvested from the declaration, so they carry its type PARAMETER (Func<Point>,pointFromAffinereturning(Point, error)); the template rewrites each to the instantiation’s type ARGUMENT before emission —internal ref global::System.Func<P256PointжnistPoint> newPoint => ref nistCurve.newPoint; internal static (P256PointжnistPoint p, error err) pointFromAffine(this ref p256Curve target, ж<bigꓸInt> Ꮡx, ж<bigꓸInt> Ꮡy) => target.nistCurve.pointFromAffine(Ꮡx, Ꮡy);— so no promoted member references the out-of-scope
Point. (The member ACCESS hop keeps the bare property namenistCurve; only the emitted TYPE is substituted.) When the ENCLOSING struct is itself GENERIC —wrapped<T>embeddingtag<T>(theGenericStructFieldsguard) — the promoted method is a GENERIC extension method carrying the struct’s own type parameters (static T show<T>(this wrapped<T> target) => target.tag.show();, the substitution then an identityT→T), else theTin the receiver and return is an undefined type name (CS0246). -
The constraint proxy imports its element’s package namespace. The proxy forwards each interface method to the boxed element’s box extension methods (
m_box.Bytes()), which live in the element type’s PACKAGE class (nistec_package, namespacego.crypto.@internal). The[assembly: GoImplement<…>(ConstraintProxy = true)]attribute driving the proxy sits inpackage_info.cs, whose usings never cover a FOREIGN element, so the forwarders would bind nothing (ж<P224Point>“has noBytes”, CS1929/CS1501).EmitConstraintProxyemitsusing <element-namespace>;for the box element’s namespace. -
An open-generic interface cast is CONVERTED but not RECORDED. Inside a generic method the receiver itself is cast to the interface — crypto/ecdh’s
return newBoringPrivateKey(c, …)withc *nistCurve[Point](Go 1.23’s source; from Go 1.24 crypto/ecdh’snistCurveis not generic). The converter must still WRAP it in the generic adapter (new nistCurveжΔCurve<Point>(Ꮡc)— the adapter the CLOSED per-instantiation records already generate), but must NOT RECORD it as an implementation: a record emits[assembly: GoImplement<nistCurve<Point>, ΔCurve>], whose type-PARAMETER argumentPointis out of scope in an assembly attribute (CS0246).convertToInterfaceTypeskips the record for an open-generic target while still firing the adapter-wrapping conversion.
(Fix 2 is guarded by the GenericEmbedPromotion behavioral test — a non-generic struct embedding a concrete
curve[*p224] over a self-referential proxy: reading a promoted internal field, calling a promoted method
whose parameter is the type argument (passed the promoted proxy-typed field), and reaching the promoted
methods through a non-generic interface adapter, values vs Go. Fixes 3 and 4 need a cross-package element /
a generic-method interface cast the single-package baseline cannot express; they are validated by the census
— elliptic, ecdh, and nistec emit their DLLs.)
Constraint-only type parameters need explicit type arguments
Go infers a type parameter that appears only in constraints through core types — func Twice[S ~[]E, E Integer](s S) infers E from S’s underlying element; the slices package’s whole Sort[S ~[]E, E cmp.Ordered] → pdqsortOrdered chain relies on this. C# never infers a type parameter that does not appear in the parameter list (CS0411 — at every call site, concrete instantiations included). When the callee declares such a constraint-only type parameter, the converter renders the call’s type arguments explicitly from the instantiation go/types already resolved (info.Instances): Twice<Point, int32>(p, 2) at a concrete site, Scale<S, E>(s, c) inside a generic body. Calls to generics whose every type parameter is argument-visible keep their bare Go-shaped form — C# infers them as Go does, no churn. (Guarded by the GenericTypeInference extension — a constrained S/E pass-through chain plus a concrete call to a constraint-only-param generic, values vs Go, the shape of the slices/maps Sort chain.)
The same explicit-type-argument rule applies to a generic function referenced as a method-group value, not just a call. slices.SortFunc(all, slices.Compare) (runtime/pprof) passes slices.Compare[S ~[]E, E cmp.Ordered] as SortFunc’s comparison delegate; C# cannot infer a generic method group’s constraint-only E when converting it to Func<…> (CS0411). convSelectorExpr spells the arguments on the selector — slices.Compare<slice<uintptr>, uintptr> — when the selector is NOT the callee of a call (!context.isCallExpr, so convCallExpr’s own type-arg site still owns the call form) and info.Uses[Sel] is a generic function with an info.Instances instantiation; every Compare(...) call stays bare. No behavioral test guards the qualified form: the shape needs a cross-package generic function with a constraint-only type parameter passed as a method-group value, which the single-package baseline cannot express (the same-package form is guarded below).
The bare-IDENT variant takes the same rule. convIdent carries the same call-vs-value flag convSelectorExpr has — IdentContext.suppressGenericTypeArgs, set by convCallExpr for a call’s CALLEE and by convIndexExpr/convIndexListExpr for the base of an explicit instantiation — so a same-package generic function passed as a method group (apply(s1, Reverse) against Reverse[S ~[]E, E any], slices’ TestInference) spells its arguments out (reverse<slice<nint>, nint>) exactly as the qualified form does, and the two spellings of one Go reference cannot disagree. The flag is what keeps every direct call bare while the value form spells its arguments. The append rides on whichever spelling the ident’s own tail arms produce (a -tests Δ-rename, a white-box bridge qualification), so a renamed generic function passed as a method group is covered too.
Three further shapes in the same family, all met in slices’ converted test suite (each a CS0411/CS0305, or a CS1503 cascade behind one, without its rule):
- An explicitly-instantiated generic function argument is still a method group.
EqualFunc(s1, s2, equal[int]),CompareFunc(s1, s2, cmp.Compare[int]),CompactFunc(s, equal[int]),equalToCmp(equal[int])— writing the type arguments fixes the group’s shape, not its C#-inference status, so the enclosing generic call still needs its own arguments spelled.exprIsMethodGrouptherefore peelsParenExpr/IndexExpr/IndexListExprbefore classifying (indexing a function value is not legal Go, so peeling can never reclassify an ordinary map/slice index). - A type parameter reachable only through an UNSUPPLIED parameter position.
Insert[S ~[]E, E any](s S, i int, v ...E)called asInsert(s, 0)hands C# an emptyparams variadic<E>, andEis inferable from nothing — whilecalleeHasConstraintOnlyTypeParamcannot see it, becauseEdoes appear in a parameter type.calleeTypeParamUnsuppliedByCallasks the question C# inference actually asks — which parameter positions did this call supply — and since every non-variadic parameter is always supplied in a well-typed Go call, it can fire on nothing but an empty variadic.Insert(s, 0, 7, 8)keeps its bare form. - A PARTIAL explicit instantiation. Go allows a written prefix and infers the rest through core types (
Equal[Slice]againstEqual[S ~[]E, E comparable], slices’ owniter_test); C# has no partial instantiation, so the prefix alone is CS0305, “requires 2 type arguments”.completedInstantiationTypeArgsreplaces the written list with the resolved one only when the resolved list is longer — a complete instantiation, which is nearly all of them, renders verbatim and byte-identically. The comparison is made after erasure filtering on both sides, so an erased pointer-core position cannot make a complete instantiation look partial. A written ANONYMOUS type argument is lifted before the list is rendered (Go 1.26 reflect’stestTypeAssert[interface{ A() }](t, vv, nil, false)). The completed list renders each position from its go/types TYPE, and only visiting a type expression lifts an anonymous interface or struct, so an unvisited written argument would render as raw Go text,interface{A()}, an unresolved dynamic type that fails reflect’s whole-testsconversion.liftWrittenTypeArgsvisits each written argument that spells an anonymous type. It emitstestTypeAssert<TestTypeAssert_type, testTypeWithMethod, …>, the lift and name a complete list gives. Guarded byTestPartialInstantiationLiftsAnonymousTypeArgand thePartialInstantiationAnonymousTypeArgbehavioral project, which asserts through each lifted interface and gets Go’s answers.
Every one of the four is an ADDITION to the trigger set rather than a replacement, and each is gated on a property that is arithmetic rather than heuristic (is this argument a function reference; did this position receive an argument; is the written list shorter than the resolved one), which keeps them to exactly the shapes that need them; the production code of slices and maps, which leans hardest on this inference, emits no differently for them. Guarded by the MethodGroupGenericArg extension, which without these rules fails with exactly the slices error set — CS0411 ×4 (instantiated method-group argument, empty variadic, bare-ident generic value ×2) plus CS0305 ×1 (partial instantiation).
A NAMED composite argument at an UNNAMED composite parameter
keys[V any](m map[int]V) called keys(names) with type Names map[int]string: Go infers V through the named
type’s underlying type. The named type emits as a wrapper struct (partial struct Names) that reaches
map<nint, V> only by a user-defined implicit conversion, and C# type inference never looks through a user-defined
conversion, so the bare call is CS0411 (the hashset module’s test has the shape).
callPassesNamedCompositeToCompositeParam
adds the shape to the trigger set: an argument whose type is a NAMED map, slice, channel or array, at a parameter
whose declared type is an UNNAMED map, slice, channel or array mentioning a type parameter. The call then spells
the resolved arguments, keys<@string>(names), total<float64>(scores), drain<nint>(feed), first<nint>(grid),
and the conversion applies once the parameter type is closed.
Three neighbours keep their bare form. An unnamed argument (keys(map[int]string{...})) already has the
parameter’s shape. A named argument at a bare type-parameter position (ident[T any](v T)) infers the wrapper
itself. A named FUNC type emits as a delegate that the parameter’s delegate type infers from (measured compiling
without the
rule). The standard library and the behavioral corpus have no other site of the shape.
Guarded by namedCompositeGenericArg_test.go and the NamedCompositeGenericArg behavioral test, which without the
rule fails with CS0411, one per composite kind.
The comparable constraint
Go’s built-in comparable admits every ==-able Go type — numerics, strings, pointers, channels, and comparable structs/arrays/interfaces. No C# constraint can express that set: a CRTP comparable<T> interface would be implemented by nothing (no real instantiation, such as maps.Keys[M ~map[K]V, K comparable], could satisfy it), and lifting IEqualityOperators would reject structs, which Go admits. A comparable type parameter therefore emits no C# constraint at all — no where clause — relying on the two facts that make it sound: Go’s checker already validated every instantiation, and emitted equality on type parameters routes through AreEqual, never operator ==.
The arm emits no new() either. golib’s @new<T> constructs through the runtime, and no comparable-constrained body constructs its parameter; and a new() would be a defect: Go pointers are comparable, a Go pointer type argument instantiates at the abstract ж<T>, and no abstract class satisfies a constructor constraint (unique’s HashTrieMap[*abi.Type, any] is the corpus witness, CS0310, with slices/maps/cmp instantiations at pointer element types behind it). Guarded by the corpus compile plus the Constraints-family behavioral goldens, which pin the clause-free form.
An interface that embeds comparable inherits that fact whole, and both sides of the emission
have to say so. type netipTypeCmp interface { comparable; netipType } (net/netip’s fuzz_test.go) is the shape:
a bare comparable in the DECLARATION’s C# base list would be CS0305, and a CONSTRAINT deciding the
interface is not a method set would take the generic CRTP form netipTypeCmp<P> against a declaration
emitted arity-0, CS0308. comparable contributes no methods, so it is dropped from the base list
(leaving the interface’s C# surface exact) and DISCOUNTED when deciding whether an interface is a
pure method set — which puts the constraint in the arity-0 where P : netipTypeCmp form the
declaration actually emits. An interface mixing comparable with a real type-term union still
restricts its type set and keeps the generic treatment.
AreEqual itself is not a performance tax on that path: a generic overload AreEqual<T>(T, T) — automatically preferred by overload resolution exactly where both operands share the type parameter — takes EqualityComparer<T>.Default.Equals for value-type arguments, which the JIT specializes per type and devirtualizes to the type’s own IEquatable<T> (operator-comparable speed, no reflection or boxing; golib wrappers emit operator == and Equals as consistent pairs, so semantics match). Reference/interface type arguments delegate to the reflective AreEqual(object, object) overload, preserving its typed-null and runtime-type semantics. (A constraint-differentiated overload pair is not expressible — C# treats where clauses as outside the signature, CS0111 — and a source-generated == twin is unnecessary given the EqualityComparer<T>.Default JIT intrinsic.) (The behavioral GenericVariadicFunc golden captures the erased form with unchanged output.)
Floating-point equality follows Go’s IEEE-754 ==, not Equals. The Equals-based fast path
above is wrong for exactly one family: double/float report NaN.Equals(NaN) as true (and
Complex/golib complex64 inherit that componentwise), while Go’s == — the operation AreEqual
stands in for — is IEEE: NaN compares unequal to everything, itself included. Equals would invert
every generic NaN probe of the x != x form: cmp.isNaN emits !AreEqual(x, x), so cmp.Less would
lose its NaN-first ordering (sort’s TestFloat64s — Float64s → slices.Sort — would produce a
NaN-scrambled order) and cmp.Compare would report NaN equal to everything, letting the mis-sort slip
past TestSortFloat64sCompareSlicesSort’s own equality check. The generic overload therefore special-cases
double, float, complex128, and complex64 to the operator compare (JIT-constant typeof(T)
guards, box-cast elided). The reflective object overload (boxed/interface comparisons) is
IEEE-correct as it stands: on .NET 7+ the primitives DECLARE op_Equality (the
IEqualityOperators implementation), so its cached operator lookup finds the real operator rather
than falling to Equals. Concrete (non-generic) float comparisons need nothing — f != f
emits the C# operator directly. (Guarded by the ReverseSortNaNOrder
behavioral test — generic isNaN/less/eq legs over float64/float32/complex128/
complex64, boxed-any NaN equality, and a NaN-aware interface sort, values vs Go.)
Generic struct equality is decided per FIELD, not per type parameter
A generic converted struct’s synthesized Equals (see Struct Types) cannot be
gated on the struct’s TYPE PARAMETERS alone. A whole-struct gate that requires every parameter to carry an
IEqualityOperators-implementing constraint (and every constraint of every parameter to implement it)
leaves the whole struct’s Equals body the constant false /* missing equality constraints */, and
since a comparable parameter deliberately emits no C# constraint
(The comparable constraint),
that would be essentially every generic struct in the corpus, breaking equality that never depends on
the parameter at all. unique.Handle[T]’s only field is *T (ж<T>), whose pointer-identity == is
valid for every T, and its documented contract is that “two handles compare equal exactly if the
values used to create them would”; internal/weak.Pointer[T]’s only field does not mention T at all.
Even internal/trace.dataTable[EI, E] — whose EI explicitly lists IEqualityOperators — would fail
the every-constraint quantifier because IAdditionOperators and its siblings do not themselves
implement the equality interface.
When that gate fails, the struct decides per member (GetEqualityFallbackMembers in
StructDeclarationSyntaxExtensions): a member whose type supports == independent of the
unconstrained parameters keeps the same this.f == other.f compare a non-generic struct emits, and
only a member whose type IS an unconstrained type parameter falls back to golib’s
AreEqual — the identical routing the converter emits for Go == on any type-parameter
operand, giving EqualityComparer<T>.Default speed on value types while preserving IEEE float
semantics (raw EqualityComparer reports NaN equal to itself, inverting Go — see the
floating-point note above) and typed-null/runtime-type semantics for reference and interface
instantiations. Real emissions (from the converted stdlib’s generated sources):
// unique.Handle<T> — ж<T> has pointer-identity == for every T:
public bool Equals(Handle<T> other) =>
this.value == other.value;
// database/sql.Null<T> — mixed: the T field routes through AreEqual, the rest keep ==:
public bool Equals(Null<T> other) =>
global::go.builtin.AreEqual(this.V, other.V) &&
this.Valid == other.Valid;
// net/http.mapping<K, V> — golib slice/map fields carry their own ==, so no member falls back:
public bool Equals(mapping<K, V> other) =>
this.s == other.s &&
this.m == other.m;
The member classifier asks only “does == COMPILE for this member type”: a type parameter
qualifies through ANY IEqualityOperators-implementing constraint (matching C# operator
resolution, not the whole-struct gate’s every-constraint test); reference types (classes incl.
ж<T> and unsafe.Pointer, interfaces, arrays, delegates), enums, pointers, and built-in value
types always qualify; a converted struct qualifies by its attribute — both struct templates
emit a same-type operator == unconditionally, and for a struct of the same compilation the
attribute is the only visible evidence, because that operator does not exist yet while the
generator runs; any other value type qualifies only by actually declaring a same-type
op_Equality. The fallback set is computed only when the whole-struct gate fails; a struct that passes
it, and every non-generic struct, compare every member with ==.
GetHashCode needs no matching change (golib.HashCode.Combine always compiled and hashes
consistently with both compare forms). (Guarded by the GenericStructEquality behavioral test —
the Handle pointer-identity shape, the plain-T fallback shape, a T-independent-field struct, the
Null-shaped mix, a nested generic struct field, and a generic struct as a map key, all
output-compared vs Go.)
A generic struct implementing an interface BY VALUE partials at its OPEN definition
A Go method on a generic type is declared for every instantiation, so func (g G[T]) M() makes
G[int], G[string] and G[G[int]] all satisfy an interface with M. The converter records a
[assembly: GoImplement<…>] per instantiation it sees. One partial struct per record, spelled with
the record’s TYPE ARGUMENTS (partial struct G<IntPtr> : I), would not work: C# reads that argument
list as a type-parameter list, so the declaration disagrees with the converter’s own
partial struct G<T> (CS0264) and the mismatched parts stop merging — every member the template writes
then lands in the containing static package class instead (CS0715 on the operators, CS0708 on
Equals/GetHashCode/ToString, CS0563 and CS0540 in the cascade). ImplementGenerator’s value-form
arm therefore emits ONE partial against the open definition, keyed
by (OriginalDefinition, interface) so all instantiations of a pair fold into it; the member and
value-pair dedupe indexes key on the same open form, since two interfaces over one open generic
share a single partial. Constraints are deliberately omitted — a partial declaration may leave them
off and they merge from the converter’s declaration, so omission can never raise CS0265. This is the
value-form sibling of the pointer-adapter arm (emittedGenericPointerAdapters, crypto/elliptic’s
nistCurve[Point]).
A single-letter generic needs one more rule, in the shared GetSimpleName helper. Asked to drop a
type-argument list, the helper tests the simple name’s IndexOf('<') > 0; a > 1 test would keep
G<T>’s arguments (its < sits at index 1), and StructTypeTemplate, which derives the constructor
name from that call, would emit public G<T>(NilType _), which is not a constructor to C#: the
partial struct G<T> scope never opens and the same spill follows. Indexing the simple name rather
than the full one also avoids a miscut on a dotted generic (a.Map<K, V> indexed at 5 into an
8-character Map<K, V>, yielding Map<K).
internal/reflectlite (type B[T any] struct{}) and runtime/debug
(type G[T any] struct{} with var dummy I = G[int]{} and var dummy2 I = G[G[int]]{}) are the
standard-library packages with this shape.
Guarded by the GenericValueInterfaceImpl behavioral test — a single-letter generic held as an
interface at three instantiations, a sibling type named exactly like the type parameter (the
runtime/debug shape that made the spilled members render as debug_test_package.T), struct
equality, struct-versus-interface comparison, and interface dispatch over a mixed slice, all
output-compared against go run.
A conversion at a generic type’s OWN type parameters is recorded by the type’s DEFINITION
Inside a generic function, a value of a local generic type instantiated at that function’s own type parameters can be converted to an interface:
func NewIter[T any](values []T) Iter {
it := sliceIter[T](values)
return &it
}
The cast site emits new sliceIterжIter<T>(…), naming the generic adapter, but no
[assembly: GoImplement<…>] record can carry the pair: an assembly attribute’s type argument cannot name
T, which is in scope only inside NewIter (CS0246). Without a record the adapter was generated only when
some OTHER conversion closed the same pair (sliceIter[int] in main, or crypto/elliptic’s per-curve
nistCurve[*nistec.P224Point] records); a type reached only through its parameters failed CS0246 at the cast, and
the value form CS0029.
The converter therefore records such a pair by its definition, through the non-generic
GoImplementAttribute, which takes typeof arguments and so can spell an unbound generic. The guard’s records,
with the closed sliceIter[int] record beside the open one:
[assembly: GoImplement(typeof(counter<>), typeof(Iter), Pointer = true)]
[assembly: GoImplement(typeof(pair<,>), typeof(Namer))]
[assembly: GoImplement(typeof(sliceIter<>), typeof(Iter), Pointer = true)]
[assembly: GoImplement<sliceIter<nint>, Iter>(Pointer = true)]
ImplementGenerator reads the unbound generic as its definition, so the record takes the same
local-generic arms as a closed record of the pair: the pointer adapter keyed by (OriginalDefinition,
interface) and the value partial at the open definition (previous section). An open record and a closed
record of one pair generate ONE adapter, byte-identical to the closed record’s own.
The scope is a generic converted to an interface with NO type parameters. A foreign generic’s pointer
conversion takes it only when every method the interface requires is declared on the generic’s definition
with a signature free of its type parameters (foreignGenericAdapterIsOpen in interfaceConversion.go).
Then every instantiation satisfies the interface the same way. The converter records the definition,
package-qualified, and names the adapter at each cast site with the instantiation’s arguments as C# type
arguments. ImplementGenerator emits ONE generic adapter, package-prefixed like every foreign adapter
(fieldlib_ExpectedжField<T>, wrapping ж<global::…fieldlib_package.Expected<T>>). Several instantiations of
one pair in one package therefore share it (Expected[string] and Expected[map[string]string] as
TypeValidatedField):
[assembly: GoImplement(typeof(go.GenericForeignAdapterInstantiations.fieldlib_package.Expected<>), typeof(go.GenericForeignAdapterInstantiations.fieldlib_package.Field), Pointer = true)]
show("string", new fieldlib_ExpectedжField<@string>(Ꮡname));
show("map", new fieldlib_ExpectedжField<map<@string, @string>>(Ꮡdeps));
show("int", new fieldlib_ExpectedжField<nint>(Ꮡsize));
Two shapes keep their earlier emission:
- A foreign generic whose required method MENTIONS a type parameter keeps one non-generic adapter per
closed instantiation (
internal/sync.HashTrieMap[any, any]as sync’smapInterface, whoseLoad(key K)an adapter open overKcould not implement asLoad(any)). Go lets exactly one instantiation satisfy such an interface, so the un-suffixed closed name cannot collide. A promoted method, a generic interface, and a definition that does not implement the interface take this route too. An open conversion of such a pair still records the open instantiation,GoImplement<…lib_package.Box<T>, Getter>, which namesTout of scope. - An interface over the type parameter (
Getter[T]) cannot be named bytypeofin its closed form either; that pair is unrecorded, as before.
Guarded by the OpenGenericPointerImplement behavioral test (pointer and value forms, a type reached only
through its parameters, and a closed record beside the open one, output-compared against go run), by
GenericForeignAdapterInstantiations (three instantiations of one foreign generic through one interface, and
a closed-route Box[string] as Getter, output-compared), by TestOpenGenericImplementIsRecordedByDefinition
and TestForeignGenericAdapterRoute in the converter, and by GenTests OpenGenericImplementRecordTests for the
one-adapter claim.
The string | []byte union
C# generic constraints are conjunctive (“and”), so they cannot express Go’s string | []byte union directly. The two members share no operators (the union is neither comparable nor additive), so a conforming body may only use the read operations common to both — indexing, len, and sub-slicing. These are captured by the golib read-only byte-sequence interface IByteSeq, which both @string and slice<T> implement; the converter emits it for the union and suppresses the (spurious) lifted operator constraints:
func HashStr[T string | []byte](sep T) (uint32, uint32) { /* uses sep[i], len(sep) */ }
public static (uint32, uint32) HashStr<T>(T sep)
where T : /* string | []byte */ IByteSeq<T, byte>, new()
The constraint is self-referential — the C# rendering of the CRTP shape, IByteSeq<TSelf, T> : IByteSeq<T> where TSelf : IByteSeq<TSelf, T>, instantiated at the type parameter itself. @string implements IByteSeq<@string, byte> and slice<T> implements IByteSeq<slice<T>, T>, so the sub-slice indexer returns the concrete sequence type rather than the interface. That extra type parameter exists purely to keep the union allocation-free (see Allocation-free union-constrained bodies).
len resolves through a len<TSeq>(TSeq) where TSeq : IByteSeq overload that is dispreferred for concrete slice<byte>/@string arguments, so existing call sites keep their specific overloads (no ambiguity). Go’s []byte(s) and string(s) over a constrained value render as the golib extensions s.ToSlice() and s.ToGoString(), each generic over the caller’s concrete type; both preserve Go’s per-instantiation semantics exactly ([]byte([]byte) shares its backing, []byte(string) copies, string(string) is free, string([]byte) copies). The behavioral test StringByteUnionConstraint exercises both the string and []byte instantiations across all of these.
Allocation-free union-constrained bodies
A single-parameter interface (IByteSeq<T>) with an IByteSeq<T> this[Range] sub-slice indexer would force a box in every member whose signature names the sequence type, and Go bodies over this union sub-slice constantly. With that shape:
| Emitted shape | Boxed | Why |
|---|---|---|
((bytes)(s[a..b])) |
one box (slice<byte> or @string) |
the range indexer returns the INTERFACE, so the struct result is boxed — and the cast then unboxes it |
len(s) |
one box per call | the overload takes IByteSeq<T>, an interface parameter |
new slice<byte>(s) — Go’s []byte(s) |
one box per call | the constructor takes IByteSeq<T> |
new @string(s) — Go’s string(s) |
one box per call | the constructor takes IByteSeq<byte> |
A parseRFC3339-shaped body (seven sub-slices, eight len calls, six []byte(s) conversions per parse) allocates hundreds of bytes per parse that way, for Go code that allocates nothing. The remedy is one idea applied four times: never name the sequence type as an interface in a signature a generic body reaches.
- Sub-slicing moves to the self-referential
TSelf this[Range]. Both implementers’ public range indexers already return their own type, so this is satisfied implicitly, and the call becomes aconstrained.direct dispatch on the value type. The result IS the type parameter, so the converter emits the range expression bare (above). lentakes the constrained type parameter (len<TSeq>(TSeq) where TSeq : IByteSeq) instead of the interface.[]byte(s)/string(s)become theToSlice/ToGoStringextension methods rather than constructors. C# has no generic constructor, so a constructor can only accept the interface; and a static factory cannot even be named here, because converted code carriesusing static go.builtin, which shadows thesliceand@stringtype names with the builtin conversion methods (slice<byte>.From(s)is CS0119 — “is a method, which is not valid in the given context”). An extension call is member access on the receiver, so it sidesteps both problems. Each foldstypeof(TSeq) == typeof(…)to a per-instantiation constant, so the sharing case reduces to a field copy.
Measured: 0 B/parse on slice<byte>; on @string what remains is exactly the six byte[] copies the six []byte(string) conversions must materialize (Go copies too), since an @string sub-slice is a window over its backing and allocates nothing. Guarded by GolibTests ByteSeqAllocationTests, which asserts the zero, the six-object count on @string, and a deliberately-boxed control that must still report at least 700 B/parse. That control pins its box behind a [MethodImpl(NoInlining)] boundary on purpose: the JIT elides a box/unbox pair written adjacently in one method, so an inline control would understate the box’s cost. An interface indexer boxes inside a callee and returns, which is why the cost is really paid.
A ~string type parameter takes IByteSeq, and a string meets it through StringToType
A type parameter whose every term is a string (~string, string, a named constraint such as type Str interface{ ~string }, one that reaches it through another (interface{ Str }), or one beside comparable) gets the same self-referential IByteSeq<T, byte> as the string | []byte union, and keeps the lifted operators, since a string set is comparable, ordered and additive. Indexing, len, sub-slicing and string(t) (as t.ToGoString()) then work as they do for the union. range t reads t.ToGoString(), because Go ranges a string by rune and IByteSeq enumerates nothing.
A string VALUE meeting T cannot be a C# cast: T is a type parameter, and @string is not T even when T is instantiated at a defined type such as type Mode string. Each such site goes through golib’s builtin.StringToType<T>(@string), which returns the string itself when T is @string and otherwise calls the IGoStringWrapper<T> member that go2cs-gen gives every defined string type:
const c = "on"
func eqConst[T ~string](t T) bool { return t == c }
func konst[T ~string]() T { return "k" }
func before[T ~string](t T) bool { return t < "m" }
func toStr[T ~string](t T) string { return string(t) }
internal static bool eqConst<T>(T t)
where T : /* ~string */ IByteSeq<T, byte>, IAdditionOperators<T, T, T>, IEqualityOperators<T, T, bool>, IComparisonOperators<T, T, bool>, new()
{ return AreEqual(t, builtin.StringToType<T>(c)); }
…
internal static T konst<T>()
where T : /* ~string */ IByteSeq<T, byte>, IAdditionOperators<T, T, T>, IEqualityOperators<T, T, bool>, IComparisonOperators<T, T, bool>, new()
{ return builtin.StringToType<T>((@string)"k"u8); }
internal static bool before<T>(T t)
where T : /* ~string */ IByteSeq<T, byte>, IAdditionOperators<T, T, T>, IEqualityOperators<T, T, bool>, IComparisonOperators<T, T, bool>, new()
{ return t < builtin.StringToType<T>("m"); }
internal static @string toStr<T>(T t)
where T : /* ~string */ IByteSeq<T, byte>, IAdditionOperators<T, T, T>, IEqualityOperators<T, T, bool>, IComparisonOperators<T, T, bool>, new()
{ return t.ToGoString(); }
The same conversion covers Go’s T(s) and var s T = "a", which declares T rather than any, and a switch over T: a label that meets T is a call, never a C# constant, so it takes a when guard (case var ᴛ when AreEqual(ᴛ, builtin.StringToType<T>("off"u8)), and in a tagless switch case {} when t > builtin.StringToType<T>("m")). A numeric type parameter’s labels take the same guard over ConvertToType<T>. Conversions out of T read it through the byte sequence: []byte(t) is t.ToSlice(), []rune(t) is slice<rune>(t.ToGoString()), Mode(t) is ((Mode)t.ToGoString()) and copy(b, t) is copy(b, t.ToGoString()). A min or max of three or more arguments over any type parameter nests as two-argument calls, min(min(a, b), builtin.StringToType<T>("m"u8)): golib’s many-argument overload asks IComparable<T>, which the emitted constraint does not carry, and Go’s rule over n values is its pair rule applied left to right (NaN and signed zero included). The comparison is the reason the conversion is not optional: without it t == c compared a Mode with an @string through AreEqual’s object overload and answered false where Go answers true.
StringToType<T> throws NotSupportedException only for a type argument that lacks the IGoStringWrapper<T> member, and go2cs-gen gives that member to every defined string type, hand-owned files included; the one type that could lack it is a hand-written struct that does not pass through go2cs-gen, and no string-only constraint in std is instantiated with one.
The conversion is a separate method rather than a ConvertToType<T>(@string) overload: @string converts implicitly from a rune, so an @string overload would make the numeric ConvertToType<T>(int literal) calls ambiguous.
A constraint with METHODS beside its ~string term (interface{ ~string; String() string }) keeps IByteSeq<T, byte> beside its lifted interface: see A constraint with a type term and methods takes its implementer as a type argument.
Guarded by ApproxStringDefinedType (every shape above plus len, indexing, sub-slicing, concatenation, rune ranging over non-ASCII text, cmp.Ordered and a named Str constraint, tagged and tagless switches over a string and a numeric type parameter, T as a map key, a typed constant, the conversions out of T, three-argument min and max, and the two indirect constraints, instantiated at Mode and at string, output-compared against go run), by GenTests StringWrapperConversionTests for the generated member, and by typeParamUntypedConst_test.go in the converter.
A constraint with a type term and methods takes its implementer as a type argument
A Go constraint that lists both a type term and methods (interface{ ~string; String() string }, interface{ ~int; String() string }) has no type parameter of its own in Go, but it lifts to a C# interface over its implementer, Stringer<ΔT>, because the term’s operators (+, < and the rest) are static abstract members that must name the implementing type. A type parameter it constrains is checked as where T : Stringer<T>. Beside the lifted interface the constraint keeps the surface the same term takes without methods: IByteSeq<T, byte> for ~string or ~string | ~[]byte, so len, indexing, sub-slicing, string(t) and []byte(t) read through the byte sequence; ISlice<E>, ISupportMake<T> and ISliceWrap<T, E> for ~[]E; and IMap<K, V>, ISupportMake<T> for ~map[K]V:
type Mode string
func (m Mode) String() string { return "mode:" + string(m) }
type Stringer interface {
~string
String() string
}
func show[T Stringer](t T) string { return t.String() + "/" + string(t) }
partial interface Stringer<ΔT> /*operators = Sum, Comparable, Ordered*/ {
// Type constraints: ~string
// Derived operators: +, ==, !=, <, <=, >, >=
@string String();
}
…
internal static @string show<T>(T t)
where T : Stringer<T>, IByteSeq<T, byte>, new()
{ return t.String() + "/"u8 + t.ToGoString(); }
A slice term beside a method ranges, takes len and indexes through ISlice:
type Lener interface {
~[]int
Len() int
}
func sum[T Lener](t T) (int, int) {
n := 0
for _, x := range t {
n += x
}
return n, t.Len() + len(t) + t[0]
}
internal static (nint, nint) sum<T>(T t)
where T : Lener<T>, ISlice<nint>, ISupportMake<T>, ISliceWrap<T, nint>, new()
{
nint n = 0;
foreach (var (_, x) in range(t)) {
n += x;
}
return (n, t.Len() + len(t) + t[0]);
}
A type argument meets the constraint as Mode : Stringer<Mode>, so its implementation record names the interface with that argument. The record is written in the typeof form, because C# refuses an interface with static abstract members as an attribute’s generic type argument; go2cs-gen reads both forms the same way:
[assembly: GoImplement(typeof(Level), typeof(Leveler<Level>))]
[assembly: GoImplement(typeof(Mode), typeof(Stringer<Mode>))]
[assembly: GoImplement(typeof(Mode), typeof(inline_T<Mode>))]
An inline constraint (func inline[T interface{ ~string; String() string }]) lifts to inline_T<ΔT> and is recorded the same way, as the last line shows.
An array term beside a method keeps IArray<E>, so the body indexes, ranges and takes len. A union of terms that share no surface (~struct{ n int } | ~[2]int) adds nothing beside the lifted interface: the body can only call the methods, and where T : named<T> is what binds them, where a method-free union of composite terms emits no constraint at all (above):
type triple interface {
~[3]int
Put(*string)
}
type Tri [3]int
func (t Tri) Put(s *string) { *s = fmt.Sprint("tri:", t[0]+t[1]+t[2]) }
func sumArray[T triple](v T) string {
var s string
v.Put(&s)
total := 0
for _, e := range v {
total += e
}
return fmt.Sprint(s, " len=", len(v), " mid=", v[1], " total=", total)
}
internal static @string sumArray<T>(T v)
where T : triple<T>, IArray<nint>, new()
type named interface {
~struct{ n int } | ~[2]int
Name() string
}
internal static @string describe<T>(T v)
where T : named<T>, new()
{ return "<"u8 + v.Name() + ">"u8; }
A constraint that is generic itself names its own type arguments, so it takes no implementer: a method naming the type parameter ([T interface{ ~[2]int; Equal(T) bool }], lifted to oneSelf_T<T>) or a generic named constraint ([T equaler[T]]) is checked as where T : oneSelf_T<T>, IArray<nint>, new(), and each type argument is recorded with the constraint closed over it, GoImplement<Pair, equaler<Pair>>, the generic attribute form, since such an interface carries no static operators. Because it carries none, the clause adds the operators of the term after its surface, as the methodless [T ~int] clause does: [T lesser[T]] over ~int is checked as where T : lesser<T>, IAdditionOperators<T, T, T>, …, IComparisonOperators<T, T, bool>, new(), so a body’s a < b and b + 1 bind. An array term has no operators to add.
An inline constraint whose method names the type parameter lifts to an interface declared over that parameter (twins_T<T>), not over its implementer, so its name already carries the type argument and nothing is appended. Each type argument is recorded against the interface closed over it, GoImplement<Pair, twins_T<Pair>>, and the term keeps its surface:
func twins[T interface {
~[2]int
Same(T) bool
}](v T) bool {
return v.Same(v) && len(v) == 2 && v[0] == v[1]
}
internal partial interface twins_T<T> /*dyn*/ {
// Type constraints: ~[2]int
// Derived operators: none
bool Same(T _);
}
internal static bool twins<T>(T v)
where T : twins_T<T>, IArray<nint>, new()
{
v = v.ΔCopy();
return v.Same(v) && len(v) == 2 && v[0] == v[1];
}
A method that names ANOTHER type parameter lifts the same way, over that parameter (keyOne_T<K>), and the record is closed over the whole instantiation, GoImplement<Pair, keyOne_T<nint>> for keyOne[int](Pair{7, 8}):
func keyOne[K comparable, T interface {
~[2]int
Key() K
}](v T) (K, int) {
return v.Key(), len(v)
}
internal partial interface keyOne_T<K> /*dyn*/ {
// Type constraints: ~[2]int
// Derived operators: none
K Key();
}
internal static (K, nint) keyOne<K, T>(T v)
where T : keyOne_T<K>, IArray<nint>, new()
{
v = v.ΔCopy();
return (v.Key(), len(v));
}
A package that converts another package’s type at that package’s constraint (lib.Show(lib.Kind("k"))) writes no record of its own when the declaring package already records the pair: the record is read back in its typeof form and keyed on the interface without its type argument, which is the record’s own target, so the importer’s qualified spelling (Stringer<go.lib_package.Kind>) and the library’s bare one (Stringer<Kind>) name one pair. A second record would make go2cs-gen compose a value adapter class in the importer, and an adapter class cannot implement the term’s static operators.
No pointer can satisfy a type term, so go2cs-gen writes no generic constraint proxy (жStringer<E>) for such an interface: a proxy class could not implement the term’s static operators, and no pointer argument needs one. A constraint generic in Go (Lesser[T]) keeps the generic attribute form, GoImplement<age, Lesser<age>>.
Guarded by ApproxStringMethodConstraint (the ~string set with +, <, len, an index, a sub-slice, string(t) and []byte(t); the inline spelling; the ~int twin with int(t) and t + 1; constraints generic themselves over ~[2]int, over ~int with <, +, *, - and %, and over ~string with +, len and <; and ~[]int, ~map[string]int and ~string | ~[]byte sets through range, len, an index and a map read; instantiated at defined types with the method and output-compared against go run), by ApproxStringMethodConstraintImport (a library that records its own pair and an importer that must not record it again), by UnionMethodConstraint (the ~[3]int, ~[]int and ~struct{ n int } | ~[2]int sets beside a method, and the inline union and single-term spellings whose method names the type parameter or another one, output-compared against go run), and by approxStringMethodConstraintPin_test.go in the converter.
Explicit type arguments come from the callee’s instantiation
A generic function’s explicit type arguments are read from the CALLEE’s resolved instantiation (info.Instances), not from the RESULT type’s arguments – the two lists differ whenever the callee has more type parameters than the result names. reflect’s rangeNum[T, N](num N, t Type) iter.Seq[Value] called rangeNum[int8](v.Int(), v.Type()): the result Seq[Value] carries ONE argument where the method needs TWO (CS0305). The result’s own arguments still gate whether to emit, so a generic callee returning a plain named type keeps C# inference:
return rangeNum<int8, int64>(v.Int(), v.Type());
Guarded by GenericTypeInference (seqOf[T ~int64, N ~int32 | ~int64](n N) Seq[T]).
A CONSTANT argument that fixes a type parameter is retyped to the instantiation
Go infers a type parameter from an untyped constant’s default type; go2cs then maps that Go type to C#. The two do not meet: an untyped int defaults to Go int, which is go2cs nint, but the constant emits as a bare C# literal whose own type is int (System.Int32). C# infers the type argument from the ARGUMENT, so wantValue(0) makes C# choose T = int where Go chose nint.
Where the mismatch only reaches a VALUE, C# repairs it – int -> nint is implicit, so a result that IS the bare type parameter converts at the use site. It does not repair what the callee observes of its own type argument (%T, a type switch, an interface box); the next section gives every such call explicit type arguments instead. What cannot be repaired is an invariant position: C# generics have no variance for these, so Action<int, bool> is not Action<nint, bool> and slice<int> is not slice<nint>. Wherever the type parameter reaches a CONSTRUCTED type, the wrong instantiation is terminal (CS1503/CS0315/CS0411).
Two gates therefore retype the constant to the C# spelling of the type Go resolved (untypedIntGenericArgCastType, applied through the per-argument castArgToType plumbing):
- The sibling
~[]Elock.Index[S ~[]E, E comparable](s S, v E)(slices): Go fixesEfromS’s core type, butwhere S : ISlice<E>carries no such flow, so C# infersEfrom the value alone andslice<nint>then fails the~[]intconstraint. - An invariant RESULT position (
typeParamReachesInvariantResult): the parameter appears inside a func, slice, array, map, chan or pointer result.internal/concurrent’s own test suite ships the control pair that isolates this exactly –expectMissing[K, V comparable](t, key K, want V) func(got V, ok bool)calledexpectMissing(t, s, 0)mis-inferredVand its returned delegate then rejected the map’snint(CS1503 x16), whileexpectDeleted(..., 15) func(deleted bool)– the same untyped literal,Vabsent from the result – compiled untouched.
wantValue((nint)0)(i, false); // V reaches func(V, bool) -- retyped
wantPresent<nint>(15)(true); // V absent from the result -- explicit type argument (next section)
bareResult<nint>(42); // result IS V -- explicit type argument (next section)
wantValue<nint>(0)(i, false); // Go wrote the type argument -- bare
sliceOf((nint)9); // []V is invariant -- retyped
wantInt64((int64)1234567890123L); // the cast follows the RESOLVED width, not always nint
The cast type comes from the resolved type, so it is nint, long, byte, nuint and so on as the instantiation requires; a resolved int32/rune is skipped because a bare C# literal already IS System.Int32. A generic NAMED result is skipped too – invariant likewise, but the explicit type-argument rule above already pins that instantiation, and retyping the argument as well would be redundant. Non-constant arguments never qualify: their emitted C# already carries the mapped Go type. Folded constant EXPRESSIONS do qualify (3 + 4, 1 << 10), since they emit as bare C# arithmetic in exactly the same way.
Guarded by GenericUntypedIntArg (the ~[]E lock) and GenericUntypedConstInfer (the invariant-result gate, its control shapes, and the nint/long/byte widths), both output-compared vs go run.
A type parameter fixed only by untyped constants gets explicit type arguments
The cast above repairs the invariant positions; it leaves the callee itself instantiated wrong. show[E any](e E) called show(3) is show[int] in Go, and fmt.Sprintf("%T", e) inside it prints int. Emitted bare, C# infers E from the literal and instantiates show<int>, so the same line prints int32. A named untyped constant is worse: it emits as its golib UntypedInt wrapper, so show(named) instantiates show<UntypedInt> and prints the wrapper’s fields.
When every argument that reaches a type parameter is an untyped constant at a position whose type IS that parameter (a variadic element included), and at least one of those constants would infer a different C# type, the call emits the instantiation go/types resolved (calleeTypeParamFixedByUntypedConst). The argument text does not change:
fmt.Println(show<nint>(3));
fmt.Println(show(2.5D));
fmt.Println(show((rune)'a'));
fmt.Println(show(1D + 2D.i()));
fmt.Println(show<nint>(named));
fmt.Println(show<nint>(1 << 10));
fmt.Println(show((@string)"s"));
fmt.Println(show(true));
fmt.Println(many<nint>(1, 2, 3));
fmt.Println(keyed<@string, nint>((@string)"k", 4));
Calls stay bare where C# already infers Go’s type: a float (2.5D), rune, string, bool or complex constant, whose emission carries its type; an integer resolved to int32; an integer outside the int32 range, which emits with a cast (toFloat64(unchecked((nint)(9007199254740993L))), from the TypeParamFloatConversion golden); a parameter some typed argument also reaches, at any position (pair(x, 3) with x int8), since C# infers from that argument, including through an alias (lib.Twice(f, 3) with f lib.Mapper[int]) and through an erased pointer-core parameter’s ж<T> (setThrough(Ꮡn, 55), above); and a constant the two cast gates above already retype, so those calls keep their emission. Measured over std and its tests on windows, linux and darwin, the shape occurred only in _test.go files, so the converted stdlib did not change.
Guarded by UntypedConstTypeArg (%T of int, float, rune, complex, string, bool, named and shifted constants through generics, a variadic and a two-parameter callee, and typed-argument controls), output-compared vs go run.
Increment/decrement on a type parameter
i++ / i-- on a constrained type parameter binds IIncrementOperators<T> / IDecrementOperators<T>, which the lifted Arithmetic operator set includes (reflect rangeNum’s loop; CS0023 without them). They live in the numeric-only Arithmetic set – never the string-including Sum set, since @string implements neither. The list is emitted in two places that must stay in sync: the converter’s getLiftedConstraints (constraintOperations.go) and the go2cs-gen InterfaceTypeTemplate.
Unary negation on a type parameter
-x on a constrained type parameter binds IUnaryNegationOperators<T, T>, which the lifted
Arithmetic operator set includes (math/rand/v2’s
func keep[T int | uint | int32 | uint32 | int64 | uint64](x T) T { return -x }, CS0023). Like
increment/decrement it is numeric-only — @string has no negation — so it never joins the Sum set.
Satisfying it needed a matching change on the generator side, because a NAMED Go numeric type is
instantiated through its go2cs-gen wrapper. Go defines -x on EVERY numeric type, unsigned
included, as the wrap-around 0 - x; C# has no unary minus for ulong at all and widens
uint/ushort/byte to a signed type, so NumericTypeTemplate emits the unsigned form as that
subtraction under unchecked — exactly Go’s semantics — so a generic over ~uint64 instantiated with a named
unsigned type (internal/trace’s dataTable[EI ~uint64, E] over type stringID uint64) satisfies
the constraint instead of failing CS0315:
// generated for `type counter uint64`
public static counter operator -(counter value) => (counter)unchecked((uint64)((uint64)0 - value.m_value));
The list is emitted in THREE places that must stay in sync: the converter’s getLiftedConstraints
(constraintOperations.go), the go2cs-gen InterfaceTypeTemplate “Arithmetic” constraint list, and
InheritedTypeTemplate’s NumericInterfaces declaration list (whose operator bodies come from
NumericTypeTemplate). Guarded by GenericNegation, which negates across the primitive widths and
through named types over both a signed (~int32) and an unsigned (~uint64) underlying type,
output-compared against Go so the unsigned wrap is verified rather than merely compiled.
uintptr as a generic numeric type argument
The golib uintptr struct declares the full generic-math interface set the lifted numeric constraints demand (IAdditionOperators through IComparisonOperators, IShiftOperators<uintptr, int, uintptr> with a >>> operator, IIncrementOperators/IDecrementOperators) – matching operators alone never satisfy a C# where-clause (CS0315 at reflect’s rangeNum<uintptr, uint64>). At runtime, ConvertToType/ConvertToUInt64 have uintptr fast paths. Guarded by GenericTypeInference (growShrink[U ~uint32 | ~uintptr]).
A NAMED numeric type argument (type small uint8, type celsius float64) matches no rung of golib’s typeof(T) ladder, so ConvertToType, ConvertToUInt64, ConvertToFloat64 and ConvertToFloat32 end in IGoNumericWrapper<T>, which go2cs-gen implements explicitly on every real (not complex) /*num:…*/ wrapper. Each member converts through the same ladder for the wrapper’s own kind and wraps or unwraps the result, so conv[small](300) is 44 as Go’s truncation gives:
small global::go.golib.IGoNumericWrapper<small>.FromUInt64(ulong value) => new(global::go.builtin.ConvertToType<uint8>(value));
ulong global::go.golib.IGoNumericWrapper<small>.ToUInt64(small value) => global::go.builtin.ConvertToUInt64(value.m_value);
Each member takes its operand as an argument, so golib calls it on one cached default of the wrapper and a conversion boxes nothing. A float kind converts an integer by the source’s signedness, as Go’s float64(x) does: ConvertToType(long) and ConvertToType(ulong) reach FromInt64 and FromUInt64.
The fallback used to be reflection over the wrapper’s Value and one-argument constructor, through System.Convert, which is checked: an out-of-range named conversion threw OverflowException, a named uintptr had no conversion, and every converted type parameter flowing in raised IL2091 for the DynamicallyAccessedMembers annotation the reflection needed. Guarded by NamedNumericOperatorConstraint, GenTests’ and GolibTests’ NumericWrapperConversionTests.
Generated
/*num:**/wrapper structs declare the generic-math interfaces (InheritedTypeTemplate’sNumericInterfaces, plus the integer-only three; see An integer named-numeric wrapper implements the integer operator interfaces above), so a NAMED numeric wrapper satisfies a union-generic constraint as a type argument.NamedNumericOperatorConstraintinstantiates one with nameduint64andint32types.
Union-constrained sub-slices ARE the type parameter
A sub-slice of a string | []byte union-constrained value is typed by Go as the type parameter again, so it assigns back to, passes as, and returns as the parameter (time format_rfc3339). The emission is the bare C# range expression:
return parse(s[0..2]) + parse(s[3..5]);
s = s[19..]; // Go: s = s[19:]
Nothing converts it, because nothing needs to: the constraint is the self-referential IByteSeq<T, byte> (above), whose TSelf this[Range] indexer returns T itself, so the range expression already has the type parameter’s type. All four bound shapes take this route — s[..hi], s[lo..hi], s[lo..] and s[..] — and a three-index slice cannot occur on a string-including union (Go forbids it on strings), so Slice3 never reaches it. Downstream members bind through the constraint on the resulting value directly: s[i..j].ToGoString() for Go’s string(s[i:j]) (bytealg’s Rabin-Karp), src[lo..hi].ꓸꓸꓸ for the variadic spread (below).
Func-literal parameters typed as the union type parameter render as the parameter itself (the enclosing method’s type parameter is in scope inside a lambda or local function), matching the Go; digitSum’s literal emits as a local function:
[GoFunc("digitSum[...].func1")] nint parse(T part) {
Guarded by StringByteUnionConstraint — trimHead/headSum (assigned back, passed on, returned), digitSum (both bounds, through a func-literal parameter), prefixMatch (low omitted) and wholeSpan (both omitted), output-compared against go run.
Spreading a union-constrained value
A union-constrained value may also be spread into a variadic — encoding/json’s appendString[Bytes []byte | string] does append(dst, src[lo:hi]...) (and the open-ended append(dst, src[lo:]...)). The sub-slice is typed as the type parameter again (above), so the spread renders as src[lo..hi].ꓸꓸꓸ. A bare type-parameter value has no members of its own, so the spread ꓸꓸꓸ (which yields the Span<byte> the append<T>(slice<T>, params Span<T>) overload binds) must be declared on the constraint interface — a member access on a constrained type-parameter value resolves through its constraint. IByteSeq<T> therefore exposes Span<T> ꓸꓸꓸ { get; }; both implementers already satisfy it (slice<T> as Span<T>, @string as Span<byte>), so the interface member is implicit and adds no cast (CS1061 otherwise — the type parameter Bytes had no ꓸꓸꓸ). (Guarded by the StringByteUnionConstraint extension appendRun — a bounded and an open-ended sub-slice of the union value spread into append, both instantiations value-compared vs Go.)
The same spread of a defined string’s own value, outside any type parameter, renders as p.ꓸꓸꓸ on the go2cs-gen wrapper, so the wrapper declares that member public, as @string and a named slice’s wrapper do; it still implements IByteSeq<byte>’s (CS1061 otherwise — an explicit-only member does not bind at the call site):
type Path string
func (p Path) appendTo(buf []byte) []byte {
return append(buf, p...)
}
partial struct Path /*@string*/;
…
internal static slice<byte> appendTo(this Path p, slice<byte> buf) {
return append(buf, p.ꓸꓸꓸ);
}
(Guarded by the NamedStringSpread row — a defined string spread directly, through a method, a range variable, a zero value and a ~string type parameter, output-compared vs go run — and by GenTests NamedStringSpreadTests.)
An unnamed array type argument carries its length through an array-dims companion
C# spells every Go [N]E as the one array<E>, so a type argument alone cannot say how long an
UNNAMED array is: the length is instance state. Most code never needs it, because a value carries it.
A generic that builds a type DESCRIPTOR from its type argument alone does need it — unique.Make
calls abi.TypeFor[T]() and reads the descriptor’s size first, and a [2]string descriptor with no
length has size zero, which sends every pair of values to Make’s zero-size arm and gives them one
handle. The converter therefore adds a companion type parameter Tᴀ : IGoArrayDims that carries
the dims from the call site to the type-only descriptor.
Go:
func wrapMake[T comparable](v T) unique.Handle[T] {
return unique.Make(v)
}
ab := unique.Make([2]string{"a", "b"})
abc := unique.Make([3]string{"a", "b", "c"})
C# emitted today. A call site whose type argument is an unnamed array supplies a phantom class
named for its dims (ᴀ2 for [2], ᴀ2_3 for [2][3]), declared once per package side beside the
first function that uses it; an unexported generic whose type parameter reaches the descriptor carries
the companion and forwards it:
internal static unique.Handle<T> wrapMake<T, Tᴀ>(T v)
where Tᴀ : IGoArrayDims
{
return unique.Make<T, Tᴀ>(v);
}
private sealed class ᴀ2 : IGoArrayDims { public static nint[] Dims { get; } = [2]; }
var ab = unique.Make<array<@string>, ᴀ2>(array<@string>(["a", "b"]));
var ac = unique.Make<array<@string>, ᴀ2>(array<@string>(["a", "c"]));
var ab2 = unique.Make<array<@string>, ᴀ2>(array<@string>(["a", "b"]));
var abc = unique.Make<array<@string>, ᴀ3>(array<@string>(["a", "b", "c"]));
An EXPORTED generic keeps its Go signature and gains an overload. A caller in another assembly
may not know about the companion, so an exported function that needs it emits two methods: the
arity-1 method Go declares, now a one-line forwarder passing golib’s ᴀnone (no dims), and the
arity-2 method carrying the body, hidden from IntelliSense:
public static Handle<T> Make<T>(T value) => Make<T, ᴀnone>(value);
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] public static Handle<T> Make<T, Tᴀ>(T value)
where Tᴀ : IGoArrayDims
{
…
// Find the map for type T.
var typ = abi.TypeFor<T, Tᴀ>();
The package publishes which functions have the overload, and at which type-parameter index, as an
assembly record in its package_info.cs; a consumer reads the record and calls the arity-2 method only
when it has an unnamed array type argument (or a companion of its own to forward), and the arity-1
method otherwise:
[assembly: GoArrayDimsCompanion("Make", 0)]
fmt.Println("foreign Same:", wrap.Same<array<@string>, ᴀ2>(array<@string>(["k", "l"]), array<@string>(["k", "l"])), wrap.Same<array<@string>, ᴀ2>(array<@string>(["k", "l"]), array<@string>(["k", "m"])));
fmt.Println("foreign non-array:", wrap.Same(strˢ, strˢ), wrap.Same<nint>(1, 2));
(strˢ is a hoisted string literal: private static readonly @string strˢ = "str"u8;.)
Why. The need starts at a sink — a function that builds a descriptor from a type argument
alone. The hand-owned sink is abi.TypeFor<T, Tᴀ>() (internal/abi/type_impl.cs), which answers the
descriptor a live [N]E value answers: the same box for equal dims, whichever assembly declared the
phantom, so unique.Make keys one map per array type. The need propagates backwards through every
generic whose type parameter reaches a sink, inside the package and across packages through the
records. Identity is the property: two equal [2]string values from two assemblies must reach one
handle. A pointer to an array, a struct holding an array and a DEFINED array type need no companion —
their own C# types carry the length (pair above is partial struct pair /*[2]@string*/).
Limits. Scope is abi.TypeFor and its consumers (unique.Make); reflect.TypeFor is not a sink
yet. An exported function whose body the converter does not emit (a hand-owned or manually converted
function) never gets an overload, and a function value of an exported generic with no unnamed array
argument binds the arity-1 method. A -tests conversion takes production’s records for production’s
exported functions, so a test file cannot change which production functions are overloaded. Dims for a
non-array type argument panic by name (abi.TypeFor: array dims [2] for …, which is not an array).
Guarded by: UnnamedArrayTypeArgDescriptor (direct, method value, local forwarder, foreign
Wrap/Same, pointer, struct and defined-array controls, output-compared against go run);
GolibTests.AbiArrayDimsCompanionTests (descriptor identity against a live value, a different length
is a different box, equal dims from two phantoms reach one box, ᴀnone moves nothing, the non-array
refusal, unique.Make keyed on the value); the converter unit tests
TestArrayDimsOverloadForAnExportedSinkReachingGeneric, TestArrayDimsOverloadNeedsTheRecord and
TestArrayDimsOverloadForwarderLine.
A zero value an array type argument would leave short panics by name, or is warned at conversion
The array-dims companion reaches only an unexported generic FUNCTION. A method of a generic type takes
no companion (its type parameters are the receiver’s, and a method’s arity is what interfaces and
method values bind), and the companion sizes a bare T, not a struct that holds one. Where such a zero
value is built, the converter emits golib’s builtin.GoZero<T>() (@new<T>() for new(T)), the same
line it emits for any type parameter’s zero. Both are the plain zero for every type argument but
array<E>, where they panic by name, since a length-0 array would be a wrong value no later code can
tell from a right one. The test folds away for every other type argument, and a type parameter whose constraint
admits no unnamed array (one with a method, or a union with no array term, such as a numeric constraint) is not tested
at all: a struct holding it and a map read of it keep the plain form. A struct that holds type
parameters by value reads builtin.GoZero<S, T1, …>() (@new<S, T1, …>()), which passes the held
parameters after its own type, at most three, and tests each the same way.
Go:
func (t *Table[K, T]) Zero() T {
var z T
return z
}
func (t *Table[K, T]) Get(k K) (T, bool) {
v, ok := t.m[k]
return v, ok
}
func (t *Table[K, T]) GetCell(k K) (T, bool) { // cells map[K]Cell[T]; type Cell[T any] struct{ V T }
c, ok := t.cells[k]
return c.V, ok
}
func (t *Table[K, T]) New() *T {
return new(T)
}
func (t *Table[K, T]) Store(v T) *T {
p := new(T)
*p = v
return p
}
func (t *Table[K, T]) AddrZero() T {
var z T
p := &z
return *p
}
func (t *Table[K, T]) Has(k K) bool {
_, ok := t.m[k]
return ok
}
C# emitted today:
public static T Zero<K, T>(this ref Table<K, T> t) {
T z = builtin.GoZero<T>();
return z;
}
…
public static (T, bool) Get<K, T>(this ref Table<K, T> t, K k) {
var (v, ok) = t.m[k, builtin.GoZero<T>, ꟷ];
return (v.ΔCopy(), ok);
}
…
public static (T, bool) GetCell<K, T>(this ref Table<K, T> t, K k) {
var (c, ok) = t.cells[k, builtin.GoZero<Cell<T>, T>, ꟷ];
return (c.V.ΔCopy(), ok);
}
…
public static ж<T> New<K, T>(this ref Table<K, T> t) {
return @new<T>();
}
public static ж<T> Store<K, T>(this ref Table<K, T> t, T v) {
v = v.ΔCopy();
var p = Ꮡ(v);
return p;
}
public static T AddrZero<K, T>(this ref Table<K, T> t) {
ref var z = ref heap(builtin.GoZero<T>(), out var Ꮡz);
var p = Ꮡz;
return p.ValueSlot.ΔCopy();
}
…
public static bool Has<K, T>(this ref Table<K, T> t, K k) {
var (_, ok) = t.m[k, ꟷ];
return ok;
}
An address-taken zero (var z T; p := &z, or a named result a closure addresses) boxes the same zero:
heap(builtin.GoZero<T>(), out var Ꮡz), or heap(GoArrayZero.Of<T, TDims>(), …) where a companion
sizes it.
In an unexported generic FUNCTION the companion sizes these zeros instead, and nothing is tested: var z T,
new(T), a map read of T, and a result named in the signature (a blank _ included), which Go declares at its zero before the body runs
whether or not the body assigns it, each give the function the companion and read GoArrayZero.Of<T, Tᴀ>():
internal static T /*r*/ named<T, Tᴀ>()
where Tᴀ : IGoArrayDims
{
T r = GoArrayZero.Of<T, Tᴀ>();
return r;
}
A new(T) whose zero the next statement overwrites, p := new(T); *p = v as in Store, builds no zero at all: the
pair becomes one box built from the stored value, var p = Ꮡ(v);, so it runs for an array type argument as it does in
Go. The pairing needs the store on the very next statement of the same block, a stored value of type T that does not
read p, and a T whose zero would be tested; any other shape keeps @new<T>() and its separate store.
The map read passes builtin.GoZero as its miss factory, called only on a miss, because the map’s own
miss zero must not panic: golib’s map, channel and type-assertion zeros read an internal unchecked form.
A comma-ok read whose value a miss never shows keeps the plain read: a blank value, as in Has; if v, ok := m[k]; ok { … }
or ok && … with no else that reads v; and if v, ok := m[k]; !ok || … { … } whose body does not read v, the
form maps.Equal and maps.EqualFunc use (every later operand of such a chain runs only on a hit).
A concrete instantiation is warned. Outside generic code (var c Cell[[2]int]) no type parameter is
left to test when the program runs, so the converter reports warning GO2CS2019 at each instantiation
site of a generic struct that holds its type parameter by value with an unnamed array type argument.
A named array type argument converts to a struct that knows its own length and is not warned; neither is
a pointer or slice type argument, nor a struct that holds the parameter only through a pointer.
Limits. A zero that Go builds and never shows refuses too: a var zero T returned only on one branch (a stack’s
Pop, whose other branch returns an element) refuses on every call, and named results that a map hit overwrites
refuse on the hit. Refusing where the zero is read instead was measured and declined: it puts a test on every length
read of every array. Not covered: a receive from a closed channel, a failed type assertion, a composite literal
that leaves a type-parameter field out, and the elements of make([]T, n) and of an array of T (var a [2]T), in
generic code (a slice or array whose elements are all assigned before they are read is right); and a generic struct holding both its type
parameter and an array field of its own (it is built through its constructor before the zero check is
reached). A [0]E type argument panics too, where Go’s empty array is right: [0]E and [2]E are one
C# type argument, array<E>, so no zero site can tell them apart. A copy of a concrete instantiation holding an
array does not build (ΔClone is not defined on the generic struct). See
Known issues.
Guarded by: ArrayTypeArgZeroRefusals (a method’s var zero, map miss, struct-of-T map miss, named
result, blank named result, new(T), address-taken var zero, named result a deferred closure addresses, and a struct zero
each panic by name, and so do a non-empty stack’s pop and a cache hit, whose zeros Go never shows (pinned as a limit);
a new(T) the next statement overwrites runs; an unexported generic function’s address-taken var zero is sized by its companion;
a hit, a blank miss, an ok-guarded miss, slice and int arguments run; the element length of make([]T, n) and of
[2]T is pinned at 0 as a limit, and so are iter.Pull and iter.Pull2 over an array element, which refuse;
maps.Equal over array values runs; exit and stdout compared against an expected output); the converter unit test
TestArrayTypeArgZero (the emitted forms, the two exempt reads, exactly two GO2CS2019 warnings at the
two unnamed-array sites) and TestUnsizedNewStore (the paired box, and the shapes that keep @new<T>());
GolibTests.GoZeroArrayRefusalTests (every overload, array and non-array, and the unchecked
zero golib’s map reads); GenericArrayTypeArgZero (an unexported generic function’s named result, read unassigned and
assigned, and its blank named result, sized by its companion; non-generic blank results of an array and of a struct
holding one; maps.Equal over array values on a miss and a hit; output-compared against go run).
A blank type parameter is named _ΔtN
Go lets a type parameter be the blank identifier wherever one is declared: on a generic function, on a generic type,
and on a method’s receiver, which need not name the receiver type’s arguments. C# has no blank type parameter. Emitted
as _, the name collides with a blank value parameter beside it, repeats in an all-blank list, and turns every
_ = x discard in the body into an assignment to a type. Each blank type parameter is therefore renamed _ΔtN,
N being its position in its own list. A blank type parameter is never referenced, so the name only has to be
distinct:
func (b Box[_]) Kind(_ int) string { return "box" }
func (p Pair[_, V]) Value() V { return p.v }
func pair[_, _ any]() string { return "pair" }
type Tag[_ any] struct{ name string }
public static @string Kind<_Δt0>(this Box<_Δt0> b, nint _) { return "box"; }
public static V Value<_Δt0, V>(this Pair<_Δt0, V> p) { return p.v.ΔCopy(); }
internal static @string pair<_Δt0, _Δt1>() { return "pair"; }
partial struct Tag<_Δt0> {
The rename happens where the file is parsed, before go/types runs (nameBlankTypeParams, in the loader’s
ParseFile hook beside the address-of unparenthesizing), so every later pass sees an ordinary named type
parameter. The name cannot collide with a Go identifier: one spelled _ΔtN is refused (GO2CS2015, the
deltaBlankTypeParamPattern shape), and the identifiers the rename itself wrote are exempt from that refusal.
(Guarded by BlankReceiverTypeParam: Box[_] with a blank value parameter, a pointer receiver whose body
discards, Pair[_, _] and Pair[_, V]; and BlankTypeParam: a generic function with a blank and a named type
parameter, an all-blank list, a generic struct and a generic func type; both output-compared against go run.
Converter unit tests TestBlankTypeParamsAreNamedWhereTheFileIsParsed and
TestBlankTypeParamNamesAreRefusedOnlyInSource.)