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 576 #577

Merged
merged 3 commits into from
Nov 16, 2022
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
4 changes: 2 additions & 2 deletions src/main/scala/viper/gobra/frontend/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ object Desugar {
val (args, argSubs) = argsWithSubs.unzip

val captured = decl match {
case d: PClosureDecl => info.capturedVariables(d)
case d: PClosureDecl => info.capturedLocalVariables(d)
case _ => Vector.empty
}
val capturedWithSubs = captured map capturedVarD
Expand Down Expand Up @@ -684,7 +684,7 @@ object Desugar {
val (args, _) = argsWithSubs.unzip

val captured = decl match {
case d: PClosureDecl => info.capturedVariables(d)
case d: PClosureDecl => info.capturedLocalVariables(d)
case _ => Vector.empty
}
val capturedWithSubs = captured map capturedVarD
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/viper/gobra/frontend/info/TypeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ trait TypeInfo extends ExternalTypeInfo {
def freeModified(n: PNode): Vector[PIdnNode]
def freeDeclared(n: PNode): Vector[PIdnNode]

def capturedVariables(decl: PClosureDecl): Vector[PIdnNode]
def capturedLocalVariables(decl: PClosureDecl): Vector[PIdnNode]
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ trait Enclosing { this: TypeInfoImpl =>
}
}

override def capturedVariables(decl: PClosureDecl): Vector[PIdnNode] =
override def capturedLocalVariables(decl: PClosureDecl): Vector[PIdnNode] =
capturedVariablesAttr(tree.parent(decl).head.asInstanceOf[PFunctionLit])
private lazy val capturedVariablesAttr: PFunctionLit => Vector[PIdnNode] = {
def capturedVar(x: PIdnNode, lit: PFunctionLit): Boolean = entity(x) match {
case _: SymbolTable.GlobalVariable => false
case r: SymbolTable.Variable => !containedIn(enclosingScope(r.rep), lit)
case _ => false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ trait ExprTyping extends BaseTyping { this: TypeInfoImpl =>
literalAssignableTo.errors(lit, simplifiedT)(n)

case f: PFunctionLit =>
capturedVariables(f.decl).flatMap(v => addressable.errors(enclosingExpr(v).get)(v)) ++
capturedLocalVariables(f.decl).flatMap(v => addressable.errors(enclosingExpr(v).get)(v)) ++
wellDefVariadicArgs(f.args) ++
f.id.fold(noMessages)(id => wellDefID(id).out)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait GhostMiscTyping extends BaseTyping { this: TypeInfoImpl =>
private[typing] def wellDefGhostMisc(misc: PGhostMisc) = misc match {
case c@PClosureSpecInstance(id, _) => resolve(id) match {
case Some(ap.Function(_, f)) => wellDefClosureSpecInstanceParams(c, f.args zip exprOrTypeType(id).asInstanceOf[FunctionT].args)
case Some(ap.Closure(_, l)) => if (c.params.isEmpty || capturedVariables(l.lit.decl).isEmpty)
case Some(ap.Closure(_, l)) => if (c.params.isEmpty || capturedLocalVariables(l.lit.decl).isEmpty)
wellDefClosureSpecInstanceParams(c, l.args zip exprOrTypeType(id).asInstanceOf[FunctionT].args)
else error(c, s"function literal ${l.lit.id.get} captures variables, so it cannot be used to derive a parametrized spec instance")
case _ => error(id, s"identifier $id does not identify a user-defined function or function literal")
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/regressions/issues/000576.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

package main

var x /*@@@*/ int = 1

func test() {
f :=
// @ requires acc(&x, _)
func /*@ g @*/ () int {
return x
}
}