fix(cap-check,intent-check): scan from bodyTokenStart — no false CAP001/INT002 on param types#75
Merged
Merged
Conversation
…positives on parameter/return types CAP001/CAP002 (cap-check.ts) and INT002 (intent-check.ts) were scanning from fn.tokenStart, which includes the fn keyword, name, parameter list, and return type annotation. This caused false positives when a stdlib namespace identifier (http, fs, time, …) appeared in a type annotation rather than an actual capability call — for example: fn handleReq(client: http.Client) -> string = "ok" // ^^ CAP001 fired on http.Client even though no capability is consumed The fix mirrors the approach from PR #71 (dep-check) and the thr002 branch (thr-check): start the scan at fn.bodyTokenStart ?? fn.tokenStart so the parameter list and return type annotation are skipped entirely. Adds regression tests to cap-check.test.ts confirming stdlib namespace in parameter type annotations no longer triggers CAP001. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes false CAP001/CAP002 and INT002 diagnostics caused by scanning tokens starting at fn.tokenStart (which includes parameter lists and return type annotations). The passes now start scanning at fn.bodyTokenStart ?? fn.tokenStart, aligning capability/intent analysis with actual function bodies.
Changes:
- Update
cap-checkbody scanning to start atfn.bodyTokenStart(with fallback) to avoid matching identifiers in type positions. - Update
intent-check(INT002) body scanning to start atfn.bodyTokenStart(with fallback) for the same reason. - Add regression tests intended to ensure stdlib namespaces in type annotations do not trigger capability errors.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/compiler/src/passes/cap-check.ts | Start capability scans at bodyTokenStart to avoid false positives from signature type annotations. |
| packages/compiler/src/passes/intent-check.ts | Start INT002 “pure body” scan at bodyTokenStart to avoid signature-only false positives. |
| packages/compiler/tests/cap-check.test.ts | Add regression coverage for stdlib namespaces appearing in parameter/return type annotations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+325
to
+330
| // Body calls http.newClient() without uses {net} — should fire for the body call but | ||
| // NOT double-fire for the return type annotation. | ||
| try { t(src); } catch { /* expected */ } | ||
| // Point: the test only checks the body-scan starts at body, not param/return type. | ||
| // The actual CAP001 fire is acceptable — we just don't want double diagnostics. | ||
| // This mainly documents the fix; the first test above is the regression sentinel. |
Comment on lines
+325
to
+330
| // Body calls http.newClient() without uses {net} — should fire for the body call but | ||
| // NOT double-fire for the return type annotation. | ||
| try { t(src); } catch { /* expected */ } | ||
| // Point: the test only checks the body-scan starts at body, not param/return type. | ||
| // The actual CAP001 fire is acceptable — we just don't want double diagnostics. | ||
| // This mainly documents the fix; the first test above is the regression sentinel. |
Comment on lines
136
to
140
| function checkDirectFn(src: string, tokens: Token[], fn: FnDecl, inner: FnDecl[]): void { | ||
| const declared = new Set(fn.capabilities); | ||
| for (let i = fn.tokenStart; i < fn.tokenEnd; i++) { | ||
| for (let i = fn.bodyTokenStart ?? fn.tokenStart; i < fn.tokenEnd; i++) { | ||
| if (insideAny(i, inner)) continue; | ||
| const tok = tokens[i]; |
Replace the assertion-free second test case with one that actually verifies no CAP001 fires when the stdlib namespace appears only in the return type (body is a plain literal, not a stdlib call). Also removes the misleading "double-fire" comment: cap-check short-circuits on first error so double-fire is impossible; the real concern is false positives from type annotations. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CAP001/CAP002 and INT002 were scanning from `fn.tokenStart`, which includes the parameter list and return type annotation. A stdlib namespace identifier (`http`, `fs`, `time`, …) appearing in a type annotation triggered a false positive — for example:
```bs
fn handleReq(client: http.Client) -> string = "ok"
// CAP001 fires: 'handleReq' calls 'http.Client' which requires 'net'
// but http.Client is a type annotation, not a capability call
```
Fix
Start the body scan at `fn.bodyTokenStart ?? fn.tokenStart` in both `cap-check.ts` and `intent-check.ts`, matching the pattern established by PR #71 (dep-check) and the thr002 branch (thr-check).
Test plan
🤖 Generated with Claude Code