Skip to content

Commit d579a76

Browse files
committed
Sema: Break request cycle when building TypeRefinementContexts
This fixes a recent regression introduced in the changes to make TRC construction lazier in b33a71d. Calling VarDecl::hasInitialValue() can ask for attached property wrappers, which can trigger macro expansion. However, macro expansion can result in TRC construction, causing a cycle. Instead, just always build a lazy TRC for the initializer. It might not be needed, but it's safest to not ask anything of the VarDecl at all. Fixes rdar://118452948.
1 parent 556c503 commit d579a76

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,8 @@ class TypeRefinementContextBuilder : private ASTWalker {
525525
// get expanded from macros attached to the parent declaration. We must
526526
// not eagerly expand the attached property wrappers to avoid request
527527
// cycles.
528-
if (auto *pattern = dyn_cast<PatternBindingDecl>(D)) {
529-
if (auto firstVar = pattern->getAnchoringVarDecl(0)) {
530-
// FIXME: We could narrow this further by detecting whether there are
531-
// any macro expansions required to visit the CustomAttrs of the var.
532-
if (firstVar->hasInitialValue() && !firstVar->isInitExposedToClients())
533-
return true;
534-
}
528+
if (isa<PatternBindingDecl>(D)) {
529+
return true;
535530
}
536531

537532
return false;

test/Sema/availability_refinement_contexts.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=someStaticPropertyInferredType
1717
// CHECK-NEXT: {{^}} (decl_implicit versions=[10.51,+Inf) decl=multiPatternStaticPropertyA
1818
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=multiPatternStaticPropertyA
19-
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=someComputedProperty
19+
// CHECK-NEXT: {{^}} (decl_implicit versions=[10.51,+Inf) decl=someComputedProperty
20+
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=someComputedProperty
2021
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=someOtherMethod()
2122
@available(OSX 10.51, *)
2223
class SomeClass {
@@ -61,7 +62,8 @@ func someFunction() { }
6162

6263
// CHECK-NEXT: {{^}} (decl versions=[10.51,+Inf) decl=SomeProtocol
6364
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=protoMethod()
64-
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=protoProperty
65+
// CHECK-NEXT: {{^}} (decl_implicit versions=[10.51,+Inf) decl=protoProperty
66+
// CHECK-NEXT: {{^}} (decl versions=[10.52,+Inf) decl=protoProperty
6567
@available(OSX 10.51, *)
6668
protocol SomeProtocol {
6769
@available(OSX 10.52, *)
@@ -233,6 +235,8 @@ extension SomeClass {
233235
}()
234236
}
235237

238+
// CHECK-NEXT: {{^}} (decl_implicit versions=[10.13,+Inf) decl=wrappedValue
239+
236240
@propertyWrapper
237241
struct Wrapper<T> {
238242
var wrappedValue: T
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
import Observation
3+
4+
@available(SwiftStdlib 5.9, *)
5+
@Observable final public class AnotherObservableClass {
6+
public var name: String
7+
8+
init(name: String) {
9+
self.name = name
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: swift_swift_parser
2+
3+
// RUN: %target-swift-frontend -typecheck -parse-as-library -external-plugin-path %swift-plugin-dir#%swift-plugin-server -primary-file %s %S/Inputs/ObservableClass2.swift
4+
5+
// RUN: %target-swift-frontend -typecheck -parse-as-library -external-plugin-path %swift-plugin-dir#%swift-plugin-server %s -primary-file %S/Inputs/ObservableClass2.swift
6+
7+
// REQUIRES: observation
8+
// REQUIRES: concurrency
9+
// REQUIRES: objc_interop
10+
// UNSUPPORTED: use_os_stdlib
11+
// UNSUPPORTED: back_deployment_runtime
12+
13+
@available(SwiftStdlib 5.9, *)
14+
let x = AnotherObservableClass(name: "Hello")

0 commit comments

Comments
 (0)