Skip to content

Commit bc9f6e5

Browse files
authored
Merge pull request #10879 from jckarter/copy-key-path-subscript-labels
Push subscript label allocation for KeyPathComponents into the root constructor.
2 parents 11dc510 + 963c58c commit bc9f6e5

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

include/swift/AST/Expr.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4612,24 +4612,24 @@ class KeyPathExpr : public Expr {
46124612
Type ComponentType;
46134613
SourceLoc Loc;
46144614

4615-
explicit Component(DeclNameOrRef decl,
4615+
explicit Component(ASTContext *ctxForCopyingLabels,
4616+
DeclNameOrRef decl,
46164617
Expr *indexExpr,
46174618
ArrayRef<Identifier> subscriptLabels,
46184619
Kind kind,
46194620
Type type,
4620-
SourceLoc loc)
4621-
: Decl(decl), SubscriptIndexExprAndKind(indexExpr, kind),
4622-
SubscriptLabels(subscriptLabels),
4623-
ComponentType(type), Loc(loc)
4624-
{}
4621+
SourceLoc loc);
46254622

46264623
public:
4627-
Component() : Component({}, nullptr, {}, Kind::Invalid, Type(), SourceLoc()) {}
4624+
Component()
4625+
: Component(nullptr, {}, nullptr, {}, Kind::Invalid, Type(), SourceLoc())
4626+
{}
46284627

46294628
/// Create an unresolved component for a property.
46304629
static Component forUnresolvedProperty(DeclName UnresolvedName,
46314630
SourceLoc Loc) {
4632-
return Component(UnresolvedName, nullptr, {},
4631+
return Component(nullptr,
4632+
UnresolvedName, nullptr, {},
46334633
Kind::UnresolvedProperty,
46344634
Type(),
46354635
Loc);
@@ -4648,25 +4648,28 @@ class KeyPathExpr : public Expr {
46484648
///
46494649
/// You shouldn't add new uses of this overload; use the one that takes a
46504650
/// list of index arguments.
4651-
static Component forUnresolvedSubscriptWithPrebuiltIndexExpr(Expr *index,
4651+
static Component forUnresolvedSubscriptWithPrebuiltIndexExpr(
4652+
ASTContext &context,
4653+
Expr *index,
46524654
ArrayRef<Identifier> subscriptLabels,
46534655
SourceLoc loc) {
46544656

4655-
return Component({}, index, subscriptLabels, Kind::UnresolvedSubscript,
4657+
return Component(&context,
4658+
{}, index, subscriptLabels, Kind::UnresolvedSubscript,
46564659
Type(), loc);
46574660
}
46584661

46594662
/// Create an unresolved optional force `!` component.
46604663
static Component forUnresolvedOptionalForce(SourceLoc BangLoc) {
4661-
return Component({}, nullptr, {},
4664+
return Component(nullptr, {}, nullptr, {},
46624665
Kind::OptionalForce,
46634666
Type(),
46644667
BangLoc);
46654668
}
46664669

46674670
/// Create an unresolved optional chain `?` component.
46684671
static Component forUnresolvedOptionalChain(SourceLoc QuestionLoc) {
4669-
return Component({}, nullptr, {},
4672+
return Component(nullptr, {}, nullptr, {},
46704673
Kind::OptionalChain,
46714674
Type(),
46724675
QuestionLoc);
@@ -4676,7 +4679,7 @@ class KeyPathExpr : public Expr {
46764679
static Component forProperty(ConcreteDeclRef property,
46774680
Type propertyType,
46784681
SourceLoc loc) {
4679-
return Component(property, nullptr, {},
4682+
return Component(nullptr, property, nullptr, {},
46804683
Kind::Property,
46814684
propertyType,
46824685
loc);
@@ -4699,21 +4702,19 @@ class KeyPathExpr : public Expr {
46994702
/// list of index arguments.
47004703
static Component forSubscriptWithPrebuiltIndexExpr(
47014704
ConcreteDeclRef subscript, Expr *index, ArrayRef<Identifier> labels,
4702-
Type elementType, SourceLoc loc) {
4703-
return Component(subscript, index, {}, Kind::Subscript, elementType, loc);
4704-
}
4705+
Type elementType, SourceLoc loc);
47054706

47064707
/// Create an optional-forcing `!` component.
47074708
static Component forOptionalForce(Type forcedType, SourceLoc bangLoc) {
4708-
return Component({}, nullptr, {},
4709+
return Component(nullptr, {}, nullptr, {},
47094710
Kind::OptionalForce, forcedType,
47104711
bangLoc);
47114712
}
47124713

47134714
/// Create an optional-chaining `?` component.
47144715
static Component forOptionalChain(Type unwrappedType,
47154716
SourceLoc questionLoc) {
4716-
return Component({}, nullptr, {},
4717+
return Component(nullptr, {}, nullptr, {},
47174718
Kind::OptionalChain, unwrappedType,
47184719
questionLoc);
47194720
}
@@ -4722,7 +4723,7 @@ class KeyPathExpr : public Expr {
47224723
/// syntax but may appear when the non-optional result of an optional chain
47234724
/// is implicitly wrapped.
47244725
static Component forOptionalWrap(Type wrappedType) {
4725-
return Component({}, nullptr, {},
4726+
return Component(nullptr, {}, nullptr, {},
47264727
Kind::OptionalWrap, wrappedType,
47274728
SourceLoc());
47284729
}

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
957957
component.getLoc())
958958
: KeyPathExpr::Component
959959
::forUnresolvedSubscriptWithPrebuiltIndexExpr(
960+
E->getType()->getASTContext(),
960961
newIndex, component.getSubscriptLabels(), component.getLoc());
961962
}
962963
break;

lib/AST/Expr.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,7 +2133,7 @@ KeyPathExpr::Component::forSubscript(ASTContext &ctx,
21332133
indexArgLabelsScratch,
21342134
indexArgLabelLocsScratch);
21352135
return forSubscriptWithPrebuiltIndexExpr(subscript, index,
2136-
ctx.AllocateCopy(indexArgLabels),
2136+
indexArgLabels,
21372137
elementType,
21382138
lSquareLoc);
21392139
}
@@ -2153,7 +2153,29 @@ KeyPathExpr::Component::forUnresolvedSubscript(ASTContext &ctx,
21532153
trailingClosure, /*implicit*/ false,
21542154
indexArgLabelsScratch,
21552155
indexArgLabelLocsScratch);
2156-
return forUnresolvedSubscriptWithPrebuiltIndexExpr(index,
2157-
ctx.AllocateCopy(indexArgLabels),
2156+
return forUnresolvedSubscriptWithPrebuiltIndexExpr(ctx, index,
2157+
indexArgLabels,
21582158
lSquareLoc);
21592159
}
2160+
2161+
KeyPathExpr::Component::Component(ASTContext *ctxForCopyingLabels,
2162+
DeclNameOrRef decl,
2163+
Expr *indexExpr,
2164+
ArrayRef<Identifier> subscriptLabels,
2165+
Kind kind,
2166+
Type type,
2167+
SourceLoc loc)
2168+
: Decl(decl), SubscriptIndexExprAndKind(indexExpr, kind),
2169+
SubscriptLabels(subscriptLabels.empty()
2170+
? subscriptLabels
2171+
: ctxForCopyingLabels->AllocateCopy(subscriptLabels)),
2172+
ComponentType(type), Loc(loc)
2173+
{}
2174+
2175+
KeyPathExpr::Component
2176+
KeyPathExpr::Component::forSubscriptWithPrebuiltIndexExpr(
2177+
ConcreteDeclRef subscript, Expr *index, ArrayRef<Identifier> labels,
2178+
Type elementType, SourceLoc loc) {
2179+
return Component(&elementType->getASTContext(),
2180+
subscript, index, {}, Kind::Subscript, elementType, loc);
2181+
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,7 @@ void PreCheckExpression::resolveKeyPathExpr(KeyPathExpr *KPE) {
14651465
// .[0] or just plain [0]
14661466
components.push_back(
14671467
KeyPathExpr::Component::forUnresolvedSubscriptWithPrebuiltIndexExpr(
1468+
TC.Context,
14681469
SE->getIndex(), SE->getArgumentLabels(), SE->getLoc()));
14691470

14701471
expr = SE->getBase();

0 commit comments

Comments
 (0)