Skip to content

Commit

Permalink
fix: Always parse parentheses for predicates (#5069)
Browse files Browse the repository at this point in the history
A loooong time ago, Dafny supported `predicate` definitions without
parentheses. Apparently, the parser still allowed that, which is wrong.
This PR changes the parser to make sure that a predicate definition
always includes parentheses.

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
  • Loading branch information
RustanLeino authored Feb 20, 2024
1 parent ab33478 commit e49dec6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Source/DafnyCore/Dafny.atg
Original file line number Diff line number Diff line change
Expand Up @@ -1771,8 +1771,7 @@ FunctionDecl<DeclModifierData dmod, out Function/*!*/ f>
"a formal [ ] declaration is only allowed for least and greatest predicates");
.)
]
[ Formals<true, true, isTwoState, true, formals>
]
Formals<true, true, isTwoState, true, formals>
[ PredicateResult<"predicate", out result> ]
| ellipsis (. signatureEllipsis = t; .)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ method TestMethod() returns (x: int) {
trait TestTrait {}
predicate TestPredicate { false }", "test-workspace-symbols.dfy", false);
predicate TestPredicate() { false }", "test-workspace-symbols.dfy", false);

await client.OpenDocumentAndWaitAsync(documentItem, CancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ module ArrowTypes {
}
}

// ---------------------- missing parentheses for `predicate` -----------------------------------

module MissingPredicateParens0 {
predicate P<A>
} // error: expecting open paren

module MissingPredicateParens1 {
predicate P<A> { // error: expecting open paren
true
}
}

module MissingPredicateParens2 {
predicate P<A> { // error: expecting open paren
true
} by method {
return true;
}
}

// ---------------------- invalid newtype definition -----------------------------------

newtype T {} // error: newtype is expected to have an '='
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,8 @@ ParseErrors.dfy(261,28): Error: formal cannot be declared 'older' in this contex
ParseErrors.dfy(267,12): Error: arrow-type arguments may not be declared as 'ghost'
ParseErrors.dfy(272,18): Error: arrow-type arguments may not be declared as 'ghost'
ParseErrors.dfy(272,35): Error: arrow-type arguments may not be declared as 'ghost'
ParseErrors.dfy(278,10): Error: invalid NewtypeDecl
77 parse errors detected in ParseErrors.dfy
ParseErrors.dfy(280,0): Error: openparen expected
ParseErrors.dfy(283,17): Error: openparen expected
ParseErrors.dfy(289,17): Error: openparen expected
ParseErrors.dfy(298,10): Error: invalid NewtypeDecl
80 parse errors detected in ParseErrors.dfy
1 change: 1 addition & 0 deletions docs/dev/news/5069.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The syntax of a predicate definition must always include parentheses.

0 comments on commit e49dec6

Please sign in to comment.