Skip to content
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

Fix #705 #706

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ trait GhostMiscTyping extends BaseTyping { this: TypeInfoImpl =>

private def illegalPreconditionNode(n: PNode): Messages = {
n match {
case PLabeledOld(PLabelUse(PLabelNode.lhsLabel), _) => noMessages
case n@ (_: POld | _: PLabeledOld) => message(n, s"old not permitted in precondition")
case n@ (_: PBefore) => message(n, s"old not permitted in precondition")
case _ => noMessages
Expand Down
75 changes: 75 additions & 0 deletions src/test/resources/regressions/issues/000705.gobra
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

package issue000705

// adapted from Viper tutorial on magic wands

type ListEntry struct {
next *ListEntry
value int
}

pred (l *ListEntry) List() {
acc(l) && (l.next != nil ==> l.next.List())
}

ghost
requires acc(l.List(), _)
pure func (l *ListEntry) Elems() (res seq[int]) {
return unfolding acc(l.List(), _) in (
l.next == nil ? seq[int]{ l.value } :
seq[int]{ l.value } ++ l.next.Elems())
}

requires l1.List() && l2.List() && l2 != nil
ensures l1.List() && l1.Elems() == old(l1.Elems() ++ l2.Elems())
func (l1 *ListEntry) append(l2 *ListEntry) {
unfold l1.List()
if l1.next == nil {
l1.next = l2
fold l1.List()
} else {
tmp := l1.next
index := 1

package tmp.List() --* (l1.List() && l1.Elems() == old(l1.Elems()[:index]) ++ old[#lhs](tmp.Elems())) {
fold l1.List()
}

newTmp, newIndex := l1.appendSubroutine(tmp, old(l1.Elems()))

unfold newTmp.List()
newTmp.next = l2
fold newTmp.List()
apply newTmp.List() --* (l1.List() && l1.Elems() == old(l1.Elems()[:newIndex]) ++ old[#lhs](newTmp.Elems()))
}
}

requires tmp.List() && /*acc(l1) && l1.next == tmp &&*/ tmp.Elems() == elems[1:]
requires tmp.List() --* (l1.List() && l1.Elems() == elems[:1] ++ old[#lhs](tmp.Elems()))
ensures 0 <= newIndex
ensures newTmp.List() && newTmp.Elems() == elems[newIndex:]
ensures unfolding newTmp.List() in newTmp.next == nil
ensures newTmp.List() --* (l1.List() && l1.Elems() == elems[:newIndex] ++ old[#lhs](newTmp.Elems()))
func (l1 *ListEntry) appendSubroutine(tmp *ListEntry, ghost elems seq[int]) (newTmp *ListEntry, newIndex int) {
index := 1

invariant 0 <= index
invariant tmp.List() && tmp.Elems() == elems[index:]
invariant tmp.List() --* (l1.List() && l1.Elems() == elems[:index] ++ old[#lhs](tmp.Elems()))
for unfolding tmp.List() in tmp.next != nil {
unfold tmp.List()
prev := tmp
tmp = tmp.next
index = index + 1
package tmp.List() --* (l1.List() && l1.Elems() == elems[:index] ++ old[#lhs](tmp.Elems())) {
fold prev.List()
apply prev.List() --* (l1.List() && l1.Elems() == elems[:index-1] ++ old[#lhs](prev.Elems()))
}
}

newTmp = tmp
newIndex = index
return
}
Loading