Skip to content

Commit 5ee5a22

Browse files
committed
[TypeChecker] NFC: Add a dedicated method to get outermost attached wrapper
The outermost wrapper is the one at index `0` in the wrapper list but it's easy for humans to make a reverse assumption since outermost is the back of the list. Let's add a dedicated method to reduce error probability of the property wrapper APIs.
1 parent 0989f43 commit 5ee5a22

File tree

7 files changed

+23
-9
lines changed

7 files changed

+23
-9
lines changed

include/swift/AST/Decl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5447,6 +5447,20 @@ class VarDecl : public AbstractStorageDecl {
54475447
/// is provided first.
54485448
llvm::TinyPtrVector<CustomAttr *> getAttachedPropertyWrappers() const;
54495449

5450+
/// Retrieve the outermost property wrapper attribute associated with
5451+
/// this declaration. For example:
5452+
///
5453+
/// \code
5454+
/// @A @B @C var <name>: Bool = ...
5455+
/// \endcode
5456+
///
5457+
/// The outermost attribute in this case is `@A` and it has
5458+
/// complete wrapper type `A<B<C<Bool>>>`.
5459+
CustomAttr *getOutermostAttachedPropertyWrapper() const {
5460+
auto wrappers = getAttachedPropertyWrappers();
5461+
return wrappers.empty() ? nullptr : wrappers.front();
5462+
}
5463+
54505464
/// Whether this property has any attached property wrappers.
54515465
bool hasAttachedPropertyWrapper() const;
54525466

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6600,7 +6600,7 @@ bool VarDecl::hasExternalPropertyWrapper() const {
66006600
return true;
66016601

66026602
// Wrappers with attribute arguments are always implementation-detail.
6603-
if (getAttachedPropertyWrappers().front()->hasArgs())
6603+
if (getOutermostAttachedPropertyWrapper()->hasArgs())
66046604
return false;
66056605

66066606
auto wrapperInfo = getAttachedPropertyWrapperTypeInfo(0);

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8578,8 +8578,8 @@ static Optional<SolutionApplicationTarget> applySolutionToInitialization(
85788578
wrappedVar, initType->mapTypeOutOfContext());
85798579

85808580
// Record the semantic initializer on the outermost property wrapper.
8581-
wrappedVar->getAttachedPropertyWrappers().front()
8582-
->setSemanticInit(initializer);
8581+
wrappedVar->getOutermostAttachedPropertyWrapper()->setSemanticInit(
8582+
initializer);
85838583

85848584
// If this is a wrapped parameter, we're done.
85858585
if (isa<ParamDecl>(wrappedVar))
@@ -8997,7 +8997,7 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
89978997
return target;
89988998
} else if (auto *wrappedVar = target.getAsUninitializedWrappedVar()) {
89998999
// Get the outermost wrapper type from the solution
9000-
auto outermostWrapper = wrappedVar->getAttachedPropertyWrappers().front();
9000+
auto outermostWrapper = wrappedVar->getOutermostAttachedPropertyWrapper();
90019001
auto backingType = solution.simplifyType(
90029002
solution.getType(outermostWrapper->getTypeExpr()));
90039003

lib/Sema/CSClosure.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class TypeVariableRefFinder : public ASTWalker {
9898

9999
if (auto *wrappedVar = var->getOriginalWrappedProperty()) {
100100
auto outermostWrapperAttr =
101-
wrappedVar->getAttachedPropertyWrappers().front();
101+
wrappedVar->getOutermostAttachedPropertyWrapper();
102102

103103
// If the attribute doesn't have a type it could only mean
104104
// that the declaration was incorrect.

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3595,7 +3595,7 @@ bool InvalidProjectedValueArgument::diagnoseAsError() {
35953595
if (!param->hasAttachedPropertyWrapper()) {
35963596
param->diagnose(diag::property_wrapper_param_no_wrapper, param->getName());
35973597
} else if (!param->hasImplicitPropertyWrapper() &&
3598-
param->getAttachedPropertyWrappers().front()->hasArgs()) {
3598+
param->getOutermostAttachedPropertyWrapper()->hasArgs()) {
35993599
param->diagnose(diag::property_wrapper_param_attr_arg);
36003600
} else {
36013601
Type backingType;

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9972,7 +9972,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
99729972
wrappedValueType = createTypeVariable(getConstraintLocator(paramDecl),
99739973
TVO_CanBindToHole | TVO_CanBindToLValue);
99749974
} else {
9975-
auto *wrapperAttr = paramDecl->getAttachedPropertyWrappers().front();
9975+
auto *wrapperAttr = paramDecl->getOutermostAttachedPropertyWrapper();
99769976
auto wrapperType = paramDecl->getAttachedPropertyWrapperType(0);
99779977
backingType = replaceInferableTypesWithTypeVars(
99789978
wrapperType, getConstraintLocator(wrapperAttr->getTypeRepr()));

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6433,9 +6433,9 @@ void ConstraintSystem::diagnoseFailureFor(SolutionApplicationTarget target) {
64336433
DE.diagnose(expr->getLoc(), diag::type_of_expression_is_ambiguous)
64346434
.highlight(expr->getSourceRange());
64356435
} else if (auto *wrappedVar = target.getAsUninitializedWrappedVar()) {
6436-
auto *wrapper = wrappedVar->getAttachedPropertyWrappers().back();
6436+
auto *outerWrapper = wrappedVar->getOutermostAttachedPropertyWrapper();
64376437
Type propertyType = wrappedVar->getInterfaceType();
6438-
Type wrapperType = wrapper->getType();
6438+
Type wrapperType = outerWrapper->getType();
64396439

64406440
// Emit the property wrapper fallback diagnostic
64416441
wrappedVar->diagnose(diag::property_wrapper_incompatible_property,

0 commit comments

Comments
 (0)