-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: A function with ghost parameters used as a value is ghost (#2847)
This PR fixes two issues: ghost parameters of function values and ghost parameters of arrow types. * Enforce that a non-ghost function with ghost parameters can be used as a value only in ghost contexts. For example, disallow `var f := F;` if `f` is compiled and `F` is a function with ghost parameters. * Disallow arrow types that look like they have ghost parameters. For example, disallow an attempted arrow type like `(ghost int) -> int`. Fixes #2843 <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
1 parent
030cf95
commit 7c207ae
Showing
7 changed files
with
110 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// RUN: %dafny_0 /functionSyntax:4 "%s" > "%t" | ||
// RUN: %diff "%s.expect" "%t" | ||
|
||
function F(ghost x: int, y: int): int { | ||
y | ||
} | ||
|
||
ghost function G(x: int, y: int): int { | ||
y | ||
} | ||
|
||
method M0() { | ||
var f: (int, int) -> int; | ||
f := F; // error: the expression `F` is ghost, since function F has a ghost parameter | ||
print F(2, 3), " ", f(2, 3), "\n"; | ||
} | ||
|
||
method M1() { | ||
var g: (int, int) -> int; | ||
g := G; // error: G is ghost | ||
print g(2, 3), "\n"; | ||
} | ||
|
||
method M2() { | ||
ghost var f: (int, int) -> int; | ||
f := F; // fine, since f is ghost | ||
f := G; // fine | ||
} | ||
|
||
method M3() { | ||
var f := F; // f is auto-ghost | ||
var g := G; // g is auto-ghost | ||
print f(2, 3), " ", g(2, 3), "\n"; // error (x2): f and g are ghost | ||
|
||
var tuple0: (ghost (int, int) -> int, int, ghost (int, int) -> int); | ||
tuple0 := (ghost F, 10, ghost G); | ||
tuple0 := (ghost f, 10, ghost g); | ||
print tuple0, "\n"; | ||
|
||
var tuple1; // type is inferred (same as for tuple0 above) | ||
tuple1 := (ghost F, 10, ghost G); | ||
tuple1 := (ghost f, 10, ghost g); | ||
print tuple1, "\n"; | ||
|
||
var tuple2: ((int, int) -> int, int, (int, int) -> int); | ||
tuple2 := (F, 10, G); // error (x2): F has ghost parameters and G is ghost | ||
tuple2 := (f, 10, g); // error (x2): f and g are ghost variables | ||
print tuple2, "\n"; | ||
|
||
ghost var tuple3: ((int, int) -> int, int, (int, int) -> int); | ||
tuple3 := (F, 10, G); | ||
tuple3 := (f, 10, g); | ||
print tuple3, "\n"; // error: cannot print a ghost | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
git-issue-2843.dfy(14,7): Error: a function with ghost parameters can be used as a value only in specification contexts | ||
git-issue-2843.dfy(20,7): Error: a ghost function is allowed only in specification contexts | ||
git-issue-2843.dfy(33,8): Error: a ghost variable is allowed only in specification contexts | ||
git-issue-2843.dfy(33,22): Error: a ghost variable is allowed only in specification contexts | ||
git-issue-2843.dfy(46,13): Error: a function with ghost parameters can be used as a value only in specification contexts | ||
git-issue-2843.dfy(46,20): Error: a ghost function is allowed only in specification contexts | ||
git-issue-2843.dfy(47,13): Error: a ghost variable is allowed only in specification contexts | ||
git-issue-2843.dfy(47,20): Error: a ghost variable is allowed only in specification contexts | ||
git-issue-2843.dfy(53,8): Error: a ghost variable is allowed only in specification contexts | ||
9 resolution/type errors detected in git-issue-2843.dfy |