Skip to content

Commit 1b0d383

Browse files
committed
[NFC] Sema: Move diagnosis of super in illegal context from CSGen to PreCheckExpr
1 parent 7313b2f commit 1b0d383

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

lib/Sema/CSGen.cpp

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,9 +1607,22 @@ namespace {
16071607
return E->getType();
16081608

16091609
// Resolve the super type of 'self'.
1610-
return getSuperType(E->getSelf(), E->getLoc(),
1611-
diag::super_invalid_context,
1612-
diag::super_no_superclass);
1610+
auto *selfDecl = E->getSelf();
1611+
1612+
DeclContext *typeContext = selfDecl->getDeclContext()->getParent();
1613+
assert(typeContext);
1614+
1615+
auto selfTy =
1616+
CS.DC->mapTypeIntoContext(typeContext->getDeclaredInterfaceType());
1617+
auto superclassTy = selfTy->getSuperclass();
1618+
1619+
if (!superclassTy)
1620+
return Type();
1621+
1622+
if (selfDecl->getInterfaceType()->is<MetatypeType>())
1623+
superclassTy = MetatypeType::get(superclassTy);
1624+
1625+
return superclassTy;
16131626
}
16141627

16151628
Type
@@ -3298,39 +3311,6 @@ namespace {
32983311
return resultType;
32993312
}
33003313

3301-
Type getSuperType(VarDecl *selfDecl, SourceLoc diagLoc,
3302-
Diag<> diag_not_in_class,
3303-
Diag<bool, const ClassDecl *> diag_no_superclass) {
3304-
DeclContext *typeContext = selfDecl->getDeclContext()->getParent();
3305-
assert(typeContext && "constructor without parent context?!");
3306-
3307-
auto &de = CS.getASTContext().Diags;
3308-
ClassDecl *classDecl = typeContext->getSelfClassDecl();
3309-
if (!classDecl) {
3310-
de.diagnose(diagLoc, diag_not_in_class);
3311-
return Type();
3312-
}
3313-
if (!classDecl->hasSuperclass()) {
3314-
de.diagnose(
3315-
diagLoc, diag_no_superclass,
3316-
/*isExtension*/ isa<ExtensionDecl>(typeContext->getAsDecl()),
3317-
classDecl);
3318-
return Type();
3319-
}
3320-
3321-
auto selfTy = CS.DC->mapTypeIntoContext(
3322-
typeContext->getDeclaredInterfaceType());
3323-
auto superclassTy = selfTy->getSuperclass();
3324-
3325-
if (!superclassTy)
3326-
return Type();
3327-
3328-
if (selfDecl->getInterfaceType()->is<MetatypeType>())
3329-
superclassTy = MetatypeType::get(superclassTy);
3330-
3331-
return superclassTy;
3332-
}
3333-
33343314
Type visitRebindSelfInConstructorExpr(RebindSelfInConstructorExpr *expr) {
33353315
// The result is void.
33363316
return TupleType::getEmpty(CS.getASTContext());

lib/Sema/PreCheckExpr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,12 @@ VarDecl *PreCheckExpression::getImplicitSelfDeclForSuperContext(SourceLoc Loc) {
18211821
} else if (!methodContext) {
18221822
Ctx.Diags.diagnose(Loc, diag::super_invalid_context);
18231823
return nullptr;
1824+
} else if (!classDecl->hasSuperclass()) {
1825+
Ctx.Diags.diagnose(
1826+
Loc, diag::super_no_superclass,
1827+
/*isExtension*/ isa<ExtensionDecl>(typeContext->getAsDecl()),
1828+
classDecl);
1829+
return nullptr;
18241830
}
18251831
} else {
18261832
Ctx.Diags.diagnose(Loc, diag::super_invalid_context);

0 commit comments

Comments
 (0)