Skip to content

Commit c7e8b3f

Browse files
author
Nathan Hawes
committed
[test] Update Index/refactoring property wrapper tests to use wrappedValue rather than value
Plus other small cleanups to comments and variable names.
1 parent 91e2e35 commit c7e8b3f

File tree

18 files changed

+77
-63
lines changed

18 files changed

+77
-63
lines changed

include/swift/IDE/Utils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,14 @@ struct ResolvedLoc {
264264
/// Resolved locations also indicate the nature of the matched occurrence (e.g.
265265
/// whether it is within active/inactive code, or a selector or string literal).
266266
class NameMatcher: public ASTWalker {
267-
using LocAndAttrArg = std::pair<SourceLoc, Expr *>;
268-
269267
SourceFile &SrcFile;
270268
std::vector<UnresolvedLoc> LocsToResolve;
271269
std::vector<ResolvedLoc> ResolvedLocs;
272270
ArrayRef<Token> TokensToCheck;
273-
llvm::Optional<LocAndAttrArg> CustomAttrDelayedArg;
271+
272+
/// The \c Expr argument of a parent \c CustomAttr (if one exists) and
273+
/// the \c SourceLoc of the type name it applies to.
274+
llvm::Optional<std::pair<SourceLoc, Expr *>> CustomAttrArg;
274275
unsigned InactiveConfigRegionNestings = 0;
275276
unsigned SelectorNestings = 0;
276277

lib/AST/ASTWalker.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,8 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
11201120
if (!Walker.walkToParameterListPre(PL))
11211121
return false;
11221122

1123-
// Walk each parameter decl, typeloc and default value.
11241123
for (auto P : *PL) {
1124+
// Walk each parameter's decl and typeloc and default value.
11251125
if (doIt(P))
11261126
return true;
11271127

@@ -1137,7 +1137,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
11371137
P->setDefaultValue(res);
11381138
}
11391139
}
1140-
1140+
11411141
return Walker.walkToParameterListPost(PL);
11421142
}
11431143

@@ -1255,7 +1255,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
12551255
return false;
12561256
}
12571257

1258-
/// Returns true on failure
12591258
bool doIt(TypeLoc &TL) {
12601259
if (!Walker.walkToTypeLocPre(TL))
12611260
return false;

lib/IDE/SourceEntityWalker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
571571
assert(customAttr->getArg());
572572
if (!SemaInit->walk(*this))
573573
return false;
574+
// Don't walk this again via the associated PatternBindingDecl's
575+
// initializer
574576
ExprsToSkip.insert(SemaInit);
575577
}
576578
} else if (auto *Arg = customAttr->getArg()) {

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ static std::vector<CharSourceRange> getEnumParamListInfo(SourceManager &SM,
362362
}
363363

364364
bool NameMatcher::handleCustomAttrs(Decl *D) {
365-
// CustomAttrs of non-param VarDecls are handled by their containing
366-
// PatternBindingDecl
365+
// CustomAttrs of non-param VarDecls are handled when this method is called
366+
// on their containing PatternBindingDecls (see below).
367367
if (isa<VarDecl>(D) && !isa<ParamDecl>(D))
368368
return true;
369369

@@ -380,9 +380,12 @@ bool NameMatcher::handleCustomAttrs(Decl *D) {
380380
continue;
381381
auto *Arg = customAttr->getArg();
382382
if (auto *Repr = customAttr->getTypeLoc().getTypeRepr()) {
383-
SWIFT_DEFER { CustomAttrDelayedArg = None; };
383+
// Note the associated call arguments of the semantic initializer call
384+
// in case we're resolving an explicit initializer call within the
385+
// CustomAttr's type, e.g. on `Wrapper` in `@Wrapper(initialValue: 10)`.
386+
SWIFT_DEFER { CustomAttrArg = None; };
384387
if (Arg && !Arg->isImplicit())
385-
CustomAttrDelayedArg = {Repr->getLoc(), Arg};
388+
CustomAttrArg = {Repr->getLoc(), Arg};
386389
if (!Repr->walk(*this))
387390
return false;
388391
}
@@ -619,10 +622,11 @@ bool NameMatcher::walkToTypeReprPre(TypeRepr *T) {
619622
return false;
620623

621624
if (isa<ComponentIdentTypeRepr>(T)) {
622-
// Check if we're in a CustomAttr and have associated call arguments
623-
if (CustomAttrDelayedArg.hasValue() && CustomAttrDelayedArg->first == T->getLoc()) {
625+
// If we're walking a CustomAttr's type we may have an associated call
626+
// argument to resolve with from its semantic initializer.
627+
if (CustomAttrArg.hasValue() && CustomAttrArg->first == T->getLoc()) {
624628
tryResolve(ASTWalker::ParentTy(T), T->getLoc(), LabelRangeType::CallArg,
625-
getCallArgLabelRanges(getSourceMgr(), CustomAttrDelayedArg->second, LabelRangeEndAt::BeforeElemStart));
629+
getCallArgLabelRanges(getSourceMgr(), CustomAttrArg->second, LabelRangeEndAt::BeforeElemStart));
626630
} else {
627631
tryResolve(ASTWalker::ParentTy(T), T->getLoc());
628632
}
@@ -788,9 +792,10 @@ bool NameMatcher::tryResolve(ASTWalker::ParentTy Node, SourceLoc NameLoc,
788792
}
789793

790794
if (Range.getByteLength() > 1 && Range.str().front() == '$') {
791-
// Also try after any leading dollar for name references of wrapped properties,
792-
// e.g. 'foo' in '$foo' occurrences.
793-
auto NewRange = CharSourceRange(Range.getStart().getAdvancedLoc(1), Range.getByteLength() - 1);
795+
// Also try after any leading dollar for name references of wrapped
796+
// properties, e.g. 'foo' in '$foo' occurrences.
797+
auto NewRange = CharSourceRange(Range.getStart().getAdvancedLoc(1),
798+
Range.getByteLength() - 1);
794799
if (NewRange.getStart() == Next.Loc) {
795800
LocsToResolve.pop_back();
796801
ResolvedLocs.push_back({Node, NewRange, {}, LabelRangeType::None,

lib/Index/Index.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
326326
return true;
327327
}
328328

329+
/// Report calls to the initializers of property wrapper types on wrapped properties.
330+
///
331+
/// These may be either explicit:
332+
/// `\@Wrapper(initialialValue: 42) var x: Int`
333+
/// or implicit:
334+
/// `\@Wrapper var x = 10`
329335
bool handleCustomAttrInitRefs(Decl * D) {
330336
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
331337
if (customAttr->isImplicit())
@@ -445,8 +451,9 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
445451
if (!reportRef(D, Loc, Info, Data.AccKind))
446452
return false;
447453

448-
// Report a reference to the underlying property if this is an explicit
449-
// property wrapper reference, e.g. report 'foo' for a '$foo' reference.
454+
// If this is a reference to a property wrapper backing property, report a
455+
// reference to the wrapped property too (i.e. report an occurrence of `foo`
456+
// in `$foo`.
450457
if (auto *VD = dyn_cast<VarDecl>(D)) {
451458
if (auto *Wrapped = VD->getOriginalWrappedProperty()) {
452459
assert(Range.getByteLength() > 1 && Range.str().front() == '$');

test/Index/property_wrappers.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
@propertyWrapper
44
public struct Wrapper<T> {
55
// CHECK: [[@LINE-1]]:15 | struct/Swift | Wrapper | [[Wrapper_USR:.*]] | Def | rel: 0
6-
public var value: T
7-
// CHECK: [[@LINE-1]]:14 | instance-property/Swift | value | [[value_USR:.*]] | Def,RelChild | rel: 1
6+
public var wrappedValue: T
7+
// CHECK: [[@LINE-1]]:14 | instance-property/Swift | wrappedValue | [[wrappedValue_USR:.*]] | Def,RelChild | rel: 1
88

99
public init(initialValue: T) {
1010
// CHECK: [[@LINE-1]]:10 | constructor/Swift | init(initialValue:) | [[WrapperInit_USR:.*]] | Def,RelChild | rel: 1
11-
self.value = initialValue
11+
self.wrappedValue = initialValue
1212
}
1313

1414
public init(body: () -> T) {
1515
// CHECK: [[@LINE-1]]:10 | constructor/Swift | init(body:) | [[WrapperBodyInit_USR:.*]] | Def,RelChild | rel: 1
16-
self.value = body()
16+
self.wrappedValue = body()
1717
}
1818
}
1919

@@ -54,12 +54,12 @@ public struct HasWrappers {
5454
// CHECK: [[@LINE-1]]:14 | instance-property/Swift | z | [[z_USR:.*]] | Def,RelChild | rel: 1
5555

5656
func backingUse() {
57-
_ = $y.value + $z.value + x + $x.value
57+
_ = $y.wrappedValue + $z.wrappedValue + x + $x.wrappedValue
5858
// CHECK: [[@LINE-1]]:10 | instance-property/Swift | y | [[y_USR]] | Ref,Read,RelCont | rel: 1
59-
// CHECK: [[@LINE-2]]:12 | instance-property/Swift | value | [[value_USR:.*]] | Ref,Read,RelCont | rel: 1
60-
// CHECK: [[@LINE-3]]:21 | instance-property/Swift | z | [[z_USR]] | Ref,Read,RelCont | rel: 1
61-
// CHECK: [[@LINE-4]]:31 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
62-
// CHECK: [[@LINE-5]]:36 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
59+
// CHECK: [[@LINE-2]]:12 | instance-property/Swift | wrappedValue | [[wrappedValue_USR:.*]] | Ref,Read,RelCont | rel: 1
60+
// CHECK: [[@LINE-3]]:28 | instance-property/Swift | z | [[z_USR]] | Ref,Read,RelCont | rel: 1
61+
// CHECK: [[@LINE-4]]:45 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
62+
// CHECK: [[@LINE-5]]:50 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
6363
}
6464
}
6565

test/refactoring/SyntacticRename/FindRangeOutputs/custom-attrs/Foo.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@propertyWrapper
22
struct /*wrapper:def*/<base>Foo</base><T> {
3-
public var value: T
3+
public var wrappedValue: T
44
init(initialValue: T) {
5-
value = initialValue
5+
wrappedValue = initialValue
66
}
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
4242

4343
func baz() {
4444
let _: /*wrapper*/<base>Foo</base><Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.value
45+
let _: Int = /*wrapped+1*/$foo.wrappedValue
4646
let _: Int = /*wrapped*/foo
4747
let _: /*wrapper*/<base>Foo</base><String> = $bar
4848
}

test/refactoring/SyntacticRename/FindRangeOutputs/custom-attrs/Other.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@propertyWrapper
22
struct /*wrapper:def*/Foo<T> {
3-
public var value: T
3+
public var wrappedValue: T
44
init(initialValue: T) {
5-
value = initialValue
5+
wrappedValue = initialValue
66
}
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
4242

4343
func baz() {
4444
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.value
45+
let _: Int = /*wrapped+1*/$foo.wrappedValue
4646
let _: Int = /*wrapped*/foo
4747
let _: /*wrapper*/Foo<String> = $bar
4848
}

test/refactoring/SyntacticRename/FindRangeOutputs/custom-attrs/wrapped.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@propertyWrapper
22
struct /*wrapper:def*/Foo<T> {
3-
public var value: T
3+
public var wrappedValue: T
44
init(initialValue: T) {
5-
value = initialValue
5+
wrappedValue = initialValue
66
}
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
4242

4343
func baz() {
4444
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$<base>foo</base>
45-
let _: Int = /*wrapped+1*/$<base>foo</base>.value
45+
let _: Int = /*wrapped+1*/$<base>foo</base>.wrappedValue
4646
let _: Int = /*wrapped*/<base>foo</base>
4747
let _: /*wrapper*/Foo<String> = $bar
4848
}

test/refactoring/SyntacticRename/FindRangeOutputs/property-wrapper-init/body.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public struct Outer {
22
@propertyWrapper
33
public struct InnerWrapper<T> {
4-
public var value: T
4+
public var wrappedValue: T
55
public /*init:def*/init(initialValue: T) {
6-
self.value = initialValue
6+
self.wrappedValue = initialValue
77
}
88
public /*body:def*/<keywordBase>init</keywordBase>(<arglabel index=0>first</arglabel><param index=0></param>: Int, <arglabel index=1>body</arglabel><param index=1></param>: () -> T) {
9-
self.value = body()
9+
self.wrappedValue = body()
1010
}
1111
}
1212
}

test/refactoring/SyntacticRename/FindRangeOutputs/property-wrapper-init/init.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public struct Outer {
22
@propertyWrapper
33
public struct InnerWrapper<T> {
4-
public var value: T
4+
public var wrappedValue: T
55
public /*init:def*/<keywordBase>init</keywordBase>(<arglabel index=0>initialValue</arglabel><param index=0></param>: T) {
6-
self.value = initialValue
6+
self.wrappedValue = initialValue
77
}
88
public /*body:def*/init(first: Int, body: () -> T) {
9-
self.value = body()
9+
self.wrappedValue = body()
1010
}
1111
}
1212
}

test/refactoring/SyntacticRename/Outputs/custom-attrs/Foo.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@propertyWrapper
22
struct /*wrapper:def*/Foo2<T> {
3-
public var value: T
3+
public var wrappedValue: T
44
init(initialValue: T) {
5-
value = initialValue
5+
wrappedValue = initialValue
66
}
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
4242

4343
func baz() {
4444
let _: /*wrapper*/Foo2<Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.value
45+
let _: Int = /*wrapped+1*/$foo.wrappedValue
4646
let _: Int = /*wrapped*/foo
4747
let _: /*wrapper*/Foo2<String> = $bar
4848
}

test/refactoring/SyntacticRename/Outputs/custom-attrs/Other.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@propertyWrapper
22
struct /*wrapper:def*/Foo<T> {
3-
public var value: T
3+
public var wrappedValue: T
44
init(initialValue: T) {
5-
value = initialValue
5+
wrappedValue = initialValue
66
}
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
4242

4343
func baz() {
4444
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.value
45+
let _: Int = /*wrapped+1*/$foo.wrappedValue
4646
let _: Int = /*wrapped*/foo
4747
let _: /*wrapper*/Foo<String> = $bar
4848
}

test/refactoring/SyntacticRename/Outputs/custom-attrs/wrapped.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@propertyWrapper
22
struct /*wrapper:def*/Foo<T> {
3-
public var value: T
3+
public var wrappedValue: T
44
init(initialValue: T) {
5-
value = initialValue
5+
wrappedValue = initialValue
66
}
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
4242

4343
func baz() {
4444
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$descriptive
45-
let _: Int = /*wrapped+1*/$descriptive.value
45+
let _: Int = /*wrapped+1*/$descriptive.wrappedValue
4646
let _: Int = /*wrapped*/descriptive
4747
let _: /*wrapper*/Foo<String> = $bar
4848
}

test/refactoring/SyntacticRename/Outputs/property-wrapper-init/body.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public struct Outer {
22
@propertyWrapper
33
public struct InnerWrapper<T> {
4-
public var value: T
4+
public var wrappedValue: T
55
public /*init:def*/init(initialValue: T) {
6-
self.value = initialValue
6+
self.wrappedValue = initialValue
77
}
88
public /*body:def*/init(second: Int, head: () -> T) {
9-
self.value = body()
9+
self.wrappedValue = body()
1010
}
1111
}
1212
}

test/refactoring/SyntacticRename/Outputs/property-wrapper-init/init.swift.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public struct Outer {
22
@propertyWrapper
33
public struct InnerWrapper<T> {
4-
public var value: T
4+
public var wrappedValue: T
55
public /*init:def*/init(somethingElse: T) {
6-
self.value = initialValue
6+
self.wrappedValue = initialValue
77
}
88
public /*body:def*/init(first: Int, body: () -> T) {
9-
self.value = body()
9+
self.wrappedValue = body()
1010
}
1111
}
1212
}

test/refactoring/SyntacticRename/custom-attrs.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@propertyWrapper
22
struct /*wrapper:def*/Foo<T> {
3-
public var value: T
3+
public var wrappedValue: T
44
init(initialValue: T) {
5-
value = initialValue
5+
wrappedValue = initialValue
66
}
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
4242

4343
func baz() {
4444
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.value
45+
let _: Int = /*wrapped+1*/$foo.wrappedValue
4646
let _: Int = /*wrapped*/foo
4747
let _: /*wrapper*/Foo<String> = $bar
4848
}

test/refactoring/SyntacticRename/property-wrapper-init.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public struct Outer {
22
@propertyWrapper
33
public struct InnerWrapper<T> {
4-
public var value: T
4+
public var wrappedValue: T
55
public /*init:def*/init(initialValue: T) {
6-
self.value = initialValue
6+
self.wrappedValue = initialValue
77
}
88
public /*body:def*/init(first: Int, body: () -> T) {
9-
self.value = body()
9+
self.wrappedValue = body()
1010
}
1111
}
1212
}

0 commit comments

Comments
 (0)