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

Simplify generated code for variable declarations with initialization expressions #572

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

jcp19
Copy link
Contributor

@jcp19 jcp19 commented Nov 11, 2022

Shared variables

Whenever we have an assignment of the form

x@ := expr // of type T

Gobra translates this as following (internal representation)

init x
*x = expr

At the Viper level, this is expanded roughly to

// init
inhale footprint x && *x == dflt[T]

// assign
exhale footprint x
inhale footprint x
inhale *x == [ expr ]

This PR simplifies that to

// init
inhale footprint x
inhale *x === [ expr ]

Non-shared variables

x := expr // of type T

Gobra translates this as following (internal representation)

init x
x = expr

At the Viper level, this is expanded roughly to

x := dflt[T]
x := [ expr ]

Instead, this PR encodes it as follows

inhale x === [ expr ]

PS: in both cases, the new encoding is only applied if the identifier is being defined. otherwise, it uses the old encoding.

@jcp19 jcp19 marked this pull request as ready for review November 11, 2022 16:41
@jcp19 jcp19 requested review from Felalolf and Dspil November 11, 2022 16:41
@jcp19 jcp19 changed the title [WIP] Simplify generated code for variable declarations with initialization expressions Simplify generated code for variable declarations with initialization expressions Nov 11, 2022
@jcp19 jcp19 requested a review from ArquintL November 11, 2022 17:06
Copy link
Contributor

@Felalolf Felalolf left a comment

Choose a reason for hiding this comment

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

The desugarer is not the place to generate inhales to encode assignments. This is clearly part of the encoding. I liked your idea of adding an argument to the initialize node. I think you should do that instead.

@Felalolf
Copy link
Contributor

Also, I would not mix code analysis and the desugarer. The necessary information for the desugaring step should be collecting by the Info object. The main concern is that the desugarer is getting way too complicated and hard to maintain.

// assign to left (which implies exhaling and inhaling the footprint). Instead, we can just assume directly the
// equality between left and right.
val eq = in.ExprAssertion(in.GhostEqCmp(left.op, implicitConversion(right.typ, left.op.typ, right))(newInfo))(newInfo)
in.Inhale(eq)(newInfo)
Copy link
Contributor

Choose a reason for hiding this comment

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

If it is ultra urgent, then you could introduce another internal node, e.g. "InitialAssignment" and then replace the inhales with that. That would still be an ugly solution, but at it maintaines the separation of purposes somewhat.

@jcp19 jcp19 marked this pull request as draft December 13, 2022 12:43
@jcp19
Copy link
Contributor Author

jcp19 commented Feb 14, 2023

This may be a nice use-case for quasi-havoc

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