diff --git a/adapters/gopatterns/ir_reaching_subset.go b/adapters/gopatterns/ir_reaching_subset.go index 5828dfa..9981107 100644 --- a/adapters/gopatterns/ir_reaching_subset.go +++ b/adapters/gopatterns/ir_reaching_subset.go @@ -45,7 +45,7 @@ func (b *irBuilder) scalarSourceBinding(node ast.Node) bool { func (b *irBuilder) scalarSourceType(node ast.Node) bool { if declaration, ok := node.(*ast.GenDecl); ok { - return declaration.Tok == token.VAR + return b.scalarDeclarationSource(declaration) } expression, ok := node.(ast.Expr) if !ok { @@ -59,6 +59,34 @@ func (b *irBuilder) scalarSourceType(node ast.Node) bool { return ok && basic.Info()&(types.IsInteger|types.IsBoolean) != 0 } +func (b *irBuilder) scalarDeclarationSource(declaration *ast.GenDecl) bool { + if declaration.Tok == token.VAR { + return true + } + if declaration.Tok != token.TYPE { + return false + } + for _, spec := range declaration.Specs { + if !b.scalarAliasSource(spec) { + return false + } + } + return true +} + +func (b *irBuilder) scalarAliasSource(spec ast.Spec) bool { + alias, ok := spec.(*ast.TypeSpec) + if !ok || !alias.Assign.IsValid() || alias.TypeParams != nil { + return false + } + typ := b.info.TypeOf(alias.Type) + if typ == nil { + return false + } + basic, ok := typ.Underlying().(*types.Basic) + return ok && basic.Info()&(types.IsInteger|types.IsBoolean) != 0 +} + func scalarBinding(object types.Object) bool { variable, ok := object.(*types.Var) if !ok || variable.IsField() { diff --git a/adapters/gopatterns/native_local_alias_test.go b/adapters/gopatterns/native_local_alias_test.go new file mode 100644 index 0000000..17cd2de --- /dev/null +++ b/adapters/gopatterns/native_local_alias_test.go @@ -0,0 +1,158 @@ +package gopatterns + +import ( + "context" + "go/ast" + "os" + "path/filepath" + "reflect" + "slices" + "testing" + + "github.com/rstyled/gstyle/adapters/govalidation" + "github.com/rstyled/gstyle/domain/pdg" +) + +func TestNativeLocalScalarAliases(t *testing.T) { + cases := []string{ + `func f(a int32) int32 { type local=int32; var x local=a; return x }`, + `func f() int32 { type local=int32; var x local; return x }`, + `func f(a bool) bool { type local=bool; var x local=a; return x }`, + `func f(a int32,b bool) int32 { type local=int32; var x local=a; if b { x=1 }; return x }`, + `func f(a int32,b bool) int32 { if b { type local=int32; var x local=a; return x }; return a }`, + `func f(a int32) int32 { type (first=int32; second=first); var x second=a; return x }`, + `func f(a int32) int32 { type local=int32; { type local=bool; var x local; _=x }; var x local=a; return x }`, + `func f() { type local=bool; var x local; _=x }`, + `func f(a uint64) uint64 { type local=uint64; var x local=a; return x }`, + `func f(a int) int { type local=int; var x local=a; return x }`, + `func f(a bool) bool { type int32=bool; var x int32=a; return x }`, + `func f(a int32) int32 { type local=int32; return a }`, + } + for _, source := range cases { + t.Run(source, func(t *testing.T) { + pkg := loadTestPackage(t, "package p; "+source) + root := filepath.Dir(pkg.Fset.PositionFor(pkg.Syntax[0].Pos(), false).Filename) + matching, err := ExtractMatching(context.Background(), root, []string{"."}) + if err != nil { + t.Fatal(err) + } + if len(matching.Ineligible) != 0 || len(matching.Graphs) != 1 { + t.Fatalf("scalar alias rejected: %+v", matching.Ineligible) + } + if matching.Graphs[0].Conformance.Status != pdg.Conformant { + t.Fatal("native graph is not certified") + } + extraction, err := Extract(context.Background(), root, []string{"."}) + if err != nil { + t.Fatal(err) + } + if len(extraction.Callables) != 1 || extraction.Callables[0].Conformance.Status != pdg.Conformant { + t.Fatal("routine export disagrees with native admission") + } + }) + } +} + +func TestLocalAliasAdmissionKeepsUnsupportedDeclarationsPartial(t *testing.T) { + for _, declaration := range []string{ + `type local int32`, + `type local=float64`, + `type local=string`, + `type local=*int32`, + `type local=struct{ X int32 }`, + `type local=func() int32`, + `type local[T any]=int32`, + } { + t.Run(declaration, func(t *testing.T) { + root := t.TempDir() + for name, content := range map[string]string{ + "go.mod": "module p\n\ngo 1.26\n", + "p.go": "package p; func f(a int32) int32 { " + declaration + "; return a }", + } { + if err := os.WriteFile(filepath.Join(root, name), []byte(content), 0600); err != nil { + t.Fatal(err) + } + } + extraction, err := Extract(context.Background(), root, []string{"."}) + if err != nil { + t.Fatal(err) + } + if len(extraction.Callables) != 1 || extraction.Callables[0].Conformance.Status == pdg.Conformant { + t.Fatal("unsupported declaration is certified") + } + }) + } +} + +func TestLocalAliasVerifierRejectsCorruptFacts(t *testing.T) { + source := `package p; func f(a int32) int32 { type local=int32; var x local=a; return x }` + pkg := loadTestPackage(t, source) + fn := findFunc(pkg.Syntax[0], "f") + for _, fault := range []string{"type-operation", "missing-value", "binding-type"} { + t.Run(fault, func(t *testing.T) { + pool := pdg.NewBuilder() + graph, err := buildGraph(pkg, fn, "p.go", pool) + if err != nil { + t.Fatal(err) + } + switch fault { + case "type-operation": + alias := fn.Body.List[0].(*ast.DeclStmt).Decl.(*ast.GenDecl).Specs[0] + graph.Spans = append(graph.Spans, pdg.Span{Source: 1, Start: uint32(pkg.Fset.PositionFor(alias.Pos(), false).Offset), End: uint32(pkg.Fset.PositionFor(alias.End(), false).Offset)}) + fake := graph.Nodes[1] + fake.ID, fake.Span, fake.OriginalKind = pool.Text("fake-type-operation"), pdg.Ref(len(graph.Spans)), pool.Known("*ast.TypeSpec") + fake.Category = pdg.Computation + graph.Nodes = append(graph.Nodes, fake) + case "missing-value": + graph.Edges = slices.DeleteFunc(graph.Edges, func(edge pdg.Edge) bool { return graph.Tables.Text(edge.Subkind) == "scalar-value" }) + case "binding-type": + graph.Symbols[0].Type = pool.Known("bool") + } + graph.Tables = pool.Tables + if err := pdg.Validate(&graph); err != nil { + t.Fatal("corruption must remain structurally valid", err) + } + if validateNativeSource(&graph, pkg, fn) == nil { + t.Fatal("corrupted alias graph accepted") + } + }) + } +} + +func TestLocalAliasHasOnlyRuntimeDependencies(t *testing.T) { + source := `package p; func f(a int32) int32 { type local=int32; var x local=a; return x }` + graph := parseIR(t, source, pdg.NewBuilder()) + fn, fset, info := independentSource(t, source) + if err := govalidation.Inventory(&graph, fn, fset, info); err != nil { + t.Fatal(err) + } + if err := govalidation.CFG(&graph, fn, fset); err != nil { + t.Fatal(err) + } + var got []string + for _, edge := range graph.Edges { + if edge.Kind != pdg.Data && edge.Kind != pdg.Execution { + t.Fatalf("unexpected dependency kind: %v", edge.Kind) + } + kind := "value" + if edge.Kind == pdg.Execution { + kind = "execution" + } + got = append(got, kind+": "+compatOperationSource(&graph, source, edge.Source)+" -> "+compatOperationSource(&graph, source, edge.Target)) + } + want := []string{ + "value: a int32 -> a", + "value: a -> x local=a", + "value: x local=a -> x", + "value: x -> return x", + "value: return x -> int32", + "execution: a -> x local=a", + "execution: x local=a -> x", + "execution: x -> return x", + } + slices.Sort(got) + slices.Sort(want) + if !reflect.DeepEqual(got, want) || len(graph.Nodes) != 6 { + t.Fatalf("runtime graph has unexpected facts: nodes=%d\ngot %v\nwant %v", len(graph.Nodes), got, want) + } +} diff --git a/adapters/govalidation/cfg.go b/adapters/govalidation/cfg.go index a9f5a2a..b300e1e 100644 --- a/adapters/govalidation/cfg.go +++ b/adapters/govalidation/cfg.go @@ -118,7 +118,14 @@ func (a *sourceFlow) returnFlow(node *ast.ReturnStmt) []pdg.Ref { func (a *sourceFlow) declarations(node *ast.DeclStmt, next []pdg.Ref) []pdg.Ref { declaration, ok := node.Decl.(*ast.GenDecl) - if !ok || declaration.Tok != token.VAR { + if !ok { + a.err = fmt.Errorf("unsupported declaration") + return nil + } + if declaration.Tok == token.TYPE { + return next + } + if declaration.Tok != token.VAR { a.err = fmt.Errorf("unsupported declaration") return nil } diff --git a/docs/pdg-scalar-compatibility.md b/docs/pdg-scalar-compatibility.md index 8ba990a..9b44681 100644 --- a/docs/pdg-scalar-compatibility.md +++ b/docs/pdg-scalar-compatibility.md @@ -42,6 +42,13 @@ zero-initialized definition identity. There are no initializer expressions to order. Multiple initializer expressions, parallel assignments and multiple results retain the existing admission limits. +Local non-generic type aliases to integer and Boolean types are admitted when +the rest of the function meets this profile. Alias declarations add no runtime +operation or dependency. Typed variable bindings retain their resolved source +type, including grouped aliases, alias chains and lexical shadowing. Distinct +local type definitions and aliases to other kinds remain outside this admission +boundary. Source inventory and CFG validation reject invented alias operations. + Routine extraction now checks eligible scalar graphs with the same independent verifiers used by native matching. Certification runs on the final graph after package exit refinement. It requires a known source snapshot and target width, diff --git a/quality-baseline.json b/quality-baseline.json index 28092cb..a5e9822 100644 --- a/quality-baseline.json +++ b/quality-baseline.json @@ -1,29 +1,29 @@ { "version": 1, - "generated_at": "2026-10-29T04:03:50Z", - "commit": "7f604ae", - "functions": 5600, + "generated_at": "2026-10-29T15:24:22Z", + "commit": "5721714", + "functions": 5602, "tolerance": 0.05, "rules": { "aggregate": { "excess": 18, - "per_1000_functions": 3.2142857142857144 + "per_1000_functions": 3.2131381649410926 }, "fn_params": { "excess": 28, - "per_1000_functions": 5 + "per_1000_functions": 4.998214923241699 }, "mutated_targets": { "excess": 59, - "per_1000_functions": 10.535714285714285 + "per_1000_functions": 10.531952873973582 }, "mutation_per_target": { "excess": 154, - "per_1000_functions": 27.5 + "per_1000_functions": 27.490182077829346 }, "side_effect_density": { "excess": 235478, - "per_1000_functions": 42049.642857142855 + "per_1000_functions": 42034.630489111034 } } }