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

Counterexamples - fix integer parsing bug and improve code quality #2461

Merged
merged 13 commits into from
Aug 4, 2022

Conversation

Dargones
Copy link
Collaborator

@Dargones Dargones commented Jul 21, 2022

This pull request does not add any new functionality to counterexamples but instead makes several code quality improvements and bug fixes. Here is a summary:

  • Correctly handle unbounded integers, sequence indices, etc. (fixes Counterexample generation fails: integer overflow #2350)
  • Use the native Microsoft.Dafny.Type in place of DafnyModelType to improve readability and reduce the amount of string processing.
  • Escape special characters when outputting the counterexample and use UTF codes for characters outside the 32 to 126 range (this prevents some parsing issues and also disambiguates between similarly looking characters such as semicolon and greek question mark)
  • Fix edge cases in object field name and datatype destructor name identification
  • Remove dead code left from the original counterexample tool

I also added relevant tests and made a change to test generation to support the move from DafnyModelType to Microsoft.Dafny.Type

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

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.

A few changes and questions, otherwise, I'm looking forward to enhance counter-examples!

RELEASE_NOTES.md Outdated Show resolved Hide resolved
@@ -137,7 +139,7 @@ public class DafnyModel {
} else if (fn.Name.StartsWith("#") && fn.Name.IndexOf('.') != -1 && fn.Name[1] != '#') {
foreach (var tpl in fn.Apps) {
var elt = tpl.Result;
datatypeValues.Add(elt, tpl);
datatypeValues[elt] = tpl;
Copy link
Member

Choose a reason for hiding this comment

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

Before, you would be ensuring an exception if the element already existed. Can you please explain me why you are confident we don't need to check this?

Copy link
Collaborator Author

@Dargones Dargones Jul 25, 2022

Choose a reason for hiding this comment

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

Type aliasing can lead to an element being associated with different datatype functions in the Model so we should not be throwing exceptions in this case. My goal is also to prevent exceptions from arising as much as possible - in some cases, for instance, the tool might not be able to figure out the type of a variable and return UnknownType but this will not break the execution and some counterexample will still be extracted, even if a portion of it will not have complete information.

On related note, would VSCode extension crash if there were an exception in counterexample extraction? I am not aware of any situations in which this would happen (aside from the ones I fix in this PR) but adding a try-catch somewhere might still make sense to prevent issues in the future. Do you know what’s the best place for this would be?

Copy link
Member

Choose a reason for hiding this comment

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

If the language server crashes because of an unhandled exception, many times the VSCode extension will not be responsive anymore.
So I understand why you want to avoid crashes and I'm with you on this refactoring.
I'm just wondering whether if you could store the information somewhere if for any reason the same key was assigned two different values. At least for example, in a log file, or at best in some log message about the counter-example sent via the counterexample API (so that it's easier to debug, and to handle later when the UI for counterexamples will change). I think the log file would be fine.

In general it's better to add the try-catch to the closest place where the exception might be thrown, if not, where it makes the most sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding a logging functionality would make sense! I would keep that separate from this PR, if possible, but I already have some logging functionality for test generation on my fork, so I might be adding it in the near future.

@Dargones
Copy link
Collaborator Author

Dargones commented Jul 25, 2022

Thank you, @MikaelMayer! Have adopted the changes.
EDIT: it seems I still get some nullability warnings - will fix these by tomorrow.

@Dargones
Copy link
Collaborator Author

Have now fixed all nullability warnings. Let me know if you would like me to make any other changes!

MikaelMayer
MikaelMayer previously approved these changes Aug 4, 2022
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.

Great job ! Thanks for your patience.
Also, can you please confirm this enhancement is not going to break clients?

@Dargones
Copy link
Collaborator Author

Dargones commented Aug 4, 2022

Thank you, @MikaelMayer! I don't add any new functionality, so it shouldn't break anything.

@MikaelMayer MikaelMayer merged commit 80f11b6 into dafny-lang:master Aug 4, 2022
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.

Counterexample generation fails: integer overflow
2 participants