rootfs apps plan 3: Task 5 round 3 P2, the layout guard recognises Pass methods by type, whatever the receiver's name
passIdents binds every receiver, parameter and local of type Pass or
*Pass (declared, or built from a Pass literal), and the primitive,
predicate and callee checks match method calls on any of them. Negative
fixtures: q.quiesce through another receiver name, keptFence on a local
&Pass{}, and q.keep before q.exempt.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+113
−21
internal/layout/layout_exempt_guard_test.go+113 −21
CI failed — run details
diff --git a/internal/layout/layout_exempt_guard_test.go b/internal/layout/layout_exempt_guard_test.go| index fe90cbc14..f65890a5d 100644 |
| --- a/internal/layout/layout_exempt_guard_test.go |
| +++ b/internal/layout/layout_exempt_guard_test.go |
| @@ -70,8 +70,86 @@ func layoutConfig() guardConfig { |
| return guardConfig{guarded: layoutGuarded, internal: layoutInternal, seams: layoutSeams, library: layoutLibraryFiles} |
| } |
| -// primitiveName names call c if it is a primitive, else "". |
| -func primitiveName(c *ast.CallExpr) string { |
| +// isPassType reports whether expr is the type Pass or *Pass. |
| +func isPassType(expr ast.Expr) bool { |
| + if star, ok := expr.(*ast.StarExpr); ok { |
| + expr = star.X |
| + } |
| + id, ok := expr.(*ast.Ident) |
| + return ok && id.Name == "Pass" |
| +} |
| + |
| +// isPassValue reports whether expr builds a Pass: Pass{...} or &Pass{...}. |
| +func isPassValue(expr ast.Expr) bool { |
| + if u, ok := expr.(*ast.UnaryExpr); ok && u.Op == token.AND { |
| + expr = u.X |
| + } |
| + cl, ok := expr.(*ast.CompositeLit) |
| + return ok && cl.Type != nil && isPassType(cl.Type) |
| +} |
| + |
| +// passIdents are the names fd binds to a Pass or *Pass, whatever they are |
| +// called: its receiver, its parameters, and its locals declared with the |
| +// type (var q *Pass) or from a Pass literal (q := &Pass{...}). A method |
| +// call on any of them is a Pass method call. |
| +func passIdents(fd *ast.FuncDecl) map[string]bool { |
| + out := map[string]bool{} |
| + fields := func(fl *ast.FieldList) { |
| + if fl == nil { |
| + return |
| + } |
| + for _, f := range fl.List { |
| + if isPassType(f.Type) { |
| + for _, n := range f.Names { |
| + out[n.Name] = true |
| + } |
| + } |
| + } |
| + } |
| + fields(fd.Recv) |
| + fields(fd.Type.Params) |
| + if fd.Body == nil { |
| + return out |
| + } |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + switch n := n.(type) { |
| + case *ast.ValueSpec: |
| + typed := n.Type != nil && isPassType(n.Type) |
| + for i, name := range n.Names { |
| + if typed || (i < len(n.Values) && isPassValue(n.Values[i])) { |
| + out[name.Name] = true |
| + } |
| + } |
| + case *ast.AssignStmt: |
| + for i, lhs := range n.Lhs { |
| + if id, ok := lhs.(*ast.Ident); ok && i < len(n.Rhs) && isPassValue(n.Rhs[i]) { |
| + out[id.Name] = true |
| + } |
| + } |
| + case *ast.FuncLit: |
| + fields(n.Type.Params) |
| + } |
| + return true |
| + }) |
| + return out |
| +} |
| + |
| +// passMethod is the Pass method c calls on one of passes, or "". |
| +func passMethod(c *ast.CallExpr, passes map[string]bool) string { |
| + if sel, ok := c.Fun.(*ast.SelectorExpr); ok { |
| + if x, ok := sel.X.(*ast.Ident); ok && passes[x.Name] { |
| + return sel.Sel.Name |
| + } |
| + } |
| + return "" |
| +} |
| + |
| +// primitiveName names call c if it is a primitive, else "". passes are |
| +// the names bound to a Pass in the enclosing function (passIdents). |
| +func primitiveName(c *ast.CallExpr, passes map[string]bool) string { |
| + if m := passMethod(c, passes); layoutPrimitiveMethods[m] { |
| + return m |
| + } |
| switch fn := c.Fun.(type) { |
| case *ast.Ident: |
| if layoutPrimitiveFuncs[fn.Name] { |
| @@ -82,9 +160,6 @@ func primitiveName(c *ast.CallExpr) string { |
| if x.Name == "activator" && fn.Sel.Name == "Classify" { |
| return "activator.Classify" |
| } |
| - if x.Name == "p" && layoutPrimitiveMethods[fn.Sel.Name] { |
| - return fn.Sel.Name |
| - } |
| } |
| if inner, ok := fn.X.(*ast.SelectorExpr); ok && inner.Sel.Name == "Fence" { |
| return "Fence." + fn.Sel.Name |
| @@ -93,27 +168,26 @@ func primitiveName(c *ast.CallExpr) string { |
| return "" |
| } |
| -// isPredicate reports whether c asks the invariant's predicate. |
| -func isPredicate(c *ast.CallExpr) bool { |
| +// isPredicate reports whether c asks the invariant's predicate: |
| +// activator.LayoutExempt, or the exempt method of a Pass. |
| +func isPredicate(c *ast.CallExpr, passes map[string]bool) bool { |
| + if passMethod(c, passes) == "exempt" { |
| + return true |
| + } |
| sel, ok := c.Fun.(*ast.SelectorExpr) |
| if !ok { |
| return false |
| } |
| x, ok := sel.X.(*ast.Ident) |
| - return ok && ((x.Name == "activator" && sel.Sel.Name == "LayoutExempt") || (x.Name == "p" && sel.Sel.Name == "exempt")) |
| + return ok && x.Name == "activator" && sel.Sel.Name == "LayoutExempt" |
| } |
| -// calleeName is the package function or pass method c calls, or "". |
| -func calleeName(c *ast.CallExpr) string { |
| - switch fn := c.Fun.(type) { |
| - case *ast.Ident: |
| - return fn.Name |
| - case *ast.SelectorExpr: |
| - if x, ok := fn.X.(*ast.Ident); ok && x.Name == "p" { |
| - return fn.Sel.Name |
| - } |
| +// calleeName is the package function or Pass method c calls, or "". |
| +func calleeName(c *ast.CallExpr, passes map[string]bool) string { |
| + if id, ok := c.Fun.(*ast.Ident); ok { |
| + return id.Name |
| } |
| - return "" |
| + return passMethod(c, passes) |
| } |
| type funcFacts struct { |
| @@ -133,19 +207,20 @@ func collectFacts(files map[string]*ast.File) map[string]*funcFacts { |
| continue |
| } |
| ff := &funcFacts{file: name, calls: map[string][]token.Pos{}} |
| + passes := passIdents(fd) |
| ast.Inspect(fd.Body, func(n ast.Node) bool { |
| c, ok := n.(*ast.CallExpr) |
| if !ok { |
| return true |
| } |
| - if isPredicate(c) && ff.predicate == token.NoPos { |
| + if isPredicate(c, passes) && ff.predicate == token.NoPos { |
| ff.predicate = c.Pos() |
| } |
| - if pn := primitiveName(c); pn != "" { |
| + if pn := primitiveName(c, passes); pn != "" { |
| ff.primitives = append(ff.primitives, c.Pos()) |
| ff.primNames = append(ff.primNames, pn) |
| } |
| - if cn := calleeName(c); cn != "" { |
| + if cn := calleeName(c, passes); cn != "" { |
| ff.calls[cn] = append(ff.calls[cn], c.Pos()) |
| } |
| return true |
| @@ -373,6 +448,23 @@ func (p *Pass) sneak(host string) { ReadJournal("d", host) }`, "sneak: calls Rea |
| func (p *Pass) entry(rt Route) { if p.exempt(rt) { return } } |
| func (p *Pass) quiesce() { p.Fence.LiftFence("h", 1) } |
| func (p *Pass) sneak(host string) { p.quiesce() }`, "sneak: calls the internal helper quiesce without asking the predicate"}, |
| + "internal helper through another receiver name": {` |
| +func (p *Pass) entry(rt Route) { if p.exempt(rt) { return } } |
| +func (p *Pass) quiesce() { p.Fence.LiftFence("h", 1) } |
| +func (q *Pass) sneak(host string) { q.quiesce() }`, "sneak: calls the internal helper quiesce without asking the predicate"}, |
| + "primitive through a local Pass": {` |
| +func (p *Pass) entry(rt Route) { if p.exempt(rt) { return } } |
| +func (p *Pass) quiesce() {} |
| +func sneak(host string) { |
| + other := &Pass{} |
| + other.keptFence(host) |
| +}`, "sneak: calls keptFence and is in no table (guard it, or list it internal)"}, |
| + "predicate under another receiver name, primitive first": {` |
| +func (q *Pass) entry(rt Route) { |
| + q.keep("h", 1) |
| + if q.exempt(rt) { return } |
| +} |
| +func (p *Pass) quiesce() {}`, "entry: calls keep before it asks the predicate"}, |
| "internal helper before the predicate": {` |
| func (p *Pass) entry(rt Route) { |
| p.quiesce() |