CodeSampleX

サンプル

golang.org/x/tools v0.1.12: typeutil.MethodSetCache

検証済みサンプル — golang golang.org/x/tools v0.1.12: typeutil.MethodSetCache. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:58a7af4bca7be2e191388e8b26691ffe13a6183cfb0dae146d9e1788ea1aa30d

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-04

ケース

HOW
ゴール
verify golang.org/x/tools/go/types/typeutil.MethodSetCache in pkg:golang/golang.org/x/tools@v0.1.12
パッケージ
シンボル
  • golang.org/x/tools/go/types/typeutil.MethodSetCache
作成日
2026-09-04T13:03:50Z

コントラクト

  1. MethodSetCache zero value is ready to use and returns the method set of a named type
  2. MethodSetCache returns identical cached *types.MethodSet pointer on repeated queries for the same type
  3. MethodSetCache caches method sets for both value receiver and pointer receiver of a named type
  4. MethodSetCache returns the same cached method set for distinct Pointer type instances with the same underlying named type
  5. MethodSetCache with nil receiver behaves like types.NewMethodSet without panicking
  6. MethodSetCache caches method sets for non-named types such as interface and struct types
  7. MethodSetCache is safe for concurrent queries across multiple goroutines

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify golang.org/x/tools/go/types/typeutil.MethodSetCache in pkg:golang/golang.org/x/tools@v0.1.12
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/tools@v0.1.12
Demonstrate these symbols/APIs:
  - golang.org/x/tools/go/types/typeutil.MethodSetCache

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:49116522a97bdadac7cb33b4bcd9528f9572de87f4d95057ff239f10eab18ffd","contract":["MethodSetCache zero value is ready to use and returns the method set of a named type","MethodSetCache returns identical cached *types.MethodSet pointer on repeated queries for the same type","MethodSetCache caches method sets for both value receiver and pointer receiver of a named type","MethodSetCache returns the same cached method set for distinct Pointer type instances with the same underlying named type","MethodSetCache with nil receiver behaves like types.NewMethodSet without panicking","MethodSetCache caches method sets for non-named types such as interface and struct types","MethodSetCache is safe for concurrent queries across multiple goroutines"],"goal":"verify golang.org/x/tools/go/types/typeutil.MethodSetCache in pkg:golang/golang.org/x/tools@v0.1.12","kind":"HOW","packages":["pkg:golang/golang.org/x/tools@v0.1.12"],"schemaVersion":1,"symbols":["golang.org/x/tools/go/types/typeutil.MethodSetCache"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/tools@v0.1.12"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.1.12","symbols":["golang.org/x/tools/go/types/typeutil.MethodSetCache"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.23.0

require golang.org/x/tools v0.1.12
go.sum
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
main.go
package main

import (
	"fmt"
	"go/token"
	"go/types"

	"golang.org/x/tools/go/types/typeutil"
)

func main() {
	// Create a named type with methods: type Greeter struct {}
	pkg := types.NewPackage("example.com/demo", "demo")
	typeName := types.NewTypeName(token.NoPos, pkg, "Greeter", nil)
	named := types.NewNamed(typeName, types.NewStruct(nil, nil), nil)

	// Add a value receiver method: func (Greeter) Greet() string
	sigVal := types.NewSignature(types.NewVar(token.NoPos, pkg, "recv", named), nil, nil, false)
	named.AddMethod(types.NewFunc(token.NoPos, pkg, "Greet", sigVal))

	// Add a pointer receiver method: func (*Greeter) Reset()
	ptrType := types.NewPointer(named)
	sigPtr := types.NewSignature(types.NewVar(token.NoPos, pkg, "recv", ptrType), nil, nil, false)
	named.AddMethod(types.NewFunc(token.NoPos, pkg, "Reset", sigPtr))

	// Zero-value cache is ready to use
	var cache typeutil.MethodSetCache

	// Query value method set
	valMset := cache.MethodSet(named)
	fmt.Printf("Greeter value method set count: %d\n", valMset.Len())
	for i := 0; i < valMset.Len(); i++ {
		fmt.Printf("  method: %s\n", valMset.At(i).Obj().Name())
	}

	// Query pointer method set
	ptrMset := cache.MethodSet(ptrType)
	fmt.Printf("Greeter pointer method set count: %d\n", ptrMset.Len())
	for i := 0; i < ptrMset.Len(); i++ {
		fmt.Printf("  method: %s\n", ptrMset.At(i).Obj().Name())
	}

	// Cached repeat query returns identical pointer
	repeatMset := cache.MethodSet(named)
	fmt.Printf("Repeat query cached: %t\n", valMset == repeatMset)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/tools/go/types/typeutil.MethodSetCache in pkg:golang/golang.org/x/tools@v0.1.12",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/tools@v0.1.12"
  ],
  "symbols": [
    "golang.org/x/tools/go/types/typeutil.MethodSetCache"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"go/token"
	"go/types"
	"os"
	"sync"

	"golang.org/x/tools/go/types/typeutil"
)

func main() {
	pkg := types.NewPackage("example.com/test", "test")

	// Helper to build a named type with value and pointer methods
	buildNamedType := func() (*types.Named, *types.Pointer) {
		typeName := types.NewTypeName(token.NoPos, pkg, "Item", nil)
		named := types.NewNamed(typeName, types.NewStruct(nil, nil), nil)

		sigVal := types.NewSignature(types.NewVar(token.NoPos, pkg, "recv", named), nil, nil, false)
		named.AddMethod(types.NewFunc(token.NoPos, pkg, "ValueMethod", sigVal))

		ptr := types.NewPointer(named)
		sigPtr := types.NewSignature(types.NewVar(token.NoPos, pkg, "recv", ptr), nil, nil, false)
		named.AddMethod(types.NewFunc(token.NoPos, pkg, "PointerMethod", sigPtr))

		return named, ptr
	}

	named, ptr := buildNamedType()

	// Assertion 1: MethodSetCache zero value is ready to use and returns the method set of a named type
	{
		var cache typeutil.MethodSetCache
		mset := cache.MethodSet(named)
		if mset == nil {
			fmt.Fprintf(os.Stderr, "FAIL: MethodSet returned nil\n")
			os.Exit(1)
		}
		if mset.Len() != 1 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 1 value method, got %d\n", mset.Len())
			os.Exit(1)
		}
		if mset.At(0).Obj().Name() != "ValueMethod" {
			fmt.Fprintf(os.Stderr, "FAIL: expected 'ValueMethod', got %q\n", mset.At(0).Obj().Name())
			os.Exit(1)
		}
	}

	// Assertion 2: MethodSetCache returns identical cached *types.MethodSet pointer on repeated queries for the same type
	{
		var cache typeutil.MethodSetCache
		first := cache.MethodSet(named)
		second := cache.MethodSet(named)
		if first != second {
			fmt.Fprintf(os.Stderr, "FAIL: repeated queries did not return identical pointer\n")
			os.Exit(1)
		}
	}

	// Assertion 3: MethodSetCache caches method sets for both value receiver and pointer receiver of a named type
	{
		var cache typeutil.MethodSetCache
		valMset := cache.MethodSet(named)
		ptrMset := cache.MethodSet(ptr)
		if valMset.Len() != 1 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 1 method on value, got %d\n", valMset.Len())
			os.Exit(1)
		}
		if ptrMset.Len() != 2 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 2 methods on pointer, got %d\n", ptrMset.Len())
			os.Exit(1)
		}
		ptrMset2 := cache.MethodSet(ptr)
		if ptrMset != ptrMset2 {
			fmt.Fprintf(os.Stderr, "FAIL: repeated pointer query did not return identical pointer\n")
			os.Exit(1)
		}
	}

	// Assertion 4: MethodSetCache returns the same cached method set for distinct Pointer type instances with the same underlying named type
	{
		var cache typeutil.MethodSetCache
		ptr1 := types.NewPointer(named)
		ptr2 := types.NewPointer(named)
		mset1 := cache.MethodSet(ptr1)
		mset2 := cache.MethodSet(ptr2)
		if mset1 != mset2 {
			fmt.Fprintf(os.Stderr, "FAIL: distinct pointer instances with same underlying named type got different method sets\n")
			os.Exit(1)
		}
	}

	// Assertion 5: MethodSetCache with nil receiver behaves like types.NewMethodSet without panicking
	{
		var nilCache *typeutil.MethodSetCache
		mset := nilCache.MethodSet(named)
		if mset == nil || mset.Len() != 1 {
			fmt.Fprintf(os.Stderr, "FAIL: nil receiver did not behave like types.NewMethodSet\n")
			os.Exit(1)
		}
	}

	// Assertion 6: MethodSetCache caches method sets for non-named types such as interface and struct types
	{
		var cache typeutil.MethodSetCache
		fnSig := types.NewSignature(nil, nil, nil, false)
		ifaceMethod := types.NewFunc(token.NoPos, pkg, "Close", fnSig)
		iface := types.NewInterfaceType([]*types.Func{ifaceMethod}, nil)
		iface.Complete()

		msetIface1 := cache.MethodSet(iface)
		if msetIface1 == nil || msetIface1.Len() != 1 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 1 interface method, got %v\n", msetIface1)
			os.Exit(1)
		}
		msetIface2 := cache.MethodSet(iface)
		if msetIface1 != msetIface2 {
			fmt.Fprintf(os.Stderr, "FAIL: repeated interface query did not return identical pointer\n")
			os.Exit(1)
		}
	}

	// Assertion 7: MethodSetCache is safe for concurrent queries across multiple goroutines
	{
		var cache typeutil.MethodSetCache
		var wg sync.WaitGroup
		errCh := make(chan error, 50)

		for i := 0; i < 20; i++ {
			wg.Add(1)
			go func() {
				defer wg.Done()
				for j := 0; j < 100; j++ {
					v := cache.MethodSet(named)
					p := cache.MethodSet(ptr)
					if v == nil || v.Len() != 1 {
						select {
						case errCh <- fmt.Errorf("concurrent value method set corrupted"):
						default:
						}
					}
					if p == nil || p.Len() != 2 {
						select {
						case errCh <- fmt.Errorf("concurrent pointer method set corrupted"):
						default:
						}
					}
				}
			}()
		}
		wg.Wait()
		close(errCh)

		if err, ok := <-errCh; ok {
			fmt.Fprintf(os.Stderr, "FAIL: %v\n", err)
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed.")
}

オリジンシーダー

匿名