[test] Update Index/refactoring property wrapper tests to use wrappedValue rather than value

Plus other small cleanups to comments and variable names.
This commit is contained in:
Nathan Hawes
2019-06-25 09:48:35 -07:00
parent 91e2e35a77
commit c7e8b3f693
18 changed files with 77 additions and 63 deletions

View File

@@ -264,13 +264,14 @@ struct ResolvedLoc {
/// Resolved locations also indicate the nature of the matched occurrence (e.g.
/// whether it is within active/inactive code, or a selector or string literal).
class NameMatcher: public ASTWalker {
using LocAndAttrArg = std::pair<SourceLoc, Expr *>;
SourceFile &SrcFile;
std::vector<UnresolvedLoc> LocsToResolve;
std::vector<ResolvedLoc> ResolvedLocs;
ArrayRef<Token> TokensToCheck;
llvm::Optional<LocAndAttrArg> CustomAttrDelayedArg;
/// The \c Expr argument of a parent \c CustomAttr (if one exists) and
/// the \c SourceLoc of the type name it applies to.
llvm::Optional<std::pair<SourceLoc, Expr *>> CustomAttrArg;
unsigned InactiveConfigRegionNestings = 0;
unsigned SelectorNestings = 0;

View File

@@ -1120,8 +1120,8 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
if (!Walker.walkToParameterListPre(PL))
return false;
// Walk each parameter decl, typeloc and default value.
for (auto P : *PL) {
// Walk each parameter's decl and typeloc and default value.
if (doIt(P))
return true;
@@ -1255,7 +1255,6 @@ public:
return false;
}
/// Returns true on failure
bool doIt(TypeLoc &TL) {
if (!Walker.walkToTypeLocPre(TL))
return false;

View File

@@ -571,6 +571,8 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
assert(customAttr->getArg());
if (!SemaInit->walk(*this))
return false;
// Don't walk this again via the associated PatternBindingDecl's
// initializer
ExprsToSkip.insert(SemaInit);
}
} else if (auto *Arg = customAttr->getArg()) {

View File

@@ -362,8 +362,8 @@ static std::vector<CharSourceRange> getEnumParamListInfo(SourceManager &SM,
}
bool NameMatcher::handleCustomAttrs(Decl *D) {
// CustomAttrs of non-param VarDecls are handled by their containing
// PatternBindingDecl
// CustomAttrs of non-param VarDecls are handled when this method is called
// on their containing PatternBindingDecls (see below).
if (isa<VarDecl>(D) && !isa<ParamDecl>(D))
return true;
@@ -380,9 +380,12 @@ bool NameMatcher::handleCustomAttrs(Decl *D) {
continue;
auto *Arg = customAttr->getArg();
if (auto *Repr = customAttr->getTypeLoc().getTypeRepr()) {
SWIFT_DEFER { CustomAttrDelayedArg = None; };
// Note the associated call arguments of the semantic initializer call
// in case we're resolving an explicit initializer call within the
// CustomAttr's type, e.g. on `Wrapper` in `@Wrapper(initialValue: 10)`.
SWIFT_DEFER { CustomAttrArg = None; };
if (Arg && !Arg->isImplicit())
CustomAttrDelayedArg = {Repr->getLoc(), Arg};
CustomAttrArg = {Repr->getLoc(), Arg};
if (!Repr->walk(*this))
return false;
}
@@ -619,10 +622,11 @@ bool NameMatcher::walkToTypeReprPre(TypeRepr *T) {
return false;
if (isa<ComponentIdentTypeRepr>(T)) {
// Check if we're in a CustomAttr and have associated call arguments
if (CustomAttrDelayedArg.hasValue() && CustomAttrDelayedArg->first == T->getLoc()) {
// If we're walking a CustomAttr's type we may have an associated call
// argument to resolve with from its semantic initializer.
if (CustomAttrArg.hasValue() && CustomAttrArg->first == T->getLoc()) {
tryResolve(ASTWalker::ParentTy(T), T->getLoc(), LabelRangeType::CallArg,
getCallArgLabelRanges(getSourceMgr(), CustomAttrDelayedArg->second, LabelRangeEndAt::BeforeElemStart));
getCallArgLabelRanges(getSourceMgr(), CustomAttrArg->second, LabelRangeEndAt::BeforeElemStart));
} else {
tryResolve(ASTWalker::ParentTy(T), T->getLoc());
}
@@ -788,9 +792,10 @@ bool NameMatcher::tryResolve(ASTWalker::ParentTy Node, SourceLoc NameLoc,
}
if (Range.getByteLength() > 1 && Range.str().front() == '$') {
// Also try after any leading dollar for name references of wrapped properties,
// e.g. 'foo' in '$foo' occurrences.
auto NewRange = CharSourceRange(Range.getStart().getAdvancedLoc(1), Range.getByteLength() - 1);
// Also try after any leading dollar for name references of wrapped
// properties, e.g. 'foo' in '$foo' occurrences.
auto NewRange = CharSourceRange(Range.getStart().getAdvancedLoc(1),
Range.getByteLength() - 1);
if (NewRange.getStart() == Next.Loc) {
LocsToResolve.pop_back();
ResolvedLocs.push_back({Node, NewRange, {}, LabelRangeType::None,

View File

@@ -326,6 +326,12 @@ private:
return true;
}
/// Report calls to the initializers of property wrapper types on wrapped properties.
///
/// These may be either explicit:
/// `\@Wrapper(initialialValue: 42) var x: Int`
/// or implicit:
/// `\@Wrapper var x = 10`
bool handleCustomAttrInitRefs(Decl * D) {
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
if (customAttr->isImplicit())
@@ -445,8 +451,9 @@ private:
if (!reportRef(D, Loc, Info, Data.AccKind))
return false;
// Report a reference to the underlying property if this is an explicit
// property wrapper reference, e.g. report 'foo' for a '$foo' reference.
// If this is a reference to a property wrapper backing property, report a
// reference to the wrapped property too (i.e. report an occurrence of `foo`
// in `$foo`.
if (auto *VD = dyn_cast<VarDecl>(D)) {
if (auto *Wrapped = VD->getOriginalWrappedProperty()) {
assert(Range.getByteLength() > 1 && Range.str().front() == '$');

View File

@@ -3,17 +3,17 @@
@propertyWrapper
public struct Wrapper<T> {
// CHECK: [[@LINE-1]]:15 | struct/Swift | Wrapper | [[Wrapper_USR:.*]] | Def | rel: 0
public var value: T
// CHECK: [[@LINE-1]]:14 | instance-property/Swift | value | [[value_USR:.*]] | Def,RelChild | rel: 1
public var wrappedValue: T
// CHECK: [[@LINE-1]]:14 | instance-property/Swift | wrappedValue | [[wrappedValue_USR:.*]] | Def,RelChild | rel: 1
public init(initialValue: T) {
// CHECK: [[@LINE-1]]:10 | constructor/Swift | init(initialValue:) | [[WrapperInit_USR:.*]] | Def,RelChild | rel: 1
self.value = initialValue
self.wrappedValue = initialValue
}
public init(body: () -> T) {
// CHECK: [[@LINE-1]]:10 | constructor/Swift | init(body:) | [[WrapperBodyInit_USR:.*]] | Def,RelChild | rel: 1
self.value = body()
self.wrappedValue = body()
}
}
@@ -54,12 +54,12 @@ public struct HasWrappers {
// CHECK: [[@LINE-1]]:14 | instance-property/Swift | z | [[z_USR:.*]] | Def,RelChild | rel: 1
func backingUse() {
_ = $y.value + $z.value + x + $x.value
_ = $y.wrappedValue + $z.wrappedValue + x + $x.wrappedValue
// CHECK: [[@LINE-1]]:10 | instance-property/Swift | y | [[y_USR]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-2]]:12 | instance-property/Swift | value | [[value_USR:.*]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-3]]:21 | instance-property/Swift | z | [[z_USR]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-4]]:31 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-5]]:36 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-2]]:12 | instance-property/Swift | wrappedValue | [[wrappedValue_USR:.*]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-3]]:28 | instance-property/Swift | z | [[z_USR]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-4]]:45 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
// CHECK: [[@LINE-5]]:50 | instance-property/Swift | x | [[x_USR]] | Ref,Read,RelCont | rel: 1
}
}

View File

@@ -1,8 +1,8 @@
@propertyWrapper
struct /*wrapper:def*/<base>Foo</base><T> {
public var value: T
public var wrappedValue: T
init(initialValue: T) {
value = initialValue
wrappedValue = initialValue
}
init(initialValue: T, otherThing: Bool) {
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
func baz() {
let _: /*wrapper*/<base>Foo</base><Int> = /*wrapped+1*/$foo
let _: Int = /*wrapped+1*/$foo.value
let _: Int = /*wrapped+1*/$foo.wrappedValue
let _: Int = /*wrapped*/foo
let _: /*wrapper*/<base>Foo</base><String> = $bar
}

View File

@@ -1,8 +1,8 @@
@propertyWrapper
struct /*wrapper:def*/Foo<T> {
public var value: T
public var wrappedValue: T
init(initialValue: T) {
value = initialValue
wrappedValue = initialValue
}
init(initialValue: T, otherThing: Bool) {
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
func baz() {
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
let _: Int = /*wrapped+1*/$foo.value
let _: Int = /*wrapped+1*/$foo.wrappedValue
let _: Int = /*wrapped*/foo
let _: /*wrapper*/Foo<String> = $bar
}

View File

@@ -1,8 +1,8 @@
@propertyWrapper
struct /*wrapper:def*/Foo<T> {
public var value: T
public var wrappedValue: T
init(initialValue: T) {
value = initialValue
wrappedValue = initialValue
}
init(initialValue: T, otherThing: Bool) {
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
func baz() {
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$<base>foo</base>
let _: Int = /*wrapped+1*/$<base>foo</base>.value
let _: Int = /*wrapped+1*/$<base>foo</base>.wrappedValue
let _: Int = /*wrapped*/<base>foo</base>
let _: /*wrapper*/Foo<String> = $bar
}

View File

@@ -1,12 +1,12 @@
public struct Outer {
@propertyWrapper
public struct InnerWrapper<T> {
public var value: T
public var wrappedValue: T
public /*init:def*/init(initialValue: T) {
self.value = initialValue
self.wrappedValue = initialValue
}
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) {
self.value = body()
self.wrappedValue = body()
}
}
}

View File

@@ -1,12 +1,12 @@
public struct Outer {
@propertyWrapper
public struct InnerWrapper<T> {
public var value: T
public var wrappedValue: T
public /*init:def*/<keywordBase>init</keywordBase>(<arglabel index=0>initialValue</arglabel><param index=0></param>: T) {
self.value = initialValue
self.wrappedValue = initialValue
}
public /*body:def*/init(first: Int, body: () -> T) {
self.value = body()
self.wrappedValue = body()
}
}
}

View File

@@ -1,8 +1,8 @@
@propertyWrapper
struct /*wrapper:def*/Foo2<T> {
public var value: T
public var wrappedValue: T
init(initialValue: T) {
value = initialValue
wrappedValue = initialValue
}
init(initialValue: T, otherThing: Bool) {
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
func baz() {
let _: /*wrapper*/Foo2<Int> = /*wrapped+1*/$foo
let _: Int = /*wrapped+1*/$foo.value
let _: Int = /*wrapped+1*/$foo.wrappedValue
let _: Int = /*wrapped*/foo
let _: /*wrapper*/Foo2<String> = $bar
}

View File

@@ -1,8 +1,8 @@
@propertyWrapper
struct /*wrapper:def*/Foo<T> {
public var value: T
public var wrappedValue: T
init(initialValue: T) {
value = initialValue
wrappedValue = initialValue
}
init(initialValue: T, otherThing: Bool) {
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
func baz() {
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
let _: Int = /*wrapped+1*/$foo.value
let _: Int = /*wrapped+1*/$foo.wrappedValue
let _: Int = /*wrapped*/foo
let _: /*wrapper*/Foo<String> = $bar
}

View File

@@ -1,8 +1,8 @@
@propertyWrapper
struct /*wrapper:def*/Foo<T> {
public var value: T
public var wrappedValue: T
init(initialValue: T) {
value = initialValue
wrappedValue = initialValue
}
init(initialValue: T, otherThing: Bool) {
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
func baz() {
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$descriptive
let _: Int = /*wrapped+1*/$descriptive.value
let _: Int = /*wrapped+1*/$descriptive.wrappedValue
let _: Int = /*wrapped*/descriptive
let _: /*wrapper*/Foo<String> = $bar
}

View File

@@ -1,12 +1,12 @@
public struct Outer {
@propertyWrapper
public struct InnerWrapper<T> {
public var value: T
public var wrappedValue: T
public /*init:def*/init(initialValue: T) {
self.value = initialValue
self.wrappedValue = initialValue
}
public /*body:def*/init(second: Int, head: () -> T) {
self.value = body()
self.wrappedValue = body()
}
}
}

View File

@@ -1,12 +1,12 @@
public struct Outer {
@propertyWrapper
public struct InnerWrapper<T> {
public var value: T
public var wrappedValue: T
public /*init:def*/init(somethingElse: T) {
self.value = initialValue
self.wrappedValue = initialValue
}
public /*body:def*/init(first: Int, body: () -> T) {
self.value = body()
self.wrappedValue = body()
}
}
}

View File

@@ -1,8 +1,8 @@
@propertyWrapper
struct /*wrapper:def*/Foo<T> {
public var value: T
public var wrappedValue: T
init(initialValue: T) {
value = initialValue
wrappedValue = initialValue
}
init(initialValue: T, otherThing: Bool) {
self.init(initialValue: initialValue)
@@ -42,7 +42,7 @@ struct Bar {
func baz() {
let _: /*wrapper*/Foo<Int> = /*wrapped+1*/$foo
let _: Int = /*wrapped+1*/$foo.value
let _: Int = /*wrapped+1*/$foo.wrappedValue
let _: Int = /*wrapped*/foo
let _: /*wrapper*/Foo<String> = $bar
}

View File

@@ -1,12 +1,12 @@
public struct Outer {
@propertyWrapper
public struct InnerWrapper<T> {
public var value: T
public var wrappedValue: T
public /*init:def*/init(initialValue: T) {
self.value = initialValue
self.wrappedValue = initialValue
}
public /*body:def*/init(first: Int, body: () -> T) {
self.value = body()
self.wrappedValue = body()
}
}
}