Skip to content

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

Merged
merged 7 commits into from
Aug 3, 2023

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Aug 1, 2023

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.

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

@DougGregor DougGregor force-pushed the lazy-type-refinement-context branch from e659571 to 38b19c0 Compare August 1, 2023 16:59
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@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
Copy link
Contributor

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.

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 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.

Copy link
Member Author

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
  }()
}

Copy link
Contributor

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!

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 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

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

@DougGregor DougGregor changed the title Lazy type refinement context Eliminate Observable circular reference errors via lazier TypeRefinementContext building Aug 1, 2023
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Windows

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility release

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Windows

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility release

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility release

@DougGregor
Copy link
Member Author

@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.
@DougGregor DougGregor force-pushed the lazy-type-refinement-context branch from 51b7b4d to 37959de Compare August 2, 2023 22:10
@DougGregor
Copy link
Member Author

@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) {
Copy link
Contributor

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 = {
Copy link
Contributor

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.

Copy link
Member Author

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)

Copy link
Contributor

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.

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 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>() &&
Copy link
Contributor

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?

@DougGregor DougGregor merged commit 300d04a into swiftlang:main Aug 3, 2023
@DougGregor DougGregor deleted the lazy-type-refinement-context branch August 3, 2023 19:47
@compnerd
Copy link
Member

compnerd commented Aug 4, 2023

This seems to be causing some trouble on CI for macOS:

/Users/ec2-user/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/swift/test/stdlib/Observation/Inputs/ObservableClass.swift:5:2: error: 'Observable()' is only available in macOS 14.0 or newer
@observable final public class ObservableClass {
^

@slavapestov
Copy link
Contributor

Instead of reverting why not just disable the new test?

@slavapestov
Copy link
Contributor

#67737 disables the new test. I filed rdar://113395709 for Doug

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.

4 participants