CodeSampleX

Exemplo

golang.org/x/tools v0.1.1: typeutil.Map

Amostra verificada para golang golang.org/x/tools v0.1.1: typeutil.Map. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:c8ba519391ac875802a9588c8086a43c39655f5f3e756cd687422cb336c55325

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-10-13

Caso

HOW
Objetivo
verify golang.org/x/tools/go/types/typeutil.Map in pkg:golang/golang.org/x/tools@v0.1.1
Pacotes
Símbolos
  • golang.org/x/tools/go/types/typeutil.Map
Criado
2026-10-13T19:45:51Z

Contrato

  1. typeutil.Map maps types.Type keys using types.Identical equivalence for structurally identical types
  2. typeutil.Map.Set stores values and returns previous value when overwriting an existing key
  3. typeutil.Map.At returns nil for absent keys and the associated value for present keys
  4. typeutil.Map.Delete removes entries by type equivalence and reports true if present
  5. typeutil.Map.Iterate visits all map entries and Len reflects the number of entries

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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.Map in pkg:golang/golang.org/x/tools@v0.1.1
Kind: HOW

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

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:b4b0afd8420f1a45d01ebaae3394ca5b33039b259b0702a64afd913c94005aae","contract":["typeutil.Map maps types.Type keys using types.Identical equivalence for structurally identical types","typeutil.Map.Set stores values and returns previous value when overwriting an existing key","typeutil.Map.At returns nil for absent keys and the associated value for present keys","typeutil.Map.Delete removes entries by type equivalence and reports true if present","typeutil.Map.Iterate visits all map entries and Len reflects the number of entries"],"goal":"verify golang.org/x/tools/go/types/typeutil.Map in pkg:golang/golang.org/x/tools@v0.1.1","kind":"HOW","packages":["pkg:golang/golang.org/x/tools@v0.1.1"],"schemaVersion":1,"symbols":["golang.org/x/tools/go/types/typeutil.Map"]},"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.1"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.1.1","symbols":["golang.org/x/tools/go/types/typeutil.Map"],"verifierAdapter":"golang@1"}
go.mod
module example.com/typeutil-map

go 1.21

require golang.org/x/tools v0.1.1
go.sum
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/tools/go/types/typeutil.Map in pkg:golang/golang.org/x/tools@v0.1.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/tools@v0.1.1"
  ],
  "symbols": [
    "golang.org/x/tools/go/types/typeutil.Map"
  ]
}
test/contract.go
package main

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

	typemap "example.com/typeutil-map"
	"golang.org/x/tools/go/types/typeutil"
)

func main() {
	reg := typemap.NewTypeRegistry()

	// Assertion 1: typeutil.Map maps types.Type keys using types.Identical equivalence for structurally identical types
	{
		slice1 := types.NewSlice(types.Typ[types.Int])
		slice2 := types.NewSlice(types.Typ[types.Int])
		if slice1 == slice2 {
			fmt.Fprintf(os.Stderr, "FAIL 1: expected distinct pointers for independently created slices\n")
			os.Exit(1)
		}

		reg.Set(slice1, "int-slice")
		val := reg.At(slice2)
		if val != "int-slice" {
			fmt.Fprintf(os.Stderr, "FAIL 1: expected identical slice lookup to yield 'int-slice', got %v\n", val)
			os.Exit(1)
		}

		field1 := types.NewField(0, nil, "FieldA", types.Typ[types.String], false)
		field2 := types.NewField(0, nil, "FieldA", types.Typ[types.String], false)
		struct1 := types.NewStruct([]*types.Var{field1}, nil)
		struct2 := types.NewStruct([]*types.Var{field2}, nil)
		if struct1 == struct2 {
			fmt.Fprintf(os.Stderr, "FAIL 1: expected distinct pointers for structs\n")
			os.Exit(1)
		}

		reg.Set(struct1, "struct-val")
		if reg.At(struct2) != "struct-val" {
			fmt.Fprintf(os.Stderr, "FAIL 1: expected struct lookup via structural equivalence to succeed\n")
			os.Exit(1)
		}
	}

	// Assertion 2: typeutil.Map.Set stores values and returns previous value when overwriting an existing key
	{
		sliceKey := types.NewSlice(types.Typ[types.Int])
		prev := reg.Set(sliceKey, "updated-slice")
		if prev != "int-slice" {
			fmt.Fprintf(os.Stderr, "FAIL 2: expected previous value 'int-slice', got %v\n", prev)
			os.Exit(1)
		}
		if reg.At(sliceKey) != "updated-slice" {
			fmt.Fprintf(os.Stderr, "FAIL 2: expected current value to be 'updated-slice'\n")
			os.Exit(1)
		}
	}

	// Assertion 3: typeutil.Map.At returns nil for absent keys and the associated value for present keys
	{
		absentKey := types.Typ[types.String]
		if reg.At(absentKey) != nil {
			fmt.Fprintf(os.Stderr, "FAIL 3: expected nil for absent key, got %v\n", reg.At(absentKey))
			os.Exit(1)
		}
		presentKey := types.NewSlice(types.Typ[types.Int])
		if reg.At(presentKey) != "updated-slice" {
			fmt.Fprintf(os.Stderr, "FAIL 3: expected 'updated-slice' for present key\n")
			os.Exit(1)
		}
	}

	// Assertion 4: typeutil.Map.Delete removes entries by type equivalence and reports true if present
	{
		sliceLookup := types.NewSlice(types.Typ[types.Int])
		deleted := reg.Delete(sliceLookup)
		if !deleted {
			fmt.Fprintf(os.Stderr, "FAIL 4: expected Delete to return true for existing key\n")
			os.Exit(1)
		}
		if reg.At(sliceLookup) != nil {
			fmt.Fprintf(os.Stderr, "FAIL 4: expected At to return nil after deletion\n")
			os.Exit(1)
		}
		if reg.Delete(sliceLookup) {
			fmt.Fprintf(os.Stderr, "FAIL 4: expected Delete to return false when key already removed\n")
			os.Exit(1)
		}
	}

	// Assertion 5: typeutil.Map.Iterate visits all map entries and Len reflects the number of entries
	{
		var m typeutil.Map
		m.Set(types.Typ[types.Bool], "bool-val")
		m.Set(types.Typ[types.Float64], "float-val")
		m.Set(types.NewPointer(types.Typ[types.Int]), "ptr-val")

		if m.Len() != 3 {
			fmt.Fprintf(os.Stderr, "FAIL 5: expected Len() == 3, got %d\n", m.Len())
			os.Exit(1)
		}

		visited := make(map[string]interface{})
		m.Iterate(func(key types.Type, value interface{}) {
			visited[key.String()] = value
		})

		if len(visited) != 3 {
			fmt.Fprintf(os.Stderr, "FAIL 5: expected 3 visited entries, got %d\n", len(visited))
			os.Exit(1)
		}
		if visited["bool"] != "bool-val" || visited["float64"] != "float-val" || visited["*int"] != "ptr-val" {
			fmt.Fprintf(os.Stderr, "FAIL 5: visited entries do not match expected contents: %v\n", visited)
			os.Exit(1)
		}

		m.Delete(types.Typ[types.Bool])
		if m.Len() != 2 {
			fmt.Fprintf(os.Stderr, "FAIL 5: expected Len() == 2 after deletion, got %d\n", m.Len())
			os.Exit(1)
		}
	}

	fmt.Println("PASS: all typeutil.Map contract assertions verified")
}
typemap.go
package typemap

import (
	"go/types"

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

// TypeRegistry associates Go types with arbitrary metadata values using typeutil.Map.
type TypeRegistry struct {
	mapping typeutil.Map
}

// NewTypeRegistry creates an empty TypeRegistry.
func NewTypeRegistry() *TypeRegistry {
	return &TypeRegistry{}
}

// Set associates the given type key with value, returning the previous value if present.
func (r *TypeRegistry) Set(k types.Type, value interface{}) interface{} {
	return r.mapping.Set(k, value)
}

// At retrieves the value associated with the given type key, or nil if not found.
func (r *TypeRegistry) At(k types.Type) interface{} {
	return r.mapping.At(k)
}

// Delete removes the entry for the given type key and reports whether it existed.
func (r *TypeRegistry) Delete(k types.Type) bool {
	return r.mapping.Delete(k)
}

// Len returns the count of active entries in the registry.
func (r *TypeRegistry) Len() int {
	return r.mapping.Len()
}

// Keys returns all keys present in the registry.
func (r *TypeRegistry) Keys() []types.Type {
	return r.mapping.Keys()
}

// Iterate iterates over all key-value pairs in the registry.
func (r *TypeRegistry) Iterate(f func(key types.Type, value interface{})) {
	r.mapping.Iterate(f)
}

Seeder de origem

anônimo