@@ -95,43 +95,36 @@ class ScopeCreator final {
95
95
// Implicit nodes don't have source information for name lookup.
96
96
if (d->isImplicit ())
97
97
return false ;
98
- // Have also seen the following in an AST:
99
- // Source::
100
- //
101
- // func testInvalidKeyPathComponents() {
102
- // let _ = \.{return 0} // expected-error* {{}}
103
- // }
104
- // class X {
105
- // class var a: Int { return 1 }
106
- // }
107
- //
108
- // AST:
109
- // clang-format off
110
- // (source_file "test.swift"
111
- // (func_decl range=[test.swift:1:3 - line:3:3] "testInvalidKeyPathComponents()" interface type='() -> ()' access=internal
112
- // (parameter_list range=[test.swift:1:36 - line:1:37])
113
- // (brace_stmt range=[test.swift:1:39 - line:3:3]
114
- // (pattern_binding_decl range=[test.swift:2:5 - line:2:11]
115
- // (pattern_any)
116
- // (error_expr implicit type='<null>'))
117
- //
118
- // (pattern_binding_decl range=[test.swift:2:5 - line:2:5] <=== SOURCE RANGE WILL CONFUSE SCOPE CODE
119
- // (pattern_typed implicit type='<<error type>>'
120
- // (pattern_named '_')))
121
- // ...
122
- // clang-format on
123
- //
124
- // So test the SourceRange
125
- //
126
- // But wait!
127
- // var z = $x0 + $x1
128
- // z
129
- //
130
- // z has start == end, but must be pushed to expand source range
131
- //
132
- // So, only check source ranges for PatternBindingDecls
133
- if (isa<PatternBindingDecl>(d) && (d->getStartLoc () == d->getEndLoc ()))
134
- return false ;
98
+ // / In \c Parser::parseDeclVarGetSet fake PBDs are created. Ignore them.
99
+ // / Example:
100
+ // / \code
101
+ // / class SR10903 { static var _: Int { 0 } }
102
+ // / \endcode
103
+ if (const auto *PBD = dyn_cast<PatternBindingDecl>(d))
104
+ if (PBD->isInvalid ())
105
+ return false ;
106
+ // / In
107
+ // / \code
108
+ // / @propertyWrapper
109
+ // / public struct Wrapper<T> {
110
+ // / public var value: T
111
+ // /
112
+ // / public init(body: () -> T) {
113
+ // / self.value = body()
114
+ // / }
115
+ // / }
116
+ // /
117
+ // / let globalInt = 17
118
+ // /
119
+ // / @Wrapper(body: { globalInt })
120
+ // / public var y: Int
121
+ // / \endcode
122
+ // / I'm seeing a dumped AST include:
123
+ // / (pattern_binding_decl range=[test.swift:13:8 - line:12:29]
124
+ const auto &SM = d->getASTContext ().SourceMgr ;
125
+ assert ((d->getStartLoc ().isInvalid () ||
126
+ !SM.isBeforeInBuffer (d->getEndLoc (), d->getStartLoc ())) &&
127
+ " end-before-start will break tree search via location" );
135
128
return true ;
136
129
}
137
130
@@ -179,9 +172,10 @@ class ScopeCreator final {
179
172
function_ref<void (NullablePtr<CaptureListExpr>, ClosureExpr *)>
180
173
foundClosure);
181
174
175
+ // A safe way to discover this, without creating a circular request.
176
+ // Cannot call getAttachedPropertyWrappers.
182
177
static bool hasCustomAttribute (VarDecl *vd) {
183
- return AttachedPropertyWrapperScope::getCustomAttributesSourceRange (vd)
184
- .isValid ();
178
+ return vd->getAttrs ().getAttribute <CustomAttr>();
185
179
}
186
180
187
181
public:
@@ -190,8 +184,9 @@ class ScopeCreator final {
190
184
191
185
void createAttachedPropertyWrapperScope (PatternBindingDecl *patternBinding,
192
186
ASTScopeImpl *parent) {
187
+
193
188
patternBinding->getPattern (0 )->forEachVariable ([&](VarDecl *vd) {
194
- // assume all same as first
189
+ // assume all same as first
195
190
if (hasCustomAttribute (vd))
196
191
createSubtree<AttachedPropertyWrapperScope>(parent, vd);
197
192
});
@@ -221,7 +216,7 @@ class ScopeCreator final {
221
216
void
222
217
forEachSpecializeAttrInSourceOrder (Decl *declBeingSpecialized,
223
218
function_ref<void (SpecializeAttr *)> fn) {
224
- llvm:: SmallVector<SpecializeAttr *, 8 > sortedSpecializeAttrs;
219
+ SmallVector<SpecializeAttr *, 8 > sortedSpecializeAttrs;
225
220
for (auto *attr : declBeingSpecialized->getAttrs ()) {
226
221
if (auto *specializeAttr = dyn_cast<SpecializeAttr>(attr)) {
227
222
if (!isDuplicate (specializeAttr))
@@ -584,6 +579,8 @@ CREATES_NEW_INSERTION_POINT(TopLevelCodeScope)
584
579
585
580
NO_NEW_INSERTION_POINT(AbstractFunctionBodyScope)
586
581
NO_NEW_INSERTION_POINT(AbstractFunctionDeclScope)
582
+ NO_NEW_INSERTION_POINT(AttachedPropertyWrapperScope)
583
+
587
584
NO_NEW_INSERTION_POINT(CaptureListScope)
588
585
NO_NEW_INSERTION_POINT(CaseStmtScope)
589
586
NO_NEW_INSERTION_POINT(CatchStmtScope)
@@ -606,7 +603,6 @@ NO_EXPANSION(ClosureParametersScope)
606
603
NO_EXPANSION(SpecializeAttributeScope)
607
604
// no accessors, unlike PatternEntryUseScope
608
605
NO_EXPANSION(ConditionalClausePatternUseScope)
609
- NO_EXPANSION(AttachedPropertyWrapperScope)
610
606
NO_EXPANSION(GuardStmtUseScope)
611
607
612
608
#undef CREATES_NEW_INSERTION_POINT
@@ -744,8 +740,10 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
744
740
}
745
741
}
746
742
// Create scope for the body.
747
- if (decl->getBody ()) {
748
- if (decl->getDeclContext ()->isTypeContext ())
743
+ // We create body scopes when there is no body for source kit to complete
744
+ // erroneous code in bodies.
745
+ if (decl->getBodySourceRange ().isValid ()) {
746
+ if (AbstractFunctionBodyScope::isAMethod (decl))
749
747
scopeCreator.createSubtree <MethodBodyScope>(leaf, decl);
750
748
else
751
749
scopeCreator.createSubtree <PureFunctionBodyScope>(leaf, decl);
@@ -754,8 +752,10 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
754
752
755
753
void AbstractFunctionBodyScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
756
754
ScopeCreator &scopeCreator) {
757
- BraceStmt *braceStmt = decl->getBody ();
758
- ASTVisitorForScopeCreation ().visitBraceStmt (braceStmt, this , scopeCreator);
755
+ // We create body scopes when there is no body for source kit to complete
756
+ // erroneous code in bodies.
757
+ if (BraceStmt *braceStmt = decl->getBody ())
758
+ ASTVisitorForScopeCreation ().visitBraceStmt (braceStmt, this , scopeCreator);
759
759
}
760
760
761
761
void IfStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
@@ -898,6 +898,15 @@ void DefaultArgumentInitializerScope::
898
898
ASTVisitorForScopeCreation ().visitExpr (initExpr, this , scopeCreator);
899
899
}
900
900
901
+ void AttachedPropertyWrapperScope::
902
+ expandAScopeThatDoesNotCreateANewInsertionPoint (
903
+ ScopeCreator &scopeCreator) {
904
+ for (auto *attr : decl->getAttrs ().getAttributes <CustomAttr>()) {
905
+ if (auto *expr = attr->getArg ())
906
+ ASTVisitorForScopeCreation ().visitExpr (expr, this , scopeCreator);
907
+ }
908
+ }
909
+
901
910
#pragma mark expandScope
902
911
903
912
ASTScopeImpl *GenericTypeOrExtensionWholePortion::expandScope (
@@ -911,8 +920,7 @@ ASTScopeImpl *GenericTypeOrExtensionWholePortion::expandScope(
911
920
scope->getDecl ().get (), scope->getGenericContext ()->getGenericParams (),
912
921
scope);
913
922
if (scope->getGenericContext ()->getTrailingWhereClause ())
914
- deepestScope =
915
- scope->createTrailingWhereClauseScope (deepestScope, scopeCreator);
923
+ scope->createTrailingWhereClauseScope (deepestScope, scopeCreator);
916
924
scope->createBodyScope (deepestScope, scopeCreator);
917
925
return scope->getParent ().get ();
918
926
}
0 commit comments