Skip to content

Commit c703a22

Browse files
committed
TypeExpr of DynamicSelfType now working.
1 parent 8788065 commit c703a22

File tree

3 files changed

+27
-37
lines changed

3 files changed

+27
-37
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ ERROR(cannot_pass_rvalue_mutating_getter,none,
163163
(Type))
164164

165165
ERROR(invalid_generic_context,none,
166-
"generic type can not be used in this context", ())
166+
"generic type cannot be used in this context", ())
167167

168168
ERROR(expression_too_complex,none,
169169
"the compiler is unable to type-check this expression in reasonable time; "

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -552,22 +552,12 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
552552

553553
if (!isConfused) {
554554
if (Name == Context.Id_Self) {
555-
// return new (Context) TypeExpr(TypeLoc::withoutLoc(
556-
// DynamicSelfType::get(
557-
// DC->getInnermostTypeContext()
558-
// ->getSelfTypeInContext()
559-
// //->getDeclaredInterfaceType()
560-
// , Context)));
561-
// return new (Context) TypeExpr(TypeLoc::withoutLoc(
562-
// DC->getInnermostTypeContext()
563-
// ->getSelfTypeInContext()));
564-
565-
// Type SelfType = DC->getInnermostTypeContext()->getSelfInterfaceType();
566-
// if (ClassType::classof(SelfType.getPointer()))
567-
// SelfType = DynamicSelfType::get(SelfType, Context);
568-
// return new (Context) TypeExpr(TypeLoc(new (Context)
569-
// FixedTypeRepr(DC//->getInnermostTypeContext()
570-
// ->mapTypeIntoContext(SelfType), Loc)));
555+
Type SelfType = DC->getInnermostTypeContext()->getSelfInterfaceType();
556+
if (SelfType->is<ClassType>() || SelfType->is<BoundGenericClassType>())
557+
SelfType = DynamicSelfType::get(SelfType, Context);
558+
SelfType = DC->mapTypeIntoContext(SelfType);
559+
return new (Context) TypeExpr(TypeLoc(new (Context)
560+
FixedTypeRepr(SelfType, Loc), SelfType));
571561

572562
auto selfs = lookupUnqualified(DC, Context.Id_self, Loc, lookupOptions);
573563
if (!selfs.empty()) {

lib/Sema/TypeCheckType.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,28 +1253,28 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
12531253
return ErrorType::get(ctx);
12541254
}
12551255

1256-
if (comp->getIdentifier() == ctx.Id_Self &&
1257-
!isa<GenericIdentTypeRepr>(comp)) {
1258-
DeclContext *nominalDC = nullptr;
1259-
NominalTypeDecl *nominal = nullptr;
1260-
auto dc = resolution.getDeclContext();
1261-
if ((nominalDC = dc->getInnermostTypeContext()) &&
1262-
(nominal = nominalDC->getSelfNominalTypeDecl())) {
1263-
// Attempt to refer to 'Self' within a non-protocol nominal
1264-
// type. Fix this by replacing 'Self' with the nominal type name.
1265-
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");
1266-
1267-
return nominal->getDeclaredInterfaceType();
1268-
}
1269-
// Attempt to refer to 'Self' from a free function.
1270-
diags.diagnose(comp->getIdLoc(), diag::dynamic_self_non_method,
1271-
dc->getParent()->isLocalContext());
1272-
1273-
return ErrorType::get(ctx);
1274-
}
1275-
12761256
// If we found nothing, complain and give ourselves a chance to recover.
12771257
if (current.isNull()) {
1258+
if (comp->getIdentifier() == ctx.Id_Self &&
1259+
!isa<GenericIdentTypeRepr>(comp)) {
1260+
DeclContext *nominalDC = nullptr;
1261+
NominalTypeDecl *nominal = nullptr;
1262+
auto dc = resolution.getDeclContext();
1263+
if ((nominalDC = dc->getInnermostTypeContext()) &&
1264+
(nominal = nominalDC->getSelfNominalTypeDecl())) {
1265+
// Attempt to refer to 'Self' within a non-protocol nominal
1266+
// type. Fix this by replacing 'Self' with the nominal type name.
1267+
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");
1268+
1269+
return nominal->getDeclaredInterfaceType();
1270+
}
1271+
// Attempt to refer to 'Self' from a free function.
1272+
diags.diagnose(comp->getIdLoc(), diag::dynamic_self_non_method,
1273+
dc->getParent()->isLocalContext());
1274+
1275+
return ErrorType::get(ctx);
1276+
}
1277+
12781278
// If we're not allowed to complain or we couldn't fix the
12791279
// source, bail out.
12801280
if (options.contains(TypeResolutionFlags::SilenceErrors))

0 commit comments

Comments
 (0)