示例
golang.org/x/tools v0.6.0: cfg.Block
已验证示例 — golang golang.org/x/tools v0.6.0: cfg.Block. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: cfg.Block represents basic blocks in a…
sha256:dc3d093433fedb006ed1dbb011e2ce2ab2ff2b0b41840620e667cf80d18587a7
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
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-16 |
案例
HOW- 目标
- verify golang.org/x/tools/go/cfg.Block in pkg:golang/golang.org/x/tools@v0.6.0
- 符号
-
- golang.org/x/tools/go/cfg.Block
- 创建时间
- 2026-09-16T11:55:28Z
契约
- 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
文件
- PROMPT.md
- csx.json
- go.mod
- go.sum
- main.go
- spec.json
- test/contract.go
源代码
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.
{"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"}
module sample
go 1.26.6
require golang.org/x/tools v0.6.0
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
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))
}
}
{
"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"
]
}
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")
}
原始种子者
匿名