Skip to content

Commit f91ad05

Browse files
authored
[ImportResolution] Fix import kind when value decl is a typealias of an existential type (#72738)
1 parent 24af84d commit f91ad05

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

lib/AST/Decl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,12 @@ ImportKind ImportDecl::getBestImportKind(const ValueDecl *VD) {
14761476

14771477
case DeclKind::TypeAlias: {
14781478
Type type = cast<TypeAliasDecl>(VD)->getDeclaredInterfaceType();
1479-
auto *nominal = type->getAnyNominal();
1479+
// FIXME: It's necessary to check for existentials because `getAnyNominal`
1480+
// looks through them.
1481+
auto canonical = type->getCanonicalType();
1482+
if (isa<ExistentialType>(canonical))
1483+
return ImportKind::Type;
1484+
auto *nominal = canonical->getAnyNominal();
14801485
if (!nominal)
14811486
return ImportKind::Type;
14821487
return getBestImportKind(nominal);

lib/AST/Type.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ NominalTypeDecl *CanType::getAnyNominal() const {
8686
}
8787

8888
GenericTypeDecl *CanType::getAnyGeneric() const {
89+
// FIXME: Remove checking for existential types. `getAnyGeneric` should return
90+
// the GenericTypeDecl the type is directly bound to.
8991
if (auto existential = dyn_cast<ExistentialType>(*this))
9092
return existential->getConstraintType()->getAnyGeneric();
9193
if (auto ppt = dyn_cast<ParameterizedProtocolType>(*this))

test/ImportResolution/Inputs/DeclsUsedWrongly.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,20 @@ public enum Choice {
77
public typealias Callback = () -> Void
88

99
public typealias Pair<T> = (T, T)
10+
11+
public struct NamespaceStruct {
12+
13+
public protocol NestedProtocol {}
14+
15+
public typealias AnyNestedProtocol = any NestedProtocol
16+
}
17+
18+
public typealias AnyNestedProtocol = NamespaceStruct.AnyNestedProtocol
19+
20+
public typealias NestedProtocol = NamespaceStruct.NestedProtocol
21+
22+
public protocol TopLevelProtocol {
23+
24+
}
25+
26+
public typealias AnyTopLevelProtocol = any TopLevelProtocol

test/ImportResolution/import-specific-fixits.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ import typealias ambiguous.SomeStruct // expected-error{{ambiguous name 'SomeStr
6969
import class ambiguous.SomeStruct // expected-error{{ambiguous name 'SomeStruct' in module 'ambiguous'}}
7070

7171
import func ambiguous.overloadedFunc // no-warning
72+
73+
import protocol DeclsUsedWrongly.TopLevelProtocol // no-warning
74+
import protocol DeclsUsedWrongly.AnyTopLevelProtocol // expected-error {{type alias 'AnyTopLevelProtocol' (aka 'any TopLevelProtocol') cannot be imported as 'protocol'}} {{8-16=typealias}}
75+
import typealias DeclsUsedWrongly.AnyTopLevelProtocol // no-warning
76+
import protocol DeclsUsedWrongly.NestedProtocol // no-warning
77+
import typealias DeclsUsedWrongly.AnyNestedProtocol // no-warning
78+
import protocol DeclsUsedWrongly.AnyNestedProtocol // expected-error {{type alias 'AnyNestedProtocol' (aka 'any NamespaceStruct.NestedProtocol') cannot be imported as 'protocol'}} {{8-16=typealias}}

0 commit comments

Comments
 (0)