Skip to content

Commit b33a202

Browse files
committed
Move if-let special case into getTypeReprOrParentPatternTypeRepr
As suggested during PR review. This mechanism seems to be general enough to fit into getTypeReprOrParentPatternRepr.
1 parent 157a644 commit b33a202

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

lib/AST/Decl.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5806,11 +5806,24 @@ void VarDecl::setNamingPattern(NamedPattern *Pat) {
58065806
}
58075807

58085808
TypeRepr *VarDecl::getTypeReprOrParentPatternTypeRepr() const {
5809-
if (auto *param = dyn_cast<ParamDecl>(this))
5810-
return param->getTypeRepr();
5811-
5812-
if (auto *parentPattern = dyn_cast_or_null<TypedPattern>(getParentPattern()))
5813-
return parentPattern->getTypeRepr();
5809+
if (auto *Param = dyn_cast<ParamDecl>(this))
5810+
return Param->getTypeRepr();
5811+
5812+
auto *ParentPattern = getParentPattern();
5813+
5814+
if (auto *TyPattern = dyn_cast_or_null<TypedPattern>(ParentPattern))
5815+
return TyPattern->getTypeRepr();
5816+
5817+
// Handle typed if/guard/while-let as a special case (e.g. `if let x: Int
5818+
// = Optional.some(4)`), since the `TypedPattern` is not the top-level
5819+
// pattern here - instead it is an implicit `OptionalSomePattern`
5820+
if (auto *SomePattern =
5821+
dyn_cast_or_null<OptionalSomePattern>(ParentPattern)) {
5822+
if (auto *TyPattern =
5823+
dyn_cast<TypedPattern>(SomePattern->getSubPattern())) {
5824+
return TyPattern->getTypeRepr();
5825+
}
5826+
}
58145827

58155828
return nullptr;
58165829
}

lib/IDE/IDETypeChecking.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -808,20 +808,6 @@ class VariableTypeCollector : public SourceEntityWalker {
808808
auto TyOffset = getTypeOffset(Buffer.str());
809809
bool HasExplicitType =
810810
VD->getTypeReprOrParentPatternTypeRepr() != nullptr;
811-
// Handle typed if/guard/while-let as a special case (e.g. `if let x: Int
812-
// = Optional.some(4)`), since the `TypedPattern` is not the top-level
813-
// pattern here - instead it is an implicit `OptionalSomePattern`
814-
if (!HasExplicitType) {
815-
if (auto *somePattern =
816-
dyn_cast_or_null<OptionalSomePattern>(VD->getParentPattern())) {
817-
if (somePattern->isImplicit()) {
818-
if (auto *typedPattern =
819-
dyn_cast<TypedPattern>(somePattern->getSubPattern())) {
820-
HasExplicitType = typedPattern->getTypeRepr() != nullptr;
821-
}
822-
}
823-
}
824-
}
825811
// Add the type information to the result list.
826812
Results.emplace_back(VarOffset, VarLength, HasExplicitType, TyOffset);
827813
}

0 commit comments

Comments
 (0)