Skip to content

Commit 0ec7a91

Browse files
committed
[AST] Add a pointer to parent 'TapExpr' expression in 'VarDecl'
Preparation for on-demand 'VarDecl' type checking in interpolated string literals.
1 parent 6fe79fa commit 0ec7a91

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

include/swift/AST/Decl.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4894,7 +4894,7 @@ class VarDecl : public AbstractStorageDecl {
48944894
};
48954895

48964896
protected:
4897-
PointerUnion<PatternBindingDecl *, Stmt *, VarDecl *> Parent;
4897+
PointerUnion<PatternBindingDecl *, Stmt *, VarDecl *, Expr *> Parent;
48984898

48994899
VarDecl(DeclKind kind, bool isStatic, Introducer introducer,
49004900
bool isCaptureList, SourceLoc nameLoc, Identifier name,
@@ -5005,6 +5005,22 @@ class VarDecl : public AbstractStorageDecl {
50055005
Parent = v;
50065006
}
50075007

5008+
/// Returns the parent expression that owns this var decl.
5009+
///
5010+
/// Concrete use cases:
5011+
/// * 'TapExpr' for interpolated string literal expression
5012+
Expr *getParentExpr() const {
5013+
if (!Parent)
5014+
return nullptr;
5015+
return Parent.dyn_cast<Expr *>();
5016+
}
5017+
5018+
/// Set \p e to be the expression that owns this var decl.
5019+
void setParentExpr(Expr *e) {
5020+
assert(e);
5021+
Parent = e;
5022+
}
5023+
50085024
NamedPattern *getNamingPattern() const;
50095025
void setNamingPattern(NamedPattern *Pat);
50105026

lib/AST/Expr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,7 @@ TapExpr::TapExpr(Expr * SubExpr, BraceStmt *Body)
23902390
assert(!Body->empty() &&
23912391
Body->getFirstElement().isDecl(DeclKind::Var) &&
23922392
"First element of Body should be a variable to init with the subExpr");
2393+
getVar()->setParentExpr(this);
23932394
}
23942395
}
23952396

lib/Sema/TypeCheckDecl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,12 +2241,16 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
22412241

22422242
case DeclKind::Var: {
22432243
auto *VD = cast<VarDecl>(D);
2244-
auto *namingPattern = VD->getNamingPattern();
2245-
if (!namingPattern) {
2246-
return ErrorType::get(Context);
2244+
Type interfaceType;
2245+
if (auto *parentE = VD->getParentExpr()) {
2246+
interfaceType = parentE->getType();
2247+
} else if (auto *namingPattern = VD->getNamingPattern()) {
2248+
interfaceType = namingPattern->getType();
22472249
}
22482250

2249-
Type interfaceType = namingPattern->getType();
2251+
if (!interfaceType)
2252+
return ErrorType::get(Context);
2253+
22502254
if (interfaceType->hasArchetype())
22512255
interfaceType = interfaceType->mapTypeOutOfContext();
22522256

0 commit comments

Comments
 (0)