Skip to content

Commit e08a6c1

Browse files
author
Nathan Hawes
committed
[IDE][Index][test] Update sourcekit/indexing support for latest property wrapper changes
The backing property for 'foo' is now '_foo', and the projected value '$foo'. This updates Indexing to report occurrences of foo within both $foo and _foo occurrences (rather than just $foo - the old _foo). FindRelatedIdents was similarlar updated, so it reports 'foo' ranges in both _foo and $foo. CursorInfo now reports the USR, documentation, and location of foo when invoked occurrences of $foo or _foo, but now leaves the name, type, and annotated declaration of _foo/$foo as is. Having the same USR ensures rename invoked on any of them will still rename via foo. Reporting foo's documentation comment instead is just to present something more useful to the user.
1 parent c547e68 commit e08a6c1

File tree

15 files changed

+201
-66
lines changed

15 files changed

+201
-66
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ struct PrintOptions {
299299
/// Empty means allow all.
300300
std::vector<AnyAttrKind> ExclusiveAttrList;
301301

302+
/// List of decls that should be printed even if they are implicit and \c SkipImplicit is set to true.
303+
std::vector<const Decl*> TreatAsExplicitDeclList;
304+
302305
/// Whether to print function @convention attribute on function types.
303306
bool PrintFunctionRepresentationAttrs = true;
304307

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,8 +1572,11 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
15721572
return false;
15731573
}
15741574

1575-
if (Options.SkipImplicit && D->isImplicit())
1576-
return false;
1575+
if (Options.SkipImplicit && D->isImplicit()) {
1576+
const auto &IgnoreList = Options.TreatAsExplicitDeclList;
1577+
if (std::find(IgnoreList.begin(), IgnoreList.end(), D) == IgnoreList.end())
1578+
return false;
1579+
}
15771580

15781581
if (Options.SkipUnavailable &&
15791582
D->getAttrs().isUnavailable(D->getASTContext()))

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ bool CursorInfoResolver::tryResolve(ValueDecl *D, TypeDecl *CtorTyRef,
9191
VD = Parent;
9292
}
9393
}
94-
// If this is the backing property of a property wrapper, treat it as
95-
// the wrapped value instead.
96-
if (auto *Wrapped = VD->getOriginalWrappedProperty()) {
97-
D = Wrapped;
98-
}
9994
}
10095
CursorInfo.setValueRef(D, CtorTyRef, ExtTyRef, IsRef, Ty, ContainerType);
10196
return true;
@@ -791,9 +786,10 @@ bool NameMatcher::tryResolve(ASTWalker::ParentTy Node, SourceLoc NameLoc,
791786
WasResolved = true;
792787
}
793788

794-
if (Range.getByteLength() > 1 && Range.str().front() == '$') {
795-
// Also try after any leading dollar for name references of wrapped
796-
// properties, e.g. 'foo' in '$foo' occurrences.
789+
if (Range.getByteLength() > 1 &&
790+
(Range.str().front() == '_' || Range.str().front() == '$')) {
791+
// Also try after any leading _ or $ for name references of wrapped
792+
// properties, e.g. 'foo' in '_foo' and '$foo' occurrences.
797793
auto NewRange = CharSourceRange(Range.getStart().getAdvancedLoc(1),
798794
Range.getByteLength() - 1);
799795
if (NewRange.getStart() == Next.Loc) {

lib/Index/Index.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,13 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
451451
if (!reportRef(D, Loc, Info, Data.AccKind))
452452
return false;
453453

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`.
454+
// If this is a reference to a property wrapper backing property or
455+
// projected value, report a reference to the wrapped property too (i.e.
456+
// report an occurrence of `foo` in `_foo` and '$foo').
457457
if (auto *VD = dyn_cast<VarDecl>(D)) {
458458
if (auto *Wrapped = VD->getOriginalWrappedProperty()) {
459-
assert(Range.getByteLength() > 1 && Range.str().front() == '$');
459+
assert(Range.getByteLength() > 1 &&
460+
(Range.str().front() == '_' || Range.str().front() == '$'));
460461
auto AfterDollar = Loc.getAdvancedLoc(1);
461462
reportRef(Wrapped, AfterDollar, Info, None);
462463
}

test/Index/property_wrappers.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public struct Wrapper<T> {
1515
// CHECK: [[@LINE-1]]:10 | constructor/Swift | init(body:) | [[WrapperBodyInit_USR:.*]] | Def,RelChild | rel: 1
1616
self.wrappedValue = body()
1717
}
18+
19+
public var projectedValue: Projection<T> {
20+
get { Projection(item: wrappedValue) }
21+
}
22+
}
23+
24+
public struct Projection<T> {
25+
var item: T
1826
}
1927

2028
var globalInt: Int { return 17 }
@@ -54,12 +62,13 @@ public struct HasWrappers {
5462
// CHECK: [[@LINE-1]]:14 | instance-property/Swift | z | [[z_USR:.*]] | Def,RelChild | rel: 1
5563

5664
func backingUse() {
57-
_ = $y.wrappedValue + $z.wrappedValue + x + $x.wrappedValue
65+
_ = _y.wrappedValue + _z.wrappedValue + x + _x.wrappedValue + $y.item
5866
// CHECK: [[@LINE-1]]:10 | instance-property/Swift | y | [[y_USR]] | Ref,Read,RelCont | rel: 1
5967
// CHECK: [[@LINE-2]]:12 | instance-property/Swift | wrappedValue | [[wrappedValue_USR:.*]] | Ref,Read,RelCont | rel: 1
6068
// CHECK: [[@LINE-3]]:28 | instance-property/Swift | z | [[z_USR]] | Ref,Read,RelCont | rel: 1
6169
// CHECK: [[@LINE-4]]:45 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
6270
// CHECK: [[@LINE-5]]:50 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
71+
// CHECK: [[@LINE-6]]:68 | instance-property/Swift | y | [[y_USR]] | Ref,Read,RelCont | rel: 1
6372
}
6473
}
6574

test/SourceKit/CursorInfo/cursor_info_property_wrappers.swift

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@ struct Wrapper<T> {
44
init(initialValue: T) {
55
wrappedValue = initialValue
66
}
7+
var projectedValue: Projection<T> {
8+
get { Projection(item: wrappedValue) }
9+
}
710
}
811

912
struct MyStruct {
13+
/// Here is some documentation.
1014
@Wrapper
1115
var foo: Int = 10
1216
func doStuff() {
1317
_ = foo
18+
_ = _foo
1419
_ = $foo
1520
}
1621
}
1722

23+
struct Projection<T> {
24+
var item: T
25+
}
26+
1827
// Split between custom attr and initializer
1928
extension Wrapper {
2029
init(initialValue: T, fieldNumber: Int, special: Bool = false) {
@@ -28,27 +37,33 @@ struct OtherStruct {
2837
var complex: Int = someValue
2938
}
3039

31-
// Tests that CursorInfo falls through to the wrapped property on
32-
// property wrapper backing properties occurreces, and that global
33-
// rename is reported as available on both (i.e. from foo and $foo).
40+
// Tests we get the same USR and documentation for the foo, _foo and $foo, so
41+
// that rename renames them all at the same time.
3442
//
35-
// RUN: %sourcekitd-test -req=cursor -cursor-action -pos=11:7 %s -- %s | %FileCheck -check-prefixes=CHECK,CHECK_DECL %s
36-
// RUN: %sourcekitd-test -req=cursor -cursor-action -pos=13:9 %s -- %s | %FileCheck -check-prefixes=CHECK,CHECK_REF %s
37-
// RUN: %sourcekitd-test -req=cursor -cursor-action -pos=14:9 %s -- %s | %FileCheck -check-prefixes=CHECK,CHECK_REF %s
38-
// CHECK_DECL: source.lang.swift.decl.var.instance (11:7-11:10)
39-
// CHECK_REF: source.lang.swift.ref.var.instance (11:7-11:10)
40-
// CHECK-NEXT: foo
43+
// RUN: %sourcekitd-test -req=cursor -cursor-action -pos=17:9 %s -- %s | %FileCheck -check-prefixes=CHECK,CHECK_WRAPPED %s
44+
// RUN: %sourcekitd-test -req=cursor -cursor-action -pos=18:9 %s -- %s | %FileCheck -check-prefixes=CHECK,CHECK_BACKING %s
45+
// RUN: %sourcekitd-test -req=cursor -cursor-action -pos=19:9 %s -- %s | %FileCheck -check-prefixes=CHECK,CHECK_PROJECTED %s
46+
//
47+
// CHECK: source.lang.swift.ref.var.instance (15:7-15:10)
48+
// CHECK_PROJECTED: $foo
49+
// CHECK_BACKING: _foo
50+
// CHECK_WRAPPED: foo
51+
// CHECK: s:29cursor_info_property_wrappers8MyStructV3fooSivp
52+
// CHECK_PROJECTED: Projection<Int>
53+
// CHECK_BACKING: Wrapper<Int>
54+
// CHECK_WRAPPED: Int
55+
// CHECK: <CommentParts><Abstract><Para>Here is some documentation.</Para></Abstract></CommentParts>
4156
// CHECK: ACTIONS BEGIN
4257
// CHECK: source.refactoring.kind.rename.global
43-
// CHECK: ACTIONS END
4458
//
45-
// Tests that CursofInfo finds occurrences within a property wrapper
46-
// constructor call where the arguments are split across the custom
47-
// attribute argument and the var initializer.
4859
//
49-
// RUN: %sourcekitd-test -req=cursor -pos=25:5 %s -- %s | %FileCheck -check-prefixes=CHECK2,CHECK2_DECL %s
50-
// RUN: %sourcekitd-test -req=cursor -pos=27:27 %s -- %s | %FileCheck -check-prefixes=CHECK2,CHECK2_REF %s
51-
// RUN: %sourcekitd-test -req=cursor -pos=28:24 %s -- %s | %FileCheck -check-prefixes=CHECK2,CHECK2_REF %s
52-
// CHECK2_DECL: source.lang.swift.decl.var.global (25:5-25:14)
53-
// CHECK2_REF: source.lang.swift.ref.var.global (25:5-25:14)
60+
// Tests that CursorInfo resolves occurrences within a property wrapper
61+
// constructor call where the arguments are split across the custom attribute
62+
// argument and the var initializer.
63+
//
64+
// RUN: %sourcekitd-test -req=cursor -pos=34:5 %s -- %s | %FileCheck -check-prefixes=CHECK2,CHECK2_DECL %s
65+
// RUN: %sourcekitd-test -req=cursor -pos=36:27 %s -- %s | %FileCheck -check-prefixes=CHECK2,CHECK2_REF %s
66+
// RUN: %sourcekitd-test -req=cursor -pos=37:24 %s -- %s | %FileCheck -check-prefixes=CHECK2,CHECK2_REF %s
67+
// CHECK2_DECL: source.lang.swift.decl.var.global (34:5-34:14)
68+
// CHECK2_REF: source.lang.swift.ref.var.global (34:5-34:14)
5469
// CHECK2-NEXT: someValue

test/SourceKit/RelatedIdents/related_idents.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,25 @@ struct Wrapper<T> {
5050
init(initialValue: T) {
5151
wrappedValue = initialValue
5252
}
53+
var projectedValue: Projection<T> {
54+
get { Projection(item: wrappedValue) }
55+
}
5356
}
5457

5558
struct MyStruct {
5659
@Wrapper
5760
var foo: Int = 10
5861
func doStuff() {
5962
_ = foo
63+
_ = _foo
6064
_ = $foo
6165
}
6266
}
6367

68+
struct Projection<T> {
69+
var item: T
70+
}
71+
6472
// RUN: %sourcekitd-test -req=related-idents -pos=6:17 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK1 %s
6573
// CHECK1: START RANGES
6674
// CHECK1-NEXT: 1:7 - 2
@@ -128,11 +136,13 @@ struct MyStruct {
128136

129137
// Test find-related-idents considers wrapped properties and their associated property wrapper backing properties as 'related'
130138
// but only returns the name portion of the property wrapper backing property occurrences (i.e. just the 'foo' in '$foo'),
131-
// RUN: %sourcekitd-test -req=related-idents -pos=57:7 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK10 %s
132-
// RUN: %sourcekitd-test -req=related-idents -pos=59:9 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK10 %s
133-
// RUN: %sourcekitd-test -req=related-idents -pos=60:9 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK10 %s
139+
// RUN: %sourcekitd-test -req=related-idents -pos=60:7 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK10 %s
140+
// RUN: %sourcekitd-test -req=related-idents -pos=62:9 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK10 %s
141+
// RUN: %sourcekitd-test -req=related-idents -pos=63:9 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK10 %s
142+
// RUN: %sourcekitd-test -req=related-idents -pos=64:9 %s -- -module-name related_idents %s | %FileCheck -check-prefix=CHECK10 %s
134143
// CHECK10: START RANGES
135-
// CHECK10-NEXT: 57:7 - 3
136-
// CHECK10-NEXT: 59:9 - 3
137-
// CHECK10-NEXT: 60:10 - 3
144+
// CHECK10-NEXT: 60:7 - 3
145+
// CHECK10-NEXT: 62:9 - 3
146+
// CHECK10-NEXT: 63:10 - 3
147+
// CHECK10-NEXT: 64:10 - 3
138148
// CHECK10-NEXT: END RANGES

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ struct /*wrapper:def*/<base>Foo</base><T> {
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
99
}
10+
var projectedValue: Projection<T> {
11+
get { Projection(item: wrappedValue) }
12+
}
13+
}
14+
15+
struct Projection<T> {
16+
var item: T
1017
}
1118

1219
@_functionBuilder
@@ -41,10 +48,11 @@ struct Bar {
4148
}
4249

4350
func baz() {
44-
let _: /*wrapper*/<base>Foo</base><Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.wrappedValue
51+
let _: /*wrapper*/<base>Foo</base><Int> = /*wrapped+1*/_foo
52+
let _: Int = /*wrapped+1*/_foo.wrappedValue
4653
let _: Int = /*wrapped*/foo
47-
let _: /*wrapper*/<base>Foo</base><String> = $bar
54+
let _: Projection<Int> = /*wrapped+1*/$foo
55+
let _: /*wrapper*/<base>Foo</base><String> = _bar
4856
}
4957
}
5058

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ struct /*wrapper:def*/Foo<T> {
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
99
}
10+
var projectedValue: Projection<T> {
11+
get { Projection(item: wrappedValue) }
12+
}
13+
}
14+
15+
struct Projection<T> {
16+
var item: T
1017
}
1118

1219
@_functionBuilder
@@ -41,10 +48,11 @@ struct Bar {
4148
}
4249

4350
func baz() {
44-
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.wrappedValue
51+
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/_foo
52+
let _: Int = /*wrapped+1*/_foo.wrappedValue
4653
let _: Int = /*wrapped*/foo
47-
let _: /*wrapper*/Foo<String> = $bar
54+
let _: Projection<Int> = /*wrapped+1*/$foo
55+
let _: /*wrapper*/Foo<String> = _bar
4856
}
4957
}
5058

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ struct /*wrapper:def*/Foo<T> {
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
99
}
10+
var projectedValue: Projection<T> {
11+
get { Projection(item: wrappedValue) }
12+
}
13+
}
14+
15+
struct Projection<T> {
16+
var item: T
1017
}
1118

1219
@_functionBuilder
@@ -41,10 +48,11 @@ struct Bar {
4148
}
4249

4350
func baz() {
44-
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$<base>foo</base>
45-
let _: Int = /*wrapped+1*/$<base>foo</base>.wrappedValue
51+
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/_<base>foo</base>
52+
let _: Int = /*wrapped+1*/_<base>foo</base>.wrappedValue
4653
let _: Int = /*wrapped*/<base>foo</base>
47-
let _: /*wrapper*/Foo<String> = $bar
54+
let _: Projection<Int> = /*wrapped+1*/$<base>foo</base>
55+
let _: /*wrapper*/Foo<String> = _bar
4856
}
4957
}
5058

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ struct /*wrapper:def*/Foo2<T> {
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
99
}
10+
var projectedValue: Projection<T> {
11+
get { Projection(item: wrappedValue) }
12+
}
13+
}
14+
15+
struct Projection<T> {
16+
var item: T
1017
}
1118

1219
@_functionBuilder
@@ -41,10 +48,11 @@ struct Bar {
4148
}
4249

4350
func baz() {
44-
let _: /*wrapper*/Foo2<Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.wrappedValue
51+
let _: /*wrapper*/Foo2<Int> = /*wrapped+1*/_foo
52+
let _: Int = /*wrapped+1*/_foo.wrappedValue
4653
let _: Int = /*wrapped*/foo
47-
let _: /*wrapper*/Foo2<String> = $bar
54+
let _: Projection<Int> = /*wrapped+1*/$foo
55+
let _: /*wrapper*/Foo2<String> = _bar
4856
}
4957
}
5058

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ struct /*wrapper:def*/Foo<T> {
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
99
}
10+
var projectedValue: Projection<T> {
11+
get { Projection(item: wrappedValue) }
12+
}
13+
}
14+
15+
struct Projection<T> {
16+
var item: T
1017
}
1118

1219
@_functionBuilder
@@ -41,10 +48,11 @@ struct Bar {
4148
}
4249

4350
func baz() {
44-
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
45-
let _: Int = /*wrapped+1*/$foo.wrappedValue
51+
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/_foo
52+
let _: Int = /*wrapped+1*/_foo.wrappedValue
4653
let _: Int = /*wrapped*/foo
47-
let _: /*wrapper*/Foo<String> = $bar
54+
let _: Projection<Int> = /*wrapped+1*/$foo
55+
let _: /*wrapper*/Foo<String> = _bar
4856
}
4957
}
5058

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ struct /*wrapper:def*/Foo<T> {
77
init(initialValue: T, otherThing: Bool) {
88
self.init(initialValue: initialValue)
99
}
10+
var projectedValue: Projection<T> {
11+
get { Projection(item: wrappedValue) }
12+
}
13+
}
14+
15+
struct Projection<T> {
16+
var item: T
1017
}
1118

1219
@_functionBuilder
@@ -41,10 +48,11 @@ struct Bar {
4148
}
4249

4350
func baz() {
44-
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$descriptive
45-
let _: Int = /*wrapped+1*/$descriptive.wrappedValue
51+
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/_descriptive
52+
let _: Int = /*wrapped+1*/_descriptive.wrappedValue
4653
let _: Int = /*wrapped*/descriptive
47-
let _: /*wrapper*/Foo<String> = $bar
54+
let _: Projection<Int> = /*wrapped+1*/$descriptive
55+
let _: /*wrapper*/Foo<String> = _bar
4856
}
4957
}
5058

0 commit comments

Comments
 (0)