-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GenC: introducing a binding to 'guard' against references created by …
…the Referentiator (#1235)
- Loading branch information
1 parent
38e8a38
commit abc79fe
Showing
27 changed files
with
232 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import stainless._ | ||
import stainless.annotation._ | ||
|
||
object InvalidReference1 { | ||
case class Ref[T](var x: T) | ||
case class Container[T](ref: Ref[T]) | ||
|
||
@cCode.`export` | ||
def test1(v: Int): Unit = { | ||
val rf = Ref(v) | ||
// Invalid reference: cannot construct an object from a mutable variable. | ||
val cont = Container(rf) | ||
} | ||
} |
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,14 @@ | ||
import stainless._ | ||
import stainless.annotation._ | ||
|
||
object InvalidReference2 { | ||
case class Ref[T](var x: T) | ||
case class Container[T](ref: Ref[T]) | ||
|
||
@cCode.`export` | ||
def test1(v: Int): Unit = { | ||
val rf = Ref(v) | ||
// Invalid reference: cannot construct an object from a mutable variable. | ||
val cont = Container({val dummy = 3; rf}) | ||
} | ||
} |
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,14 @@ | ||
import stainless._ | ||
import stainless.annotation._ | ||
|
||
object InvalidReference3 { | ||
case class Ref[T](var x: T) | ||
case class Container[T](ref: Ref[T]) | ||
|
||
@cCode.`export` | ||
def test1(v: Int): Unit = { | ||
val rf = Ref(v) | ||
// Invalid reference: cannot construct an object from a mutable variable. | ||
val cont = Container({val tmp = rf; tmp}) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,23 @@ | ||
import stainless._ | ||
import stainless.annotation.cCode | ||
import stainless.math._ | ||
|
||
object InPlaceRefFnCall1 { | ||
case class Ref[T](var x: T) | ||
case class Container[T](v: Ref[T]) | ||
|
||
@cCode.`export` | ||
def f(v: Int): Unit = wrapping { | ||
// The expression Container(Ref(v + 10)) will be converted to something like: | ||
// int32_t tmp = v + 10; | ||
// Container { .v = &tmp } | ||
placeholder(Container(Ref(v + 10))) | ||
} | ||
|
||
def placeholder(r: Container[Int]): Unit = { | ||
() | ||
} | ||
|
||
@cCode.`export` | ||
def main(): Unit = () | ||
} |
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,22 @@ | ||
import stainless._ | ||
import stainless.annotation.cCode | ||
import stainless.math._ | ||
|
||
object InPlaceRefFnCall2 { | ||
case class Ref[T](var x: T) | ||
|
||
@cCode.`export` | ||
def f(v: Int): Unit = wrapping { | ||
// The expression (456, Ref(v + 10)) will be converted to something like: | ||
// int32_t tmp = v + 10; | ||
// Tuple2 { ._1 = 456, ._2 = &tmp } | ||
placeholder((456, Ref(v + 10))) | ||
} | ||
|
||
def placeholder(r: (Int, Ref[Int])): Unit = { | ||
() | ||
} | ||
|
||
@cCode.`export` | ||
def main(): Unit = () | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,29 @@ | ||
import stainless.annotation._ | ||
import stainless.io._ | ||
|
||
object Pointer2 { | ||
|
||
case class Pointer[T](var x: T) | ||
|
||
def inc(p: Pointer[Int]): Int = { | ||
require(0 <= p.x && p.x <= 1000) | ||
p.x += 1 | ||
p.x | ||
} | ||
|
||
def f(v: Int): Int = { | ||
require(0 <= v && v <= 500) | ||
inc(Pointer(v + 42)) | ||
} | ||
|
||
@cCode.`export` | ||
def main(): Unit = { | ||
@ghost implicit val state = newState | ||
val res1 = inc(Pointer(123)) | ||
assert(res1 == 124) | ||
val res2 = f(400) | ||
assert(res2 == 443) | ||
StdOut.println(res1) | ||
StdOut.println(res2) | ||
} | ||
} |
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,25 @@ | ||
import stainless._ | ||
import stainless.annotation._ | ||
|
||
object RefInCtor { | ||
|
||
case class Ref[T](var x: T) | ||
|
||
case class Container[T](ref: Ref[T]) | ||
|
||
@cCode.`export` | ||
def test1(v: Int): Unit = { | ||
val cont = Container(Ref(v)) | ||
// The above is fine, but the following is not (see invalid/InvalidReference1) | ||
// val rf = Ref(v) | ||
// val cont = Container(rf) | ||
} | ||
|
||
@cCode.`export` | ||
def test2(v: Int): Unit = { | ||
val cont = Container({val tmp = Ref(v); tmp}) // tmp is a ref. to a mutable variable, but is local to its block. | ||
} | ||
|
||
@cCode.`export` | ||
def main(): Unit = () | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.