Skip to content

AST: make isNotAfter a strict weak ordering #27805

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

Closed

Conversation

compnerd
Copy link
Member

std::stable_sort requires that the comparator provide a strict weak
ordering, which we did not previously. This was caught by MSVCPRT.

Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate.

Resolves SR-NNNN.

@compnerd
Copy link
Member Author

CC: @davidungar

@compnerd
Copy link
Member Author

@swift-ci please test

@@ -617,7 +617,7 @@ class ScopeCreator final {
#endif
ASTScopeAssert(startOrder * endOrder != -1,
"Start order contradicts end order");
return startOrder + endOrder < 1;
return startOrder < 0 && endOrder < 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a semantics change, though. Exact matching ranges, for example, should be considered "not after".

(I'm also a little confused by this whole idea. One range nested entirely inside another will trip the assert.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it was very confusing. I think that <= should maintain the strict weak ordering, so that seems like a better approach. I'll change that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you, @compnerd for working on fixing my oversight. Thank you, @jrose-apple for flagging me. I believe that this change is incorrect, but so is the existing code I wrote.

The ordering here is used for sorting, which is used in ASTScopeImpl::findChildContaining. That function compares a location against the End of the ranges only. What is needed is a factoring: a function that does the comparison and is used by both the sort routing and the findChildContaining routine. Otherwise, we'll have hiding places for bugs. The factored routine should just use the End locations. (I originally chose that because @DougGregor 's prototype used that.)

In addition to factoring (and keeping the assertion), I wonder if the assertion should be turned into a test that crashes the compiler, even in production runs. The rationale would be that a miscompile will result from incorrect lookups when the ranges overlap. What do you both think?

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'm not really certain how to refactor this. The findChildContaining simply queries the SourceManager, and the comparator doesn't really generalise very much. Did you have something in mind? I'd like to get this resolved ASAP, this is breaking the Windows build.

`std::stable_sort` requires that the comparator provide a strict weak
ordering, which we did not previously.  This was caught by MSVCPRT.
@compnerd compnerd force-pushed the strictly-strengthening-weakness branch from 7e2955a to 485bb5d Compare October 21, 2019 20:10
@compnerd
Copy link
Member Author

@swift-ci please test

Copy link
Contributor

@davidungar davidungar left a comment

Choose a reason for hiding this comment

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

As per my comment, I'm glad you are addressing this issue, but I don't think it's the right change.

@compnerd
Copy link
Member Author

@davidungar - ping? This is blocking debug builds on Windows :-(

@@ -617,7 +617,7 @@ class ScopeCreator final {
#endif
ASTScopeAssert(startOrder * endOrder != -1,
"Start order contradicts end order");
return startOrder + endOrder < 1;
return startOrder <= 0 && endOrder <= 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

After looking at this again, I'm suspecting that the right answer is an assertion that endOrder != 0 and a simple test for endOrder < 0. But I haven't tried it yet. I'll run some tests this weekend. (I would also keep the existing assertion.)

Copy link
Contributor

Choose a reason for hiding this comment

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

@compnerd I've got a slightly different solution that I like better, or I will if it passes the tests. See #28039
Once it lands, I'm hopeful it will solve this problem Thanks for your patience!

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for fixing this, I'll test it out locally.

Copy link
Contributor

Choose a reason for hiding this comment

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

Happy to do so. Thank you for your patience.

@compnerd compnerd closed this Nov 4, 2019
@compnerd compnerd deleted the strictly-strengthening-weakness branch November 4, 2019 18:15
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