Skip to content

Commit 9ba1da9

Browse files
committed
[Sema] Scale back CodingKeys hack in TypeChecker::lookupUnqualifiedType
I previously added this hack to match the logic in `TypeChecker::lookupUnqualified`, but it turns out that can introduce request cycles for cases where `CodingKeys` is used in a generic requirement for one of `Codable`'s potential value witnesses. Scale back the hack such that it's only done when we get an initial empty lookup result, ensuring we maintain source compatibility. Both these lookup hacks should go away once we properly handle CodingKeys synthesis. rdar://153096639
1 parent 9bf7538 commit 9ba1da9

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,6 @@ TypeChecker::lookupUnqualifiedType(DeclContext *dc, DeclNameRef name,
325325
NameLookupOptions options) {
326326
auto &ctx = dc->getASTContext();
327327

328-
// HACK: Synthesize CodingKeys if needed.
329-
synthesizeCodingKeysIfNeededForUnqualifiedLookup(ctx, dc, name);
330-
331328
auto ulOptions = convertToUnqualifiedLookupOptions(options) |
332329
UnqualifiedLookupFlags::TypeLookup;
333330
{
@@ -336,8 +333,15 @@ TypeChecker::lookupUnqualifiedType(DeclContext *dc, DeclNameRef name,
336333
name, dc, loc,
337334
ulOptions - UnqualifiedLookupFlags::AllowProtocolMembers);
338335

339-
auto lookup =
340-
evaluateOrDefault(ctx.evaluator, UnqualifiedLookupRequest{desc}, {});
336+
UnqualifiedLookupRequest req(desc);
337+
auto lookup = evaluateOrDefault(ctx.evaluator, req, {});
338+
339+
// HACK: Try synthesize CodingKeys if we got an empty result.
340+
if (lookup.allResults().empty() && name.isSimpleName(ctx.Id_CodingKeys)) {
341+
synthesizeCodingKeysIfNeededForUnqualifiedLookup(ctx, dc, name);
342+
lookup = evaluateOrDefault(ctx.evaluator, req, {});
343+
}
344+
341345
if (!lookup.allResults().empty())
342346
return lookup;
343347
}

test/Sema/rdar153096639.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// Make sure we don't run into a request cycle for the below use of 'CodingKeys'
4+
// in an `init(from:)` signature.
5+
6+
protocol P {
7+
associatedtype X
8+
}
9+
10+
struct S: Codable {
11+
var foo: String?
12+
13+
enum CodingKeys: CodingKey {
14+
case foo
15+
}
16+
17+
init<T: P>(from: T) where T.X == CodingKeys {}
18+
}

0 commit comments

Comments
 (0)