-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Forbid recursive function uses in decreases clauses (#6045)
Fixes #6043 This PR changes two verification checks into resolution checks: * use of a function in a `decreases` clause in the function's SCC * use of naked functions inside an SCC The first of these was omitted in the recent `CanCall` PR. Hence, this PR fixes that regression. <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
0fcac3a
commit 6afa6d5
Showing
53 changed files
with
338 additions
and
273 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
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
41 changes: 41 additions & 0 deletions
41
Source/DafnyCore/Resolver/DetectUnsoundFunctionReferencesVisitor.cs
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,41 @@ | ||
using System.Diagnostics.Contracts; | ||
|
||
namespace Microsoft.Dafny; | ||
|
||
class DetectUnsoundFunctionReferencesVisitor : ResolverBottomUpVisitor { | ||
private readonly ICallable context; | ||
private bool doDecreasesChecks; | ||
private DetectUnsoundFunctionReferencesVisitor(ModuleResolver resolver, ICallable context) | ||
: base(resolver) { | ||
Contract.Requires(resolver != null); | ||
Contract.Requires(context != null); | ||
this.context = context; | ||
} | ||
|
||
public static void Check(Function function, ModuleResolver resolver) { | ||
var visitor = new DetectUnsoundFunctionReferencesVisitor(resolver, function); | ||
visitor.doDecreasesChecks = false; | ||
visitor.Visit(function); | ||
visitor.doDecreasesChecks = true; | ||
visitor.Visit(function.Decreases.Expressions); | ||
} | ||
|
||
protected override void VisitOneExpr(Expression expr) { | ||
if (!doDecreasesChecks && expr is MemberSelectExpr { Member: Function fn } && ModuleDefinition.InSameSCC(context, fn)) { | ||
resolver.reporter.Error(MessageSource.Resolver, expr.Origin, | ||
"cannot use naked function in recursive setting. Possible solution: eta expansion."); | ||
} | ||
|
||
if (doDecreasesChecks && expr is FunctionCallExpr callExpr && ModuleDefinition.InSameSCC(context, callExpr.Function)) { | ||
string msg; | ||
if (context == callExpr.Function) { | ||
msg = "a decreases clause is not allowed to call the enclosing function"; | ||
} else { | ||
msg = $"the decreases clause of {context.WhatKind} '{context.NameRelativeToModule}' is not allowed to call '{callExpr.Function}', " + | ||
"because they are mutually recursive"; | ||
} | ||
|
||
resolver.reporter.Error(MessageSource.Resolver, callExpr.Origin, msg); | ||
} | ||
} | ||
} |
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,35 @@ | ||
using System.Diagnostics.Contracts; | ||
|
||
namespace Microsoft.Dafny; | ||
|
||
class ExtremeLemmaChecksVisitor : ResolverBottomUpVisitor { | ||
ExtremeLemma context; | ||
public ExtremeLemmaChecksVisitor(ModuleResolver resolver, ExtremeLemma context) | ||
: base(resolver) { | ||
Contract.Requires(resolver != null); | ||
Contract.Requires(context != null); | ||
this.context = context; | ||
} | ||
protected override void VisitOneStmt(Statement stmt) { | ||
if (stmt is CallStmt callStmt) { | ||
if (callStmt.Method is ExtremeLemma or PrefixLemma) { | ||
// all is cool | ||
} else { | ||
// the call goes from an extreme lemma context to a non-extreme-lemma callee | ||
if (ModuleDefinition.InSameSCC(context, callStmt.Method)) { | ||
// we're looking at a recursive call (to a non-extreme-lemma) | ||
resolver.reporter.Error(MessageSource.Resolver, callStmt.Origin, "a recursive call from a {0} can go only to other {0}s and prefix lemmas", context.WhatKind); | ||
} | ||
} | ||
} | ||
} | ||
protected override void VisitOneExpr(Expression expr) { | ||
if (expr is FunctionCallExpr callExpr) { | ||
// the call goes from a greatest lemma context to a non-greatest-lemma callee | ||
if (ModuleDefinition.InSameSCC(context, callExpr.Function)) { | ||
// we're looking at a recursive call (to a non-greatest-lemma) | ||
resolver.reporter.Error(MessageSource.Resolver, callExpr.Origin, "a recursive call from a greatest lemma can go only to other greatest lemmas and prefix lemmas"); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.