Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Initial work on improving scopes such that symbols are correctly removed#367

Merged
aytey merged 3 commits intostp:masterfrom
aytey:scope_cleanup
Jul 30, 2020
Merged

Initial work on improving scopes such that symbols are correctly removed#367
aytey merged 3 commits intostp:masterfrom
aytey:scope_cleanup

Conversation

@aytey
Copy link
Member

@aytey aytey commented Jul 20, 2020

Issue

As identified in #365, the creation of functions can allow for symbols to escape having their reference count decreased to zero, and therefore their lifetimes are longer than we would expect.

Example

(push 1)
(declare-fun x!2 () Bool)
(define-fun x!3 () Bool (not x!2))
(pop 1)
(assert (not x!2))
(check-sat)

After popping x!2 and x!3, it should not be possible to refer to x!2, but STP allows this.

Solution

This PR reworks the symbol/function scopes inside of STP such that popping a frame also removes the functions declared in that scope.

Discussion

I opted to keep a pointer to the "global" function context and then remove the popped functions during destruction of a frame.

An alternative would be change Cpp_interface::applyFunction to search through all of the frames (starting from the deepest) to find which to apply.

I have initially opted to tweaking how the scopes were stored, as this seemed like the less-invasive approach. Now that we have a SolverFrame class, we can improve Cpp_interface::applyFunction in the future to also use a "frame" of functions (rather than a global map).

@aytey aytey changed the title Initial work on improving scopes such that symbols are correctly moved Initial work on improving scopes such that symbols are correctly removed Jul 20, 2020
@msoos
Copy link
Member

msoos commented Jul 20, 2020

+1 looks good to me but @TrevorHansen may need to check as he is more familiar with this part of the code :)

Comment on lines 658 to 661
for (vector<std::string>::const_iterator scoped_function_name =
getFunctions().begin();
scoped_function_name != getFunctions().end(); ++scoped_function_name)
{
Copy link
Member

Choose a reason for hiding this comment

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

Feel free to use modern style if you'd like:
for (const auto& scoped_function_name : getFunctions())

scoped_function_name != getFunctions().end(); ++scoped_function_name)
{
// Find this function in the global context
std::unordered_map<std::string, Function>::iterator function_to_erase =
Copy link
Member

Choose a reason for hiding this comment

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

You could make make this:
std::unordered_map<const std::string, Function>::const_iterator function_to_erase =
or

const auto function_to_erase =

if you wanted to.

Copy link
Member

@TrevorHansen TrevorHansen left a comment

Choose a reason for hiding this comment

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

Thanks for fixing up my mistakes!~

@aytey
Copy link
Member Author

aytey commented Jul 21, 2020

@TrevorHansen for:

Feel free to use modern style if you'd like

Do you have a preference? Basically, I'm not a huge fan of auto because I feel it makes it unclear what is what.

For example, if I use an auto on the return of getFunctions and then I create an auto iterator on that return, without looking up the definition of getFunctions, I don't actually know what type you get from dereferencing the iterator.

If you don't use auto, you know from the immediate context what the types are. That being said, the CVC4 guidelines explicitly encourage the use of auto for the iterator instance.

Maybe the compromise here is that iterators should use auto but other code is "up to the developer"?

@rgov
Copy link
Member

rgov commented Jul 21, 2020

My usual approach is: when in doubt, steal from the LLVM Coding Standards or whichever major project you like. Try to match nearby code so future readers don't need to switch between styles. They have a stance on auto.

@aytey
Copy link
Member Author

aytey commented Jul 21, 2020

Shall we set up a CODING_GUIDELINES document that says "if in doubt, defer to LLVM"? Basically, I feel what's in in the LLVM document aligns with what I already wrote on auto (e.g., it is okay to use auto for iterators to make the code more concise).

@TrevorHansen
Copy link
Member

I don't mean to come across like I have strong feelings about ```auto''', or coding guidelines. I was looking for something to comment on mostly so that it was obvious that I took @andrewvaughanj 's contribution seriously enough to read through it.

If it'd make it easier for people to contribute to STP, then yes, I'm very keen on coding guidelines. Otherwise, I'm happy to let people exercise their judgement - we get so few patches through it's hard for me to imagine it all becoming chaos.

Thanks again for fixing up the function stuff!

@rgov
Copy link
Member

rgov commented Jul 30, 2020

Haven't followed the discussion closely: @andrewvaughanj if this is ready to go, please merge.

@aytey
Copy link
Member Author

aytey commented Jul 30, 2020

Haven't followed the discussion closely: @andrewvaughanj if this is ready to go, please merge.

I think this is good -- @TrevorHansen was happy + Travis is green.

Will merge!

@aytey aytey merged commit 3f78204 into stp:master Jul 30, 2020
@aytey aytey deleted the scope_cleanup branch July 30, 2020 19:09
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.

5 participants