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

For function postcondition violations, point to the problematic expression branch #5681

Conversation

keyboardDrummer
Copy link
Member

@keyboardDrummer keyboardDrummer commented Aug 12, 2024

Description

If the result of an expression does not satisfy the constraints of its context, then an error is reported in the branch of the expression that does not satisfy the context.

Effected contexts:

  • Function postconditions
  • Witness expressions
  • Expression default values
  • Subset type checks for function call arguments
  • Assignment to a subset type local

Example of improved error reporting:

function ElseError(x: int): int // previous primary error location
  ensures ElseError(x) == 0 // related error location
{
  if x == 0 then
    0 
  else 
    1 // new primary error location
}
function ThenError(x: int): int // previous primary error location
  ensures ThenError(x) == 0 // related error location
{
  if x == 0 then
    1 // new primary error location
  else 
    0 
}

function CaseError(x: int): int // previous primary error location
  ensures CaseError(x) == 1 // related error location
{
  match x {
    case 0 => 1
    case 1 => 0 // new primary error location
    case _ => 1 
  }
}

function LetError(x: int): int // previous primary error location
  ensures LetError(x) == 1 // related error location
{
  var r := 3;
  r // new primary error location
}

How has this been tested?

  • Added CLI test ast/functions/ensuresReporting.dfy
  • ast/subsetTypes/errorReporting.dfy
  • ast/expression/functionCall.dfy
  • ast/statement/localAssignment.dfy

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@keyboardDrummer keyboardDrummer changed the title Improve error reporting for expression contexts For function postcondition violations, point to expression branch that causes the error Aug 16, 2024
@keyboardDrummer keyboardDrummer changed the title For function postcondition violations, point to expression branch that causes the error For function postcondition violations, point to the problematic expression branch Aug 16, 2024
@keyboardDrummer keyboardDrummer enabled auto-merge (squash) August 26, 2024 11:42
@@ -1526,17 +1560,17 @@ void CheckWellformedLetExprWithResult(LetExpr e, WFOptions wfOptions, Bpl.Expr r
var letBody = Substitute(e.Body, null, substMap);
CheckWellformed(letBody, wfOptions, locals, builder, etran);
if (e.Constraint_Bounds != null) {
var substMap_prime = SetupBoundVarsAsLocals(lhsVars, builder, locals, etran);
var nonGhostMap_prime = new Dictionary<IVariable, Expression>();
var substMapPrime = SetupBoundVarsAsLocals(lhsVars, builder, locals, etran);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would match better C# idioms to write.

Suggested change
var substMapPrime = SetupBoundVarsAsLocals(lhsVars, builder, locals, etran);
var substMap2 = SetupBoundVarsAsLocals(lhsVars, builder, locals, etran);

The original code looks like written by someone who missed the substMap' notation Dafny offers 😁

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really??? I don't think it improves anything. But sure I'll rename it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it improves maintainability. At first when I looked at "substMapPrime" I was thinking of prime numbers, which had nothing to do with that. Then I looked everywhere to see what "prime" was referring to and I did not find that. Then I realized it could be a character encoding of the ' and it made sense. Just want to avoid future confusion.
We could also replace the "2" by something more self-explaining, like substMapLocalVars

@keyboardDrummer keyboardDrummer removed the request for review from RustanLeino August 27, 2024 07:20
@keyboardDrummer keyboardDrummer requested review from MikaelMayer and removed request for MikaelMayer August 27, 2024 10:59
Copy link
Member

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only two comments. Still a few check warnings in BoogieExtractor.cs line 190 as well.
Otherwise looks good.

@@ -1431,16 +1431,16 @@ void AddWellformednessCheck(RedirectingTypeDecl decl) {
// parameters of the procedure
var inParams = MkTyParamFormals(decl.TypeArgs, true);
Type baseType;
Bpl.Expr wh;
Bpl.Expr whereClause;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful renaming.

// check that the witness expression checks out
witnessExpr = Substitute(decl.Constraint, decl.Var, result);
witnessExpr = decl.Constraint != null ? Substitute(decl.Constraint, decl.Var, decl.Witness) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
witnessExpr = decl.Constraint != null ? Substitute(decl.Constraint, decl.Var, decl.Witness) : null;
witnessExpr = decl.Constraint != null ? Substitute(decl.Constraint, decl.Var, decl.Witness) : decl.Witness;

?

CheckResultToBeInType(decl.tok, witness, decl.Var.Type, locals, witnessCheckBuilder, etran, $"trying witness {witnessString}: ");
witnessExpr = Substitute(decl.Constraint, decl.Var, witness);
CheckResultToBeInType(decl.tok, witness, baseType, locals, witnessCheckBuilder, etran, $"trying witness {witnessString}: ");
witnessExpr = decl.Constraint != null ? Substitute(decl.Constraint, decl.Var, witness) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as above. I'm surprised that witnessExpr can be null even if there is a witness given.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied these changes from #5547. I'm afraid I don't understand them myself. @ssomayyajula do you?

Comment on lines 1591 to 1594
// var delayer = new ReadsCheckDelayer(etran, null, locals, builderInitializationArea, constraintCheckBuilder);
// delayer.DoWithDelayedReadsChecks(false, wfo => {
// CheckWellformedAndAssume(decl.Constraint, wfo, locals, constraintCheckBuilder, etran, "predicate subtype constraint");
// });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// var delayer = new ReadsCheckDelayer(etran, null, locals, builderInitializationArea, constraintCheckBuilder);
// delayer.DoWithDelayedReadsChecks(false, wfo => {
// CheckWellformedAndAssume(decl.Constraint, wfo, locals, constraintCheckBuilder, etran, "predicate subtype constraint");
// });

Copy link
Member

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the warnings at the bottom that are unrelated to your PR will be fixed by #5694
Great job for making this to work.

@keyboardDrummer keyboardDrummer merged commit ef1e3bc into dafny-lang:master Sep 4, 2024
22 checks passed
@keyboardDrummer keyboardDrummer deleted the testRemovingAdaptBoxAndResultDescription branch September 5, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants