Skip to content

Commit

Permalink
TypeExpr of DynamicSelfType now working.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnno1962 committed Mar 1, 2019
1 parent 8788065 commit c703a22
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ ERROR(cannot_pass_rvalue_mutating_getter,none,
(Type))

ERROR(invalid_generic_context,none,
"generic type can not be used in this context", ())
"generic type cannot be used in this context", ())

ERROR(expression_too_complex,none,
"the compiler is unable to type-check this expression in reasonable time; "
Expand Down
22 changes: 6 additions & 16 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,22 +552,12 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {

if (!isConfused) {
if (Name == Context.Id_Self) {
// return new (Context) TypeExpr(TypeLoc::withoutLoc(
// DynamicSelfType::get(
// DC->getInnermostTypeContext()
// ->getSelfTypeInContext()
// //->getDeclaredInterfaceType()
// , Context)));
// return new (Context) TypeExpr(TypeLoc::withoutLoc(
// DC->getInnermostTypeContext()
// ->getSelfTypeInContext()));

// Type SelfType = DC->getInnermostTypeContext()->getSelfInterfaceType();
// if (ClassType::classof(SelfType.getPointer()))
// SelfType = DynamicSelfType::get(SelfType, Context);
// return new (Context) TypeExpr(TypeLoc(new (Context)
// FixedTypeRepr(DC//->getInnermostTypeContext()
// ->mapTypeIntoContext(SelfType), Loc)));
Type SelfType = DC->getInnermostTypeContext()->getSelfInterfaceType();
if (SelfType->is<ClassType>() || SelfType->is<BoundGenericClassType>())
SelfType = DynamicSelfType::get(SelfType, Context);
SelfType = DC->mapTypeIntoContext(SelfType);
return new (Context) TypeExpr(TypeLoc(new (Context)
FixedTypeRepr(SelfType, Loc), SelfType));

auto selfs = lookupUnqualified(DC, Context.Id_self, Loc, lookupOptions);
if (!selfs.empty()) {
Expand Down
40 changes: 20 additions & 20 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,28 +1253,28 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
return ErrorType::get(ctx);
}

if (comp->getIdentifier() == ctx.Id_Self &&
!isa<GenericIdentTypeRepr>(comp)) {
DeclContext *nominalDC = nullptr;
NominalTypeDecl *nominal = nullptr;
auto dc = resolution.getDeclContext();
if ((nominalDC = dc->getInnermostTypeContext()) &&
(nominal = nominalDC->getSelfNominalTypeDecl())) {
// Attempt to refer to 'Self' within a non-protocol nominal
// type. Fix this by replacing 'Self' with the nominal type name.
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");

return nominal->getDeclaredInterfaceType();
}
// Attempt to refer to 'Self' from a free function.
diags.diagnose(comp->getIdLoc(), diag::dynamic_self_non_method,
dc->getParent()->isLocalContext());

return ErrorType::get(ctx);
}

// If we found nothing, complain and give ourselves a chance to recover.
if (current.isNull()) {
if (comp->getIdentifier() == ctx.Id_Self &&
!isa<GenericIdentTypeRepr>(comp)) {
DeclContext *nominalDC = nullptr;
NominalTypeDecl *nominal = nullptr;
auto dc = resolution.getDeclContext();
if ((nominalDC = dc->getInnermostTypeContext()) &&
(nominal = nominalDC->getSelfNominalTypeDecl())) {
// Attempt to refer to 'Self' within a non-protocol nominal
// type. Fix this by replacing 'Self' with the nominal type name.
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");

return nominal->getDeclaredInterfaceType();
}
// Attempt to refer to 'Self' from a free function.
diags.diagnose(comp->getIdLoc(), diag::dynamic_self_non_method,
dc->getParent()->isLocalContext());

return ErrorType::get(ctx);
}

// If we're not allowed to complain or we couldn't fix the
// source, bail out.
if (options.contains(TypeResolutionFlags::SilenceErrors))
Expand Down

0 comments on commit c703a22

Please sign in to comment.