Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1299,27 +1299,6 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
auto DC = resolution.getDeclContext();
auto id = comp->getIdentifier();

// Dynamic 'Self' in the result type of a function body.
if (id == ctx.Id_Self) {
if (auto *typeDC = DC->getInnermostTypeContext()) {
// FIXME: The passed-in TypeRepr should get 'typechecked' as well.
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
// while the 'Self' type is more than just a reference to a TypeDecl.
auto selfType = resolution.mapTypeIntoContext(
typeDC->getSelfInterfaceType());

// Check if we can reference Self here, and if so, what kind of Self it is.
switch (getSelfTypeKind(DC, options)) {
case SelfTypeKind::StaticSelf:
return selfType;
case SelfTypeKind::DynamicSelf:
return DynamicSelfType::get(selfType, ctx);
case SelfTypeKind::InvalidSelf:
break;
}
}
}

NameLookupOptions lookupOptions = defaultUnqualifiedLookupOptions;
if (options.contains(TypeResolutionFlags::KnownNonCascadingDependency))
lookupOptions |= NameLookupFlags::KnownPrivate;
Expand Down Expand Up @@ -1378,6 +1357,27 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,

// If we found nothing, complain and give ourselves a chance to recover.
if (current.isNull()) {
// Dynamic 'Self' in the result type of a function body.
if (id == ctx.Id_Self) {
if (auto *typeDC = DC->getInnermostTypeContext()) {
// FIXME: The passed-in TypeRepr should get 'typechecked' as well.
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
// while the 'Self' type is more than just a reference to a TypeDecl.
auto selfType = resolution.mapTypeIntoContext(
typeDC->getSelfInterfaceType());

// Check if we can reference Self here, and if so, what kind of Self it is.
switch (getSelfTypeKind(DC, options)) {
case SelfTypeKind::StaticSelf:
return selfType;
case SelfTypeKind::DynamicSelf:
return DynamicSelfType::get(selfType, ctx);
case SelfTypeKind::InvalidSelf:
break;
}
}
}

// If we're not allowed to complain or we couldn't fix the
// source, bail out.
if (options.contains(TypeResolutionFlags::SilenceErrors))
Expand Down
12 changes: 12 additions & 0 deletions test/type/self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,15 @@ class Boxer {

required init() {}
}

// https://bugs.swift.org/browse/SR-12133 - a type named 'Self' should be found first
struct OuterType {
struct `Self` {
let string: String
}

var foo: `Self`? {
let optional: String? = "foo"
return optional.map { `Self`(string: $0) }
}
}