Repository navigation
fix: fail on //@ annotations that lsc does not read #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sequela02
wants to merge
1
commit into
midspiral:main
Choose a base branch
from
Sequela02:fix/unread-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import { initTypeParser } from "./types.js"; | |
| import type { RawExpr, RawStmt, RawFunction, RawModule, RawClass, RawConst, RawGhostLet, RawGhostAssign } from "./rawir.js"; | ||
| import { normalizeBigIntLiteral } from "./rawir.js"; | ||
| import { setUserNames, freshName } from "./names.js"; | ||
| import { DEFAULT_OPTIONS, type LscOptions } from "./config.js"; | ||
| import { DEFAULT_OPTIONS, fileCommentLines, isOptionDirective, type LscOptions } from "./config.js"; | ||
|
|
||
| // ── Expression extraction ──────────────────────────────────── | ||
|
|
||
|
|
@@ -737,24 +737,82 @@ function extractExpr(node: Expression): RawExpr { | |
|
|
||
| const PREFIX = "//@ "; | ||
| const KEYWORDS = ["requires", "ensures", "contract", "invariant", "decreases", "done_with", "type"] as const; | ||
| /** The other directives (SPEC §2.1) besides options, read by keyword elsewhere. */ | ||
| const DIRECTIVES = ["verify", "pure", "impure", "skip", "extern", "havoc", "autohavoc", "assert", "assume", | ||
| "ghost", "declare-type", "backend", "lean-module"]; | ||
| export type AnnotKind = (typeof KEYWORDS)[number]; | ||
| interface Annotation { kind: AnnotKind; expr: string } | ||
| /** `line` is set only for annotations in the file being extracted. */ | ||
| interface Annotation { kind: AnnotKind; expr: string; line?: number } | ||
|
|
||
| function parseAnnotation(text: string): Annotation | null { | ||
| if (!text.startsWith(PREFIX)) return null; | ||
| const content = text.slice(PREFIX.length); | ||
| const sp = content.indexOf(" "); | ||
| if (sp === -1) return null; | ||
| const kw = content.slice(0, sp); | ||
| if (!(KEYWORDS as readonly string[]).includes(kw)) return null; | ||
| return { kind: kw as AnnotKind, expr: content.slice(sp + 1).trim() }; | ||
| } | ||
|
|
||
| function parseAnnotations(node: Node): Annotation[] { | ||
| const result: Annotation[] = []; | ||
| for (const range of node.getLeadingCommentRanges()) { | ||
| const text = range.getText().trim(); | ||
| if (!text.startsWith(PREFIX)) continue; | ||
| const content = text.slice(PREFIX.length); | ||
| const sp = content.indexOf(" "); | ||
| if (sp === -1) continue; | ||
| const kw = content.slice(0, sp); | ||
| if (!(KEYWORDS as readonly string[]).includes(kw)) continue; | ||
| result.push({ kind: kw as AnnotKind, expr: content.slice(sp + 1).trim() }); | ||
| const a = parseAnnotation(range.getText().trim()); | ||
| if (!a) continue; | ||
| a.line = currentFileLine(node, range.getPos()); | ||
| result.push(a); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** The line of `pos` in `node`'s file, when that is the file being extracted. */ | ||
| function currentFileLine(node: Node, pos: number): number | undefined { | ||
| const sourceFile = node.getSourceFile(); | ||
| return sourceFile === _currentSourceFile ? sourceFile.getLineAndColumnAtPos(pos).line : undefined; | ||
| } | ||
|
|
||
| /** Lines of the annotations whose value reached the Raw IR. */ | ||
| const _readLines = new Set<number>(); | ||
| /** Line spans the extractor leaves out on purpose: `//@ skip`, unselected and extern functions. */ | ||
| const _skippedSpans: [number, number][] = []; | ||
|
|
||
| /** The annotation's expression, recording that it was read. */ | ||
| function readExpr(a: Annotation): string { | ||
| if (a.line !== undefined) _readLines.add(a.line); | ||
| return a.expr; | ||
| } | ||
| function annotExprs(annots: Annotation[], kind: AnnotKind): string[] { | ||
| return annots.filter(a => a.kind === kind).map(readExpr); | ||
| } | ||
| /** Only the first is read; a repeated one is reported as unread. */ | ||
| function firstAnnotExpr(annots: Annotation[], kind: AnnotKind): string | null { | ||
| const a = annots.find(a => a.kind === kind); | ||
| return a ? readExpr(a) : null; | ||
| } | ||
| function skipSpan(node: Node): void { | ||
| const first = node.getLeadingCommentRanges()[0]; | ||
| const from = first ? node.getSourceFile().getLineAndColumnAtPos(first.getPos()).line : node.getStartLineNumber(); | ||
| _skippedSpans.push([from, node.getEndLineNumber()]); | ||
| } | ||
|
|
||
| /** Fail on `//@` comments lsc ignores: a malformed or unknown one, or a | ||
| * contract annotation where no reader looks. Other directives are checked | ||
| * by keyword only. TypeScript's `//@ts-…` comments are not ours. */ | ||
| function checkAnnotationsRead(sourceFile: SourceFile): void { | ||
| const errors: string[] = []; | ||
| for (const { comment, index } of fileCommentLines(sourceFile.getFullText())) { | ||
| const text = comment.trim(); | ||
| if (!text.startsWith("//@") || text.startsWith("//@ts-")) continue; | ||
| const line = index + 1; | ||
| const keyword = text.match(/^\/\/@ (\S+)(?: |$)/)?.[1]; | ||
| const read = parseAnnotation(text) | ||
| ? _readLines.has(line) || _skippedSpans.some(([from, to]) => from <= line && line <= to) | ||
| : DIRECTIVES.includes(keyword ?? "") || isOptionDirective(text); | ||
| if (!read) errors.push(`${sourceFile.getBaseName()}:${line}: lsc does not read this annotation: ${text}`); | ||
| } | ||
| if (errors.length > 0) throw new Error(errors.join("\n")); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now that we've updated the case studies, should we make this a warning? or else there should be a flag to make it a warning. i am concerned that some code bases might have further conventions for //@ |
||
| } | ||
|
|
||
| function collectAnnotations(node: Node, body?: Node[]): Annotation[] { | ||
| const own = parseAnnotations(node); | ||
| if (body && body.length > 0) return [...own, ...parseAnnotations(body[0])]; | ||
|
|
@@ -881,8 +939,8 @@ function extractTypeDecl(decl: TypeAliasDeclaration, extraDecls?: TypeDeclInfo[] | |
| // Leading `//@ type <ty>` overrides extraction — the declared TS type is | ||
| // replaced by the annotated backend type. Used to coerce literal unions | ||
| // (`5 | 15 | 30` → `nat`) and other types LS can't model precisely. | ||
| const override = parseAnnotations(decl).find(a => a.kind === "type"); | ||
| if (override) return { name, typeParams: tpField, kind: "alias", aliasOf: override.expr }; | ||
| const override = firstAnnotExpr(parseAnnotations(decl), "type"); | ||
| if (override) return { name, typeParams: tpField, kind: "alias", aliasOf: override }; | ||
|
|
||
| if (type.isUnion()) { | ||
| const members = type.getUnionTypes(); | ||
|
|
@@ -979,7 +1037,9 @@ function extractInterface(decl: InterfaceDeclaration, extraDecls?: TypeDeclInfo[ | |
| if (Node.isPropertySignature(member)) { | ||
| const text = member.getTrailingCommentRanges().map(r => r.getText()).join(" "); | ||
| const match = text.match(/\/\/@ type (\w+)/); | ||
| if (match) overrides.set(member.getName(), match[1]); | ||
| if (match) { | ||
| overrides.set(member.getName(), readExpr({ kind: "type", expr: match[1], line: currentFileLine(member, member.getEnd()) })); | ||
| } | ||
| } | ||
| } | ||
| return extractRecord(decl.getName(), decl.getType(), decl, overrides, extraDecls); | ||
|
|
@@ -1400,6 +1460,7 @@ function extractStmts(stmts: Node[]): RawStmt[] { | |
|
|
||
| // //@ skip — omit this statement from the verification model | ||
| if (leadingComments.some(r => r.getText().trim() === "//@ skip")) { | ||
| skipSpan(s); | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -1606,9 +1667,9 @@ function extractStmts(stmts: Node[]): RawStmt[] { | |
| result.push({ | ||
| kind: "while", | ||
| cond: extractExpr(s.getExpression()), | ||
| invariants: annots.filter(a => a.kind === "invariant").map(a => a.expr), | ||
| decreases: annots.find(a => a.kind === "decreases")?.expr ?? null, | ||
| doneWith: annots.find(a => a.kind === "done_with")?.expr ?? null, | ||
| invariants: annotExprs(annots, "invariant"), | ||
| decreases: firstAnnotExpr(annots, "decreases"), | ||
| doneWith: firstAnnotExpr(annots, "done_with"), | ||
| body: extractStmts(bodyStmts), | ||
| line, | ||
| }); | ||
|
|
@@ -1656,8 +1717,8 @@ function extractStmts(stmts: Node[]): RawStmt[] { | |
| kind: "forof", | ||
| names, | ||
| iterable: extractExpr(iterableExpr), | ||
| invariants: annots.filter(a => a.kind === "invariant").map(a => a.expr), | ||
| doneWith: annots.find(a => a.kind === "done_with")?.expr ?? null, | ||
| invariants: annotExprs(annots, "invariant"), | ||
| doneWith: firstAnnotExpr(annots, "done_with"), | ||
| body: extractStmts(bodyStmts), | ||
| line, | ||
| }); | ||
|
|
@@ -1717,9 +1778,9 @@ function extractStmts(stmts: Node[]): RawStmt[] { | |
| } | ||
|
|
||
| let condExpr: RawExpr = cond ? extractExpr(cond) : { kind: "bool", value: true }; | ||
| let invariants = annots.filter(a => a.kind === "invariant").map(a => a.expr); | ||
| let decreases = annots.find(a => a.kind === "decreases")?.expr ?? null; | ||
| let doneWith = annots.find(a => a.kind === "done_with")?.expr ?? null; | ||
| let invariants = annotExprs(annots, "invariant"); | ||
| let decreases = firstAnnotExpr(annots, "decreases"); | ||
| let doneWith = firstAnnotExpr(annots, "done_with"); | ||
| for (const [from, to] of renames) { | ||
| for (let k = 0; k < hoisted.length; k++) hoisted[k] = renameRawStmt(hoisted[k], from, to); | ||
| condExpr = renameRawExpr(condExpr, from, to); | ||
|
|
@@ -1756,8 +1817,8 @@ function extractStmts(stmts: Node[]): RawStmt[] { | |
| kind: "forof", | ||
| names: [name], | ||
| iterable: extractExpr(s.getExpression()), | ||
| invariants: annots.filter(a => a.kind === "invariant").map(a => a.expr), | ||
| doneWith: annots.find(a => a.kind === "done_with")?.expr ?? null, | ||
| invariants: annotExprs(annots, "invariant"), | ||
| doneWith: firstAnnotExpr(annots, "done_with"), | ||
| body: extractStmts(bodyStmts), | ||
| line, | ||
| }); | ||
|
|
@@ -1976,7 +2037,9 @@ function extractFunctionInner(fn: FunctionDeclaration, parentAnnotations?: Annot | |
| for (const a of annots) { | ||
| if (a.kind === "type") { | ||
| const parts = a.expr.split(/\s+/); | ||
| if (parts.length === 2) typeAnnotations.push({ name: parts[0], type: parts[1] }); | ||
| if (parts.length !== 2) continue; | ||
| readExpr(a); | ||
| typeAnnotations.push({ name: parts[0], type: parts[1] }); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2065,10 +2128,10 @@ function extractFunctionInner(fn: FunctionDeclaration, parentAnnotations?: Annot | |
| if (inferred.isAny()) return "unknown"; | ||
| return _eraseGenerics(typeToString(inferred)); | ||
| })(), | ||
| requires: annots.filter(a => a.kind === "requires").map(a => a.expr), | ||
| ensures: annots.filter(a => a.kind === "ensures").map(a => a.expr), | ||
| contract: annots.filter(a => a.kind === "contract").map(a => a.expr), | ||
| decreases: annots.find(a => a.kind === "decreases")?.expr ?? null, | ||
| requires: annotExprs(annots, "requires"), | ||
| ensures: annotExprs(annots, "ensures"), | ||
| contract: annotExprs(annots, "contract"), | ||
| decreases: firstAnnotExpr(annots, "decreases"), | ||
| pure: hasPureAnnotation(fn, body && Node.isBlock(body) ? body.getStatements() as Node[] : undefined), | ||
| autohavoc: false, // set in extractModule (file-level directive or per-function) | ||
| typeAnnotations, | ||
|
|
@@ -2094,6 +2157,8 @@ export function extractModule(sourceFile: SourceFile, options: LscOptions = DEFA | |
| // here), deduped by qualified name. | ||
| _externs.clear(); | ||
| _externSigTypes.length = 0; | ||
| _readLines.clear(); | ||
| _skippedSpans.length = 0; | ||
|
|
||
| // Match a `//@ <kw>` directive only as the first non-whitespace on a line, | ||
| // so prose mentioning an annotation does not activate it. | ||
|
|
@@ -2204,7 +2269,7 @@ export function extractModule(sourceFile: SourceFile, options: LscOptions = DEFA | |
| const constants: RawConst[] = []; | ||
| for (const stmt of sourceFile.getStatements()) { | ||
| if (Node.isVariableStatement(stmt)) { | ||
| if (hasLeadingDirective(stmt, "skip")) continue; | ||
| if (hasLeadingDirective(stmt, "skip")) { skipSpan(stmt); continue; } | ||
| for (const decl of stmt.getDeclarationList().getDeclarations()) { | ||
| if (stmt.getDeclarationList().getFlags() & 2 /* const */) { | ||
| const init = decl.getInitializer(); | ||
|
|
@@ -2361,6 +2426,12 @@ export function extractModule(sourceFile: SourceFile, options: LscOptions = DEFA | |
| ); | ||
| const hasVerifyDirective = nonExternFns.some(hasVerify) || hasVerifiedClassMethod; | ||
| const fnsToExtract = hasVerifyDirective ? nonExternFns.filter(hasVerify) : nonExternFns; | ||
| // A statement holding a selected function keeps its comments checked. | ||
| for (const f of allFns) { | ||
| if (fnsToExtract.includes(f)) continue; | ||
| const wholeStmt = f.parentStmt && !fnsToExtract.some(s => s.parentStmt === f.parentStmt); | ||
| skipSpan(wholeStmt ? f.parentStmt! : f.node); | ||
| } | ||
|
|
||
| // `//@ autohavoc` — enable the auto-havoc abstraction (see autohavoc.ts). | ||
| // File-level: a directive at column 0 (top of file) enables it for every | ||
|
|
@@ -2739,11 +2810,11 @@ export function extractModule(sourceFile: SourceFile, options: LscOptions = DEFA | |
| // Extract classes with //@ verify methods | ||
| const classes: RawClass[] = []; | ||
| for (const cls of sourceFile.getClasses()) { | ||
| if (hasLeadingDirective(cls, "skip")) continue; | ||
| if (hasLeadingDirective(cls, "skip")) { skipSpan(cls); continue; } | ||
| const methods: RawFunction[] = []; | ||
| for (const method of cls.getMethods()) { | ||
| if (hasLeadingDirective(method, "skip")) continue; | ||
| if (!method.getFullText().includes('//@ verify')) continue; | ||
| if (hasLeadingDirective(method, "skip")) { skipSpan(method); continue; } | ||
| if (!method.getFullText().includes('//@ verify')) { skipSpan(method); continue; } | ||
| methods.push(extractFunction(method as any)); | ||
| } | ||
| if (methods.length === 0) continue; | ||
|
|
@@ -2758,6 +2829,8 @@ export function extractModule(sourceFile: SourceFile, options: LscOptions = DEFA | |
| // union stringification outside of an extractModule call. | ||
| _synthArrayUnions = null; | ||
|
|
||
| checkAnnotationsRead(sourceFile); | ||
|
|
||
| return { | ||
| file: sourceFile.getFilePath(), | ||
| typeDecls, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the relative file path than just the base