Malformed or misplaced //@ annotations are ignored without a word, so a false contract passes
A //@ annotation that lsc doesn't read is dropped without a word. When the dropped line is a postcondition, a false contract passes: lsc check exits 0 with 0 verified, 0 errors, and nothing in the output says the contract was never checked.
Reproduction
Each file states a false postcondition: f returns x, and the contract says the result is greater than x. Written correctly, Dafny rejects it.
export function f(x: number): number {
//@ ensures \result > x
return x;
}
Then change only the annotation line, or where it sits, and run lsc check --backend=dafny f.ts on each.
Expected
Every form either verifies the contract, so Dafny rejects it and lsc check exits 1 as in the correct file, or lsc says it ignored the line.
Actual
On dd0847e with Dafny 4.11.0. 0.6.5 gives the same results; the misspelled keyword wasn't tried there.
| Annotation | lsc check |
ensures in lsc info --typed |
|---|---|---|
//@ ensures \result > x (correct) |
exit 1, 0 verified, 1 error |
1 |
//@ensures \result > x |
exit 0, 0 verified, 0 errors |
0 |
//@ ensures \result > x |
exit 0, 0 verified, 0 errors |
0 |
//@ ensures<TAB>\result > x |
exit 0, 0 verified, 0 errors |
0 |
//@ ensures (no expression) |
exit 0, 0 verified, 0 errors |
0 |
//@ ensure \result > x (misspelled) |
exit 0, 0 verified, 0 errors |
0 |
Above the declaration, which collectAnnotations also reads:
| Placement | lsc check |
ensures |
|---|---|---|
above export function f |
exit 1, 0 verified, 1 error |
1 |
| above an arrow with an expression body | exit 1, 0 verified, 1 error |
1 |
above an arrow with a block body, export const f = (x: number): number => { return x; }; |
exit 0, 0 verified, 0 errors |
0 |
above f, with a code line in between (const unrelated = 1;) |
exit 0, 0 verified, 0 errors |
0 |
We met the first forms through agents. In Tesota, a coding agent that runs lsc on the code it writes, one model wrote //@ ensures …, and lsc reported nothing wrong.
Where it happens
parseAnnotations in tools/src/extract.ts skips a comment with a bare continue in three places: when it doesn't start with "//@ ", when there is no space after the keyword, and when the keyword isn't in KEYWORDS. For the two placements, the annotations end up as leading comments of a node that no function reads: the const statement, or the arrow's variable statement, since parentAnnotations is used only for expression bodies.
Possible fix
A diagnostic on stderr for each //@ comment that lsc reads nothing from, naming the file, line and text. It must leave out the bare directives that other code reads, such as //@ verify, //@ pure, //@ skip and //@ extern (the full list is in SPEC §2.1). Whether it is a warning or fails the run is your call: a warning keeps existing files working, while an error makes the false pass impossible. Reading annotations above an arrow with a block body, as for an expression body, would remove one of the placement cases.
I can open a pull request for whichever you prefer.
Yes, thanks for the bug report. Agreed this should be fixed.
Thinking about it more, I am not sure it makes sense to track all these typos. Would we complain about any @ annotation that is ignored?
Yes, that's what I had in mind: a warning for any //@ that nothing reads, leaving TypeScript's //@ts-… comments alone. Does that sound too broad? Happy to send a PR, or leave it to you.
I am happy to get a PR and see what that looks like. I have a slight concern that other tools might use annotations for unrelated reasons, but we can see how it fares on the case studies with the CI.
Let's make it an error for now, just to see what breaks, though we should be ready to soften it to a warning.
Thanks! ~n