CodeSampleX

Sample

golang.org/x/tools v0.6.0: cfg.Block

Verified sample for golang golang.org/x/tools v0.6.0: cfg.Block. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:dc3d093433fedb006ed1dbb011e2ce2ab2ff2b0b41840620e667cf80d18587a7

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go

Verification-run environments

Environment Contract Stages Run
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-16

Case

HOW
Goal
verify golang.org/x/tools/go/cfg.Block in pkg:golang/golang.org/x/tools@v0.6.0
Packages
Symbols
  • golang.org/x/tools/go/cfg.Block
Created
2026-09-16T11:55:28Z

Contract

  1. cfg.Block represents basic blocks in a control-flow graph with Index, Live, Nodes, and Succs properties
  2. cfg.Block.Return returns the terminating ReturnStmt for return blocks and nil for non-return blocks
  3. cfg.Block.String returns a human-readable string representation of the block
  4. cfg.Block identifies unreachable code blocks with Live set to false
  5. cfg.Block distinguishes branching blocks with two successors from sequential blocks with a single successor

Files

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

Download the source artifact (tar.gz)

Source

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

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/tools@v0.6.0
Demonstrate these symbols/APIs:
  - golang.org/x/tools/go/cfg.Block

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:0b6ef63231341313a71b7b23fb5e3c8f6be8d95162c0e4dc38dfa32a30ad1d10","contract":["cfg.Block represents basic blocks in a control-flow graph with Index, Live, Nodes, and Succs properties","cfg.Block.Return returns the terminating ReturnStmt for return blocks and nil for non-return blocks","cfg.Block.String returns a human-readable string representation of the block","cfg.Block identifies unreachable code blocks with Live set to false","cfg.Block distinguishes branching blocks with two successors from sequential blocks with a single successor"],"goal":"verify golang.org/x/tools/go/cfg.Block in pkg:golang/golang.org/x/tools@v0.6.0","kind":"HOW","packages":["pkg:golang/golang.org/x/tools@v0.6.0"],"schemaVersion":1,"symbols":["golang.org/x/tools/go/cfg.Block"]},"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.6.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.6.0","symbols":["golang.org/x/tools/go/cfg.Block"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require golang.org/x/tools v0.6.0
go.sum
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
main.go
package main

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"

	"golang.org/x/tools/go/cfg"
)

func main() {
	src := `package p
func example(x int) int {
	if x > 0 {
		return 1
	}
	return 0
}`
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "p.go", src, 0)
	if err != nil {
		fmt.Printf("parse error: %v\n", err)
		return
	}

	var fn *ast.FuncDecl
	for _, decl := range f.Decls {
		if fd, ok := decl.(*ast.FuncDecl); ok {
			fn = fd
			break
		}
	}

	if fn == nil || fn.Body == nil {
		fmt.Println("no function body found")
		return
	}

	g := cfg.New(fn.Body, func(call *ast.CallExpr) bool { return true })
	fmt.Printf("Constructed CFG with %d blocks\n", len(g.Blocks))
	for _, b := range g.Blocks {
		fmt.Printf("Block %d: live=%v, succs=%d, nodes=%d\n", b.Index, b.Live, len(b.Succs), len(b.Nodes))
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/tools/go/cfg.Block in pkg:golang/golang.org/x/tools@v0.6.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/tools@v0.6.0"
  ],
  "symbols": [
    "golang.org/x/tools/go/cfg.Block"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"os"
	"strings"

	"golang.org/x/tools/go/cfg"
)

func parseFunc(src string) (*ast.FuncDecl, error) {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "src.go", src, 0)
	if err != nil {
		return nil, fmt.Errorf("parse error: %w", err)
	}
	for _, decl := range f.Decls {
		if fd, ok := decl.(*ast.FuncDecl); ok {
			return fd, nil
		}
	}
	return nil, fmt.Errorf("no function declaration found")
}

func main() {
	// Assertion 1: cfg.Block represents basic blocks in a control-flow graph with Index, Live, Nodes, and Succs properties
	{
		src := `package p
func f() {
	x := 10
	_ = x
}`
		fn, err := parseFunc(src)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: %v\n", err)
			os.Exit(1)
		}
		g := cfg.New(fn.Body, func(call *ast.CallExpr) bool { return true })
		if len(g.Blocks) == 0 {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected at least 1 block, got 0\n")
			os.Exit(1)
		}
		entry := g.Blocks[0]
		if entry.Index != 0 {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected entry Index 0, got %d\n", entry.Index)
			os.Exit(1)
		}
		if !entry.Live {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected entry Live true, got false\n")
			os.Exit(1)
		}
		if len(entry.Nodes) == 0 {
			fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected non-empty Nodes in entry block\n")
			os.Exit(1)
		}
	}

	// Assertion 2: cfg.Block.Return returns the terminating ReturnStmt for return blocks and nil for non-return blocks
	{
		src := `package p
func f(x int) int {
	if x > 0 {
		return 42
	}
	return 0
}`
		fn, err := parseFunc(src)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Assertion 2 failed: %v\n", err)
			os.Exit(1)
		}
		g := cfg.New(fn.Body, func(call *ast.CallExpr) bool { return true })

		var foundReturnBlock, foundNonReturnBlock bool
		for _, b := range g.Blocks {
			ret := b.Return()
			if ret != nil {
				foundReturnBlock = true
			} else {
				foundNonReturnBlock = true
			}
		}
		if !foundReturnBlock {
			fmt.Fprintf(os.Stderr, "Assertion 2 failed: expected at least one block with Return() != nil\n")
			os.Exit(1)
		}
		if !foundNonReturnBlock {
			fmt.Fprintf(os.Stderr, "Assertion 2 failed: expected at least one block with Return() == nil\n")
			os.Exit(1)
		}
	}

	// Assertion 3: cfg.Block.String returns a human-readable string representation of the block
	{
		src := `package p
func f() int {
	return 1
}`
		fn, err := parseFunc(src)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: %v\n", err)
			os.Exit(1)
		}
		g := cfg.New(fn.Body, func(call *ast.CallExpr) bool { return true })
		if len(g.Blocks) == 0 {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: no blocks found\n")
			os.Exit(1)
		}
		entryStr := g.Blocks[0].String()
		if !strings.Contains(entryStr, "block 0") && !strings.Contains(entryStr, "entry") {
			fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected String() to contain block info, got %q\n", entryStr)
			os.Exit(1)
		}
	}

	// Assertion 4: cfg.Block identifies unreachable code blocks with Live set to false
	{
		src := `package p
func f() int {
	return 1
	x := 2
	return x
}`
		fn, err := parseFunc(src)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Assertion 4 failed: %v\n", err)
			os.Exit(1)
		}
		g := cfg.New(fn.Body, func(call *ast.CallExpr) bool { return true })
		var foundDeadBlock bool
		for _, b := range g.Blocks {
			if !b.Live {
				foundDeadBlock = true
				break
			}
		}
		if !foundDeadBlock {
			fmt.Fprintf(os.Stderr, "Assertion 4 failed: expected unreachable block with Live == false\n")
			os.Exit(1)
		}
	}

	// Assertion 5: cfg.Block distinguishes branching blocks with two successors from sequential blocks with a single successor
	{
		src := `package p
func f(b bool) {
	if b {
		_ = 1
	}
	_ = 2
}`
		fn, err := parseFunc(src)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Assertion 5 failed: %v\n", err)
			os.Exit(1)
		}
		g := cfg.New(fn.Body, func(call *ast.CallExpr) bool { return true })
		var foundBranchBlock, foundSingleSuccBlock bool
		for _, blk := range g.Blocks {
			if len(blk.Succs) == 2 {
				foundBranchBlock = true
			} else if len(blk.Succs) == 1 {
				foundSingleSuccBlock = true
			}
		}
		if !foundBranchBlock {
			fmt.Fprintf(os.Stderr, "Assertion 5 failed: expected branching block with 2 successors\n")
			os.Exit(1)
		}
		if !foundSingleSuccBlock {
			fmt.Fprintf(os.Stderr, "Assertion 5 failed: expected sequential block with 1 successor\n")
			os.Exit(1)
		}
	}

	fmt.Println("PASS: all cfg.Block contract assertions passed")
}

Origin Seeder

anonymous