Skip to content

Commit 8f99e2c

Browse files
authored
Merge pull request #29900 from slavapestov/self-but-not-self-5.2
Sema: Prefer an outer type named 'Self' over the SE-0068 behavior [5.2]
2 parents 60c9107 + 42fbd51 commit 8f99e2c

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,27 +1299,6 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
12991299
auto DC = resolution.getDeclContext();
13001300
auto id = comp->getIdentifier();
13011301

1302-
// Dynamic 'Self' in the result type of a function body.
1303-
if (id == ctx.Id_Self) {
1304-
if (auto *typeDC = DC->getInnermostTypeContext()) {
1305-
// FIXME: The passed-in TypeRepr should get 'typechecked' as well.
1306-
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
1307-
// while the 'Self' type is more than just a reference to a TypeDecl.
1308-
auto selfType = resolution.mapTypeIntoContext(
1309-
typeDC->getSelfInterfaceType());
1310-
1311-
// Check if we can reference Self here, and if so, what kind of Self it is.
1312-
switch (getSelfTypeKind(DC, options)) {
1313-
case SelfTypeKind::StaticSelf:
1314-
return selfType;
1315-
case SelfTypeKind::DynamicSelf:
1316-
return DynamicSelfType::get(selfType, ctx);
1317-
case SelfTypeKind::InvalidSelf:
1318-
break;
1319-
}
1320-
}
1321-
}
1322-
13231302
NameLookupOptions lookupOptions = defaultUnqualifiedLookupOptions;
13241303
if (options.contains(TypeResolutionFlags::KnownNonCascadingDependency))
13251304
lookupOptions |= NameLookupFlags::KnownPrivate;
@@ -1378,6 +1357,27 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
13781357

13791358
// If we found nothing, complain and give ourselves a chance to recover.
13801359
if (current.isNull()) {
1360+
// Dynamic 'Self' in the result type of a function body.
1361+
if (id == ctx.Id_Self) {
1362+
if (auto *typeDC = DC->getInnermostTypeContext()) {
1363+
// FIXME: The passed-in TypeRepr should get 'typechecked' as well.
1364+
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
1365+
// while the 'Self' type is more than just a reference to a TypeDecl.
1366+
auto selfType = resolution.mapTypeIntoContext(
1367+
typeDC->getSelfInterfaceType());
1368+
1369+
// Check if we can reference Self here, and if so, what kind of Self it is.
1370+
switch (getSelfTypeKind(DC, options)) {
1371+
case SelfTypeKind::StaticSelf:
1372+
return selfType;
1373+
case SelfTypeKind::DynamicSelf:
1374+
return DynamicSelfType::get(selfType, ctx);
1375+
case SelfTypeKind::InvalidSelf:
1376+
break;
1377+
}
1378+
}
1379+
}
1380+
13811381
// If we're not allowed to complain or we couldn't fix the
13821382
// source, bail out.
13831383
if (options.contains(TypeResolutionFlags::SilenceErrors))

test/type/self.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,15 @@ class Boxer {
287287

288288
required init() {}
289289
}
290+
291+
// https://bugs.swift.org/browse/SR-12133 - a type named 'Self' should be found first
292+
struct OuterType {
293+
struct `Self` {
294+
let string: String
295+
}
296+
297+
var foo: `Self`? {
298+
let optional: String? = "foo"
299+
return optional.map { `Self`(string: $0) }
300+
}
301+
}

0 commit comments

Comments
 (0)