示例
golang.org/x/tools v0.1.1: cfg.Block
已验证示例 — golang golang.org/x/tools v0.1.1: cfg.Block. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: cfg.Block represents basic blocks in a…
sha256:c6c314c614778a453c6762935d96fc7fee83b7b384b656621ebcae24d279fc6b
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 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-10-14 |
案例
HOW- 目标
- verify golang.org/x/tools/go/cfg.Block in pkg:golang/golang.org/x/tools@v0.1.1
- 符号
-
- golang.org/x/tools/go/cfg.Block
- 创建时间
- 2026-10-14T00:48:59Z
契约
- cfg.Block represents basic blocks in a control-flow graph with Index, Live, Nodes, and Succs properties
- cfg.Block.Return returns the terminating *ast.ReturnStmt for return blocks and nil for non-return blocks
- cfg.Block.String formats the block index and comment description
- cfg.Block marks unreachable basic blocks after unconditional return with Live == false
- cfg.Block Succs links conditional branches to two successor blocks and linear flow to one successor block
文件
- 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.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/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:1527fa01316e8237c0cbc0896d88046c76e4279c201e95be769c65d3299d6ce7","contract":["cfg.Block represents basic blocks in a control-flow graph with Index, Live, Nodes, and Succs properties","cfg.Block.Return returns the terminating *ast.ReturnStmt for return blocks and nil for non-return blocks","cfg.Block.String formats the block index and comment description","cfg.Block marks unreachable basic blocks after unconditional return with Live == false","cfg.Block Succs links conditional branches to two successor blocks and linear flow to one successor block"],"goal":"verify golang.org/x/tools/go/cfg.Block 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/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.1.1"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.1.1","symbols":["golang.org/x/tools/go/cfg.Block"],"verifierAdapter":"golang@1"}
module sample
go 1.21
require golang.org/x/tools v0.1.1
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"golang.org/x/tools/go/cfg"
)
func main() {
src := `package p
func classify(n int) int {
if n > 0 {
return 1
}
return 0
}`
fset := token.NewFileSet()
file, 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 file.Decls {
if fd, ok := decl.(*ast.FuncDecl); ok {
fn = fd
break
}
}
if fn == nil || fn.Body == nil {
fmt.Println("missing function body")
return
}
graph := cfg.New(fn.Body, func(call *ast.CallExpr) bool { return true })
for _, blk := range graph.Blocks {
var b *cfg.Block = blk
fmt.Printf("Block %d: live=%v succs=%d nodes=%d return=%v\n", b.Index, b.Live, len(b.Succs), len(b.Nodes), b.Return() != nil)
}
}
{
"schemaVersion": 1,
"goal": "verify golang.org/x/tools/go/cfg.Block 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/cfg.Block"
]
}
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
"golang.org/x/tools/go/cfg"
)
func mustParseFunc(src string) (*ast.FuncDecl, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "input.go", src, 0)
if err != nil {
return nil, fmt.Errorf("parse error: %w", err)
}
for _, decl := range file.Decls {
if fd, ok := decl.(*ast.FuncDecl); ok {
return fd, nil
}
}
return nil, fmt.Errorf("no FuncDecl 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 simple() {
a := 1
_ = a
}`
fn, err := mustParseFunc(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 non-empty Blocks\n")
os.Exit(1)
}
var entry *cfg.Block = g.Blocks[0]
if entry.Index != 0 || !entry.Live || len(entry.Nodes) == 0 {
fmt.Fprintf(os.Stderr, "assertion 1 failed: unexpected entry block state: index=%d live=%v nodes=%d\n", entry.Index, entry.Live, len(entry.Nodes))
os.Exit(1)
}
}
// Assertion 2: cfg.Block.Return returns the terminating *ast.ReturnStmt for return blocks and nil for non-return blocks
{
src := `package p
func cond(v int) int {
if v > 0 {
return 10
}
return 20
}`
fn, err := mustParseFunc(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 hasReturn, hasNonReturn bool
for _, b := range g.Blocks {
if b.Return() != nil {
hasReturn = true
} else {
hasNonReturn = true
}
}
if !hasReturn || !hasNonReturn {
fmt.Fprintf(os.Stderr, "assertion 2 failed: hasReturn=%v hasNonReturn=%v\n", hasReturn, hasNonReturn)
os.Exit(1)
}
}
// Assertion 3: cfg.Block.String formats the block index and comment description
{
src := `package p
func ret() int {
return 7
}`
fn, err := mustParseFunc(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 })
s := g.Blocks[0].String()
if !strings.Contains(s, "block 0") {
fmt.Fprintf(os.Stderr, "assertion 3 failed: unexpected String() output %q\n", s)
os.Exit(1)
}
}
// Assertion 4: cfg.Block marks unreachable basic blocks after unconditional return with Live == false
{
src := `package p
func dead() int {
return 1
y := 2
return y
}`
fn, err := mustParseFunc(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 sawDead bool
for _, b := range g.Blocks {
if !b.Live {
sawDead = true
break
}
}
if !sawDead {
fmt.Fprintf(os.Stderr, "assertion 4 failed: expected at least one dead block with Live == false\n")
os.Exit(1)
}
}
// Assertion 5: cfg.Block Succs links conditional branches to two successor blocks and linear flow to one successor block
{
src := `package p
func branch(flag bool) {
if flag {
_ = 1
}
_ = 2
}`
fn, err := mustParseFunc(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 twoSuccs, oneSucc bool
for _, b := range g.Blocks {
switch len(b.Succs) {
case 2:
twoSuccs = true
case 1:
oneSucc = true
}
}
if !twoSuccs || !oneSucc {
fmt.Fprintf(os.Stderr, "assertion 5 failed: twoSuccs=%v oneSucc=%v\n", twoSuccs, oneSucc)
os.Exit(1)
}
}
fmt.Println("contract ok")
}
原始种子者
匿名