Skip to content

[CursorInfo] Implement a few expression references as solver-based #62478

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

Merged
merged 5 commits into from
Feb 10, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Dec 9, 2022

This implements cursor info resolving for a few expression types using the constraint system. This allows us to detect ambiguous results – we cannot deliver them yet but that will be done in a follow-up PR.

@ahoppen ahoppen marked this pull request as draft December 9, 2022 13:40
@ahoppen
Copy link
Member Author

ahoppen commented Dec 9, 2022

@swift-ci Please SourceKit stress test

@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from a7772e6 to 352d75f Compare December 10, 2022 11:21
@ahoppen
Copy link
Member Author

ahoppen commented Dec 10, 2022

@swift-ci Please SourceKit stress test

@ahoppen
Copy link
Member Author

ahoppen commented Dec 14, 2022

Stress tester failure at SceneDelegate.swift:unmodified:1981 is an improvement. A reduced reproducer is

protocol MyProto {
    static func makeInstance() -> Self
}

class Base: MyProto {
    static func makeInstance() -> Self {
        fatalError()
    }
}

class Inherited: Base {
    var member: Int
}

// RUN: %sourcekitd-test -req=cursor -pos=%(line + 2):30 %s -- %s
func foo() {
    Inherited.makeInstance().member = 2
}

The AST-based implementation reported Base as the receiver type, the solver-based implementation reports Inherited, which is correct.

@ahoppen
Copy link
Member Author

ahoppen commented Dec 14, 2022

Stress tester failure at SceneDelegate.swift:concurrent-708:703 is an improvement.

The AST-based implementation reported shared as a class variable, but it’s spelled as a static variable in source.

@ahoppen
Copy link
Member Author

ahoppen commented Dec 14, 2022

Stress tester failure at APIService.swift:unmodified:3766 is an improvement.

In the AST-based implementation on async in DispatchQueue.main.async no receiver was reported, the solver-based implementation reports OS_dispatch_queue.

@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from 352d75f to 50bfe6b Compare December 14, 2022 13:27
@ahoppen ahoppen changed the title WIP: [CursorInfo] Implement a few expression references as solver-based 🚥#62574 [CursorInfo] Implement a few expression references as solver-based Dec 14, 2022
@ahoppen ahoppen marked this pull request as ready for review December 14, 2022 13:59
@ahoppen ahoppen changed the title 🚥#62574 [CursorInfo] Implement a few expression references as solver-based [CursorInfo] Implement a few expression references as solver-based 🚥#62574 Dec 14, 2022
@bnbarham
Copy link
Contributor

The AST-based implementation reported Base as the receiver type, the solver-based implementation reports Inherited, which is correct.

I wouldn't expect any receiver type here, it's statically resolvable. If the old implementation is returning dynamic here I'd say that's a bug.

In the AST-based implementation on async in DispatchQueue.main.async no receiver was reported, the solver-based implementation reports OS_dispatch_queue.

Is DispatchQueue.async @objc? If it isn't then as it's defined in an extension, it isn't overridable, and thus shouldn't be dynamic or have a receiver. As far as I can tell, it isn't, though maybe we're just missing the annotation in the generated interface.

@ahoppen ahoppen changed the title [CursorInfo] Implement a few expression references as solver-based 🚥#62574 [CursorInfo] Implement a few expression references as solver-based Dec 14, 2022
@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from 50bfe6b to 7dbddfb Compare February 3, 2023 20:43
@ahoppen
Copy link
Member Author

ahoppen commented Feb 3, 2023

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch 2 times, most recently from 4c74b61 to 3649562 Compare February 4, 2023 08:00
@ahoppen
Copy link
Member Author

ahoppen commented Feb 4, 2023

@swift-ci Please SourceKit stress test

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented Feb 4, 2023

@swift-ci Please SourceKit stress test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 4, 2023

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch 2 times, most recently from c60da65 to 255fcfe Compare February 6, 2023 17:59
@ahoppen
Copy link
Member Author

ahoppen commented Feb 6, 2023

@swift-ci Please smoke test

This implements cursor info resolving for a few expression types using the constraint system. This allows us to detect ambiguous results – we cannot deliver them yet but that will be done in a follow-up PR.
@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from 255fcfe to 3d4ca87 Compare February 7, 2023 09:59
@ahoppen
Copy link
Member Author

ahoppen commented Feb 7, 2023

@swift-ci Please SourceKit stress test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 7, 2023

@swift-ci Please smoke test

Running the SourceKit stress tester with verification of solver-based cursor info returned quite a few differences but in all of them, the old AST-based implementation was actually incorrect. So, instead of verifying  the results, deliver the results from solver-baesd cursor info and only fall back to AST-based cursor info if the solver-based implementation returned no results.

rdar://103369449
@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from 3d4ca87 to b14b470 Compare February 8, 2023 11:42
@ahoppen
Copy link
Member Author

ahoppen commented Feb 8, 2023

@swift-ci Please SourceKit stress test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 8, 2023

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from b14b470 to fef7bd7 Compare February 8, 2023 20:48
@ahoppen
Copy link
Member Author

ahoppen commented Feb 8, 2023

@swift-ci Please SourceKit stress test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 8, 2023

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from fef7bd7 to 922ec03 Compare February 9, 2023 17:20
@ahoppen
Copy link
Member Author

ahoppen commented Feb 9, 2023

Note to self: The stress tester only found failures in refactoring where previously we didn’t report any cursor, so all of those are improvements.

@ahoppen
Copy link
Member Author

ahoppen commented Feb 9, 2023

@swift-ci Please smoke test

lib/AST/Decl.cpp Outdated
Comment on lines 1832 to 1834
if (!classDecl->isActor() && !isa<VarDecl>(D)) {
return StaticSpellingKind::KeywordClass;
}
Copy link
Member

Choose a reason for hiding this comment

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

Re: !isa<VarDecl>(D), does this affect class computed values? e.g.

class C {
  class var value: Int { 1 }
}

Comment on lines 931 to 933
while (auto Identity = dyn_cast<IdentityExpr>(Base)) {
Base = Identity->getSubExpr();
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
while (auto Identity = dyn_cast<IdentityExpr>(Base)) {
Base = Identity->getSubExpr();
}
Base = Base->getSemanticsProvidingExpr();

@@ -962,6 +969,8 @@ void swift::ide::getReceiverType(Expr *Base,
ReceiverTy = MetaT->getInstanceType();
else if (auto SelfT = ReceiverTy->getAs<DynamicSelfType>())
ReceiverTy = SelfT->getSelfType();
else if (auto InoutT = ReceiverTy->getAs<InOutType>())
ReceiverTy = InoutT->getObjectType();
Copy link
Member

Choose a reason for hiding this comment

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

Use ReceiverTy = ReceiverTy->getWithoutSpecifierType() that handles both LValueType and InOutType.
Also for MetatypeType, we might want to consider using ReceiverTy->getMetatypeInstanceType()

@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from 922ec03 to d9ebe09 Compare February 9, 2023 22:23
@ahoppen
Copy link
Member Author

ahoppen commented Feb 9, 2023

@swift-ci Please smoke test

ReceiverTy = SelfT->getSelfType();

ReceiverTy->getWithoutSpecifierType();
Copy link
Member

Choose a reason for hiding this comment

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

Forgot to remove?

…nd new test cases

In these cases the solver-based and AST-based cursor info differed in their results. Fix the AST-based cursor info to return the correct results and add test cases to make sure the solver-based implementation doesn’t regress them.
…if invoked on a symbol from a different module
@ahoppen ahoppen force-pushed the ahoppen/solver-based-cursor-info branch from d9ebe09 to a9cba54 Compare February 9, 2023 22:29
@ahoppen
Copy link
Member Author

ahoppen commented Feb 9, 2023

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 9, 2023

@swift-ci Please SourceKit stress test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 10, 2023

@swift-ci Please smoke test Windows

@ahoppen ahoppen merged commit fd039f4 into swiftlang:main Feb 10, 2023
@ahoppen ahoppen deleted the ahoppen/solver-based-cursor-info branch February 10, 2023 15:49
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