-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Eliminate Observable circular reference errors via lazier TypeRefinementContext building #67642
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
Eliminate Observable circular reference errors via lazier TypeRefinementContext building #67642
Conversation
@swift-ci please smoke test |
@swift-ci please test source compatibility |
@swift-ci please smoke test Linux |
e659571
to
38b19c0
Compare
@swift-ci please smoke test |
1 similar comment
@swift-ci please smoke test |
@swift-ci please test source compatibility |
// the property wrapper attribute and use its source range to create a | ||
// TRC for the initializer expression. | ||
// | ||
// FIXME: Since we don't have an expression here, we can't build out its |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment no longer true, since we can now lazily expand the TRC for the init expr? I'm having a little trouble following whether this is handled now, but it sounds like it might be from a comment in the request implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know that this has fully addressed the issue, because it seems like we'd need to add the TRCs for the property wrapper attributes (below). I'd need to dig into rdar://77841331 to see what it would take to fix that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from the bugs I've added, I suspect this is related... this fails to type-check both before and after my PR:
@available(macOS 10.15, *)
func f() -> Int { 17 }
class X {
@available(macOS 10.14, *)
private static let value: Int = {
if #available(macOS 10.15, *) {
return f() // produces an error here saying that f() is not available
}
return 0
}()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for checking. I'll take a look at fixing that eventually!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the approach a little bit to in this commit deal with a failure in the source compatibility suite, which also fixes the example I gave above---an existing bug that's now fixed by my PR. It doesn't address rdar://77841331, though
@swift-ci please smoke test Linux |
@swift-ci please smoke test |
@swift-ci please test source compatibility |
@swift-ci please smoke test macOS |
@swift-ci please smoke test Windows |
@swift-ci please test source compatibility release |
@swift-ci please smoke test macOS |
@swift-ci please smoke test Windows |
@swift-ci please test source compatibility release |
1 similar comment
@swift-ci please test source compatibility release |
@swift-ci please smoke test Windows |
…e info Querying property wrappers involves semantic analysis that can cause cyclic references while building the type refinement context, and it's unnecessary: we need only know that these are custom attributes to incorporate their source ranges. Switch to the simpler/cheaper query. A small part of fixing the cyclic references in rdar://112079160.
Eager expansion of type refinement contexts (TRCs) for variables within pattern binding declarations is causing cyclic references in some places involving macros. Make this expansion lazy, triggered by walking into these pattern binding declarations as part of (e.g.) availability queries. Another step toward fixing the cyclic references in rdar://112079160.
… lazily The check for "has property wrappers" as part of determining whether the layout of a variable is exposed to clients can trigger reference cycles. Push this check later, which eliminates these cycles for types that aren't frozen/fixed-layout. This is a hack, not a real fix, but it eliminates the cyclic references observed in rdar://112079160.
Add a test case for Observable types that are extended from other source files. Prior to the recent changes to make `TypeRefinementContext` more lazy, this would trigger circular references through the `TypeRefinementContextBuilder`. Finishes rdar://112079160.
…lder This state is a holdover from when accessors we stored "alongside" their variable declarations, rather than contained within them. That's no longer the case, so we don't need to track this information any more.
The type refinement context builder had a bunch of logic to try to model type refinement contexts for the first variable declaration that shows up within a pattern binding declaration. Instead, model this more syntactically by creating a type refinement context for the pattern binding declaration itself. This both addresses a regression in the handling of `if #available` within a closure that's part of an initializer, and fixes a bug in the same area where similar code has explicit availability annotations.
51b7b4d
to
37959de
Compare
@swift-ci please smoke test |
@@ -749,3 +751,31 @@ ASTContext::getSwift5PlusAvailability(llvm::VersionTuple swiftVersion) { | |||
bool ASTContext::supportsVersionedAvailability() const { | |||
return minimumAvailableOSVersionForTriple(LangOpts.Target).has_value(); | |||
} | |||
|
|||
const Decl * | |||
swift::abstractSyntaxDeclForAvailableAttribute(const Decl *ConcreteSyntaxDecl) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, it feels like there's some overlap here with the parentDeclForInferredAvailability
function that I introduced. I don't know that the two can merge, but maybe parentDeclForInferredAvailability
should call this?
func f() -> Int { 17 } | ||
|
||
class StoredPropertiesWithAvailabilityInClosures { | ||
private static let value: Int = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could maybe use a test case asserting that we diagnose a reference to f
when there is no if #available
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, we actually don't diagnose a reference to f
there because it's an initializer, and those aren't exposed to the client (so they're checked at the deployment target)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So then should f have availability later than the deployment target? Otherwise, we're not necessarily testing that TRCs get built out properly for the body.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some test cases locally to check this, and will push them to a separate PR shortly.
|
||
auto nominalAccess = | ||
parent->getFormalAccessScope(/*useDC=*/nullptr, | ||
/*treatUsableFromInlineAsPublic=*/true); | ||
if (!nominalAccess.isPublic()) return false; | ||
|
||
return (parent->getAttrs().hasAttribute<FrozenAttr>() || | ||
parent->getAttrs().hasAttribute<FixedLayoutAttr>()); | ||
if (!parent->getAttrs().hasAttribute<FrozenAttr>() && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth adding a comment here pointing out that we want to check the "easy" conditions before possibly evaluating requests?
This seems to be causing some trouble on CI for macOS:
|
Instead of reverting why not just disable the new test? |
#67737 disables the new test. I filed rdar://113395709 for Doug |
Eager expansion of type refinement contexts (TRCs) for variables within pattern binding declarations is causing cyclic references in some places involving macros. Make this expansion lazy, triggered by walking into these pattern binding declarations as part of (e.g.) availability queries. Also be careful to avoid "has property wrapper" queries as part of building the type refinement context, because they tend to cause circularities.
Fixes rdar://112079160, a case where we would see circular references when extending an
@Observable
class from another source file.