Skip to content

[Sema] Copy key path component types when merging solutions #38648

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jul 27, 2021

In #38389 I forgot to copy key path component types when applying a solution to the constraint system. That caused a crash in key path code completion.

Fixes rdar://81118700 [SR-14979]

@ahoppen ahoppen requested a review from rintaro July 27, 2021 13:55
@ahoppen
Copy link
Member Author

ahoppen commented Jul 27, 2021

@swift-ci Please smoke test

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

is this only a problem for result builders because of the dependent component step applying solutions before attempting dependent components?

@ahoppen ahoppen force-pushed the pr/apply-key-path-results-when-merging-solutions branch from 1055a21 to ae312b0 Compare July 28, 2021 10:50
@ahoppen
Copy link
Member Author

ahoppen commented Jul 28, 2021

IIUC this is only a problem for result builders, because we might determine the type of a key path in a dependent component. Prior to this change we weren’t copying that type over to the constraint system again to make sure it’s included in the merged solution. Thus, the type lookup would fail when doing code completion.


I updated the PR a fair bit because I forgot to reset KeyPathComponentTypes when a SolverScope is left.

@ahoppen
Copy link
Member Author

ahoppen commented Jul 28, 2021

@swift-ci Please smoke test

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

It’s too bad that result builders make this to be more complicated… That’s why I asked about this being needed for them, my follow-up was to add “scoped” cache if the answer was yes :)

@@ -570,6 +576,19 @@ ConstraintSystem::SolverScope::~SolverScope() {
}
truncate(cs.addedNodeTypes, numAddedNodeTypes);

// Remove any node types we registered.
for (unsigned i : reverse(range(numAddedKeyPathComponentTypes,
cs.addedKeyPathComponentTypes.size()))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In what situations previously assigned types could be re-assigned? Does it actually happen that constraints are generated twice for a particular key path expression in result builder body?

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 don’t know of any, I just followed the same pattern that we have for NodeTypes. I suspect that the same situations that apply for NodeTypes also apply for KeyPathComponentTypes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand, would be an interesting separate experiment to remove it from both places because i think overrides like that shouldn’t be allowed…

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 can try that in a follow-up PR.

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 just tried removing addedNodeTypes and it causes all sorts of failures in the test suite. IIUC we assign new type variables to a call expression as we try different overloads for it, for example.

If we need to keep addedNodeTypes, I would argue to also keep addedKeyPathComponentTypes so they share the same conceptual implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let’s merge this yes. I was actually talking about removing only if statement in rollback phase and leaving only .erase :)

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 tried that and it also fails. There’s one case where we just set a node’s type to the one it already has. If we remove erase it again afterwards, that causes crashes. But even when ignoring that special case, things fail. For example test/Constraints/sr13183.swift. I haven’t looked too deeply into why that is the case. You’ve probably got a better understanding of the constraint system to actually judge what’s going on there and if it’s intended.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, I going to take a closer look at that…

In 2eeff36 I forgot to copy key path component types when applying a solution to the constraint system. That caused a crash in key path code completion.

Fixes rdar://81118700 [SR-14979]
@ahoppen ahoppen force-pushed the pr/apply-key-path-results-when-merging-solutions branch from ae312b0 to f42f961 Compare July 30, 2021 09:07
@ahoppen
Copy link
Member Author

ahoppen commented Jul 30, 2021

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Jul 30, 2021

@xedin Are you OK with me merging this and looking at removing addedNodeTypes and addedKeyPathComponentTypes in a follow-up PR?

@ahoppen ahoppen merged commit b7a4822 into swiftlang:main Aug 1, 2021
@ahoppen ahoppen deleted the pr/apply-key-path-results-when-merging-solutions branch August 1, 2021 20:29
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