Skip to content

Commit 9004d4c

Browse files
authored
fix pretty printing of termination measures (#847)
1 parent dbb955d commit 9004d4c

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Diff for: src/main/scala/viper/silver/plugin/standard/termination/TerminationASTExtension.scala

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ case class DecreasesTuple(tupleExpressions: Seq[Exp] = Nil, override val conditi
5252

5353
override lazy val prettyPrint: PrettyPrintPrimitives#Cont = {
5454
text("decreases") <>
55-
(if (tupleExpressions.nonEmpty) space <> ssep(tupleExpressions map (toParenDoc(_)), char(',') <> space) else nil)
55+
(if (tupleExpressions.nonEmpty) space <> ssep(tupleExpressions map (toParenDoc(_)), char(',') <> space) else nil) <>
56+
(if (condition.nonEmpty) space <> "if" <+> toParenDoc(condition.get) else nil)
5657
}
5758

5859
override val extensionSubnodes: Seq[Node] = tupleExpressions ++ condition
@@ -76,7 +77,10 @@ case class DecreasesWildcard(override val condition: Option[Exp] = None)
7677

7778
override val typ: Type = Bool
7879

79-
override lazy val prettyPrint: PrettyPrintPrimitives#Cont = text("decreases _")
80+
override lazy val prettyPrint: PrettyPrintPrimitives#Cont = {
81+
text("decreases _") <>
82+
(if (condition.nonEmpty) space <> "if" <+> toParenDoc(condition.get) else nil)
83+
}
8084

8185
override val extensionSubnodes: Seq[Node] = condition.toSeq
8286

Diff for: src/test/scala/PrettyPrinterTest.scala

+40
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,45 @@ class PrettyPrinterTest extends AnyFunSuite with Matchers {
118118
assert(origString == resString)
119119
}
120120

121+
test("Test pretty printing conditional wildcard termination measures") {
122+
import viper.silver.plugin.standard.termination.DecreasesWildcard
123+
124+
val m = Method(
125+
name = "f",
126+
formalArgs = Seq.empty,
127+
formalReturns = Seq.empty,
128+
pres = Seq(DecreasesWildcard(Some(TrueLit()()))()),
129+
posts = Seq.empty,
130+
body = None
131+
)()
132+
val actual = m.toString().trim
133+
val expected =
134+
"""
135+
|method f()
136+
| decreases _ if true
137+
|""".stripMargin.trim
138+
assert(actual == expected)
139+
}
140+
141+
test("Test pretty printing conditional tuple termination measures") {
142+
import viper.silver.plugin.standard.termination.DecreasesTuple
143+
144+
val m = Method(
145+
name = "f",
146+
formalArgs = Seq.empty,
147+
formalReturns = Seq.empty,
148+
pres = Seq(DecreasesTuple(Seq(IntLit(1)()), Some(TrueLit()()))()),
149+
posts = Seq.empty,
150+
body = None
151+
)()
152+
val actual = m.toString().trim
153+
val expected =
154+
"""
155+
|method f()
156+
| decreases 1 if true
157+
|""".stripMargin.trim
158+
assert(actual == expected)
159+
}
160+
121161

122162
}

0 commit comments

Comments
 (0)