Skip to content

Commit 8954e5f

Browse files
authored
Merge pull request #19891 from slavapestov/unqualified-lookup-fixes-part-2
Unqualified lookup fixes, part 2
2 parents 0b94112 + 8f97511 commit 8954e5f

File tree

9 files changed

+141
-50
lines changed

9 files changed

+141
-50
lines changed

lib/AST/NameLookup.cpp

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -999,8 +999,6 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
999999

10001000
// Look in the generic parameters after checking our local declaration.
10011001
GenericParams = AFD->getGenericParams();
1002-
} else if (auto *SD = dyn_cast<SubscriptDecl>(DC)) {
1003-
GenericParams = SD->getGenericParams();
10041002
} else if (auto *ACE = dyn_cast<AbstractClosureExpr>(DC)) {
10051003
// Look for local variables; normally, the parser resolves these
10061004
// for us, but it can't do the right thing inside local types.
@@ -1042,12 +1040,14 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
10421040
continue;
10431041
} else {
10441042
assert(isa<TopLevelCodeDecl>(DC) || isa<Initializer>(DC) ||
1045-
isa<TypeAliasDecl>(DC));
1043+
isa<TypeAliasDecl>(DC) || isa<SubscriptDecl>(DC));
10461044
if (!isCascadingUse.hasValue())
10471045
isCascadingUse = DC->isCascadingContextForLookup(false);
10481046
}
10491047

1050-
// Check the generic parameters for something with the given name.
1048+
// If we're inside a function context, we've already moved to
1049+
// the parent DC, so we have to check the function's generic
1050+
// parameters first.
10511051
if (GenericParams) {
10521052
namelookup::FindLocalVal localVal(SM, Loc, Consumer);
10531053
localVal.checkGenericParams(GenericParams);
@@ -1056,6 +1056,30 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
10561056
return;
10571057
}
10581058

1059+
// Check the generic parameters of our context.
1060+
GenericParamList *dcGenericParams = nullptr;
1061+
if (auto nominal = dyn_cast<NominalTypeDecl>(DC))
1062+
dcGenericParams = nominal->getGenericParams();
1063+
else if (auto ext = dyn_cast<ExtensionDecl>(DC))
1064+
dcGenericParams = ext->getGenericParams();
1065+
else if (auto subscript = dyn_cast<SubscriptDecl>(DC))
1066+
dcGenericParams = subscript->getGenericParams();
1067+
1068+
while (dcGenericParams) {
1069+
namelookup::FindLocalVal localVal(SM, Loc, Consumer);
1070+
localVal.checkGenericParams(dcGenericParams);
1071+
1072+
if (shouldReturnBasedOnResults())
1073+
return;
1074+
1075+
// Extensions of nested types have multiple levels of
1076+
// generic parameters, so we have to visit them explicitly.
1077+
if (!isa<ExtensionDecl>(DC))
1078+
break;
1079+
1080+
dcGenericParams = dcGenericParams->getOuterParameters();
1081+
}
1082+
10591083
if (BaseDC && !lookupDecls.empty()) {
10601084
NLOptions options = baseNLOptions;
10611085
if (isCascadingUse.getValue())
@@ -1071,12 +1095,9 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
10711095
// Classify this declaration.
10721096
FoundAny = true;
10731097

1074-
// Types are local or metatype members.
1098+
// Types are formally members of the metatype.
10751099
if (auto TD = dyn_cast<TypeDecl>(Result)) {
1076-
if (isa<GenericTypeParamDecl>(TD))
1077-
Results.push_back(LookupResultEntry(Result));
1078-
else
1079-
Results.push_back(LookupResultEntry(MetaBaseDC, Result));
1100+
Results.push_back(LookupResultEntry(MetaBaseDC, Result));
10801101
continue;
10811102
}
10821103

@@ -1108,29 +1129,6 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
11081129
}
11091130
}
11101131

1111-
// Check the generic parameters if our context is a generic type or
1112-
// extension thereof.
1113-
GenericParamList *dcGenericParams = nullptr;
1114-
if (auto nominal = dyn_cast<NominalTypeDecl>(DC))
1115-
dcGenericParams = nominal->getGenericParams();
1116-
else if (auto ext = dyn_cast<ExtensionDecl>(DC))
1117-
dcGenericParams = ext->getGenericParams();
1118-
else if (auto subscript = dyn_cast<SubscriptDecl>(DC))
1119-
dcGenericParams = subscript->getGenericParams();
1120-
1121-
while (dcGenericParams) {
1122-
namelookup::FindLocalVal localVal(SM, Loc, Consumer);
1123-
localVal.checkGenericParams(dcGenericParams);
1124-
1125-
if (shouldReturnBasedOnResults())
1126-
return;
1127-
1128-
if (!isa<ExtensionDecl>(DC))
1129-
break;
1130-
1131-
dcGenericParams = dcGenericParams->getOuterParameters();
1132-
}
1133-
11341132
DC = DC->getParentForLookup();
11351133
}
11361134

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ static NominalTypeDecl *resolveSingleNominalTypeDecl(
20572057

20582058
TypeResolutionOptions options = TypeResolverContext::TypeAliasDecl;
20592059
options |= flags;
2060-
if (tc.validateType(typeLoc, TypeResolution::forContextual(DC), options))
2060+
if (tc.validateType(typeLoc, TypeResolution::forInterface(DC), options))
20612061
return nullptr;
20622062

20632063
return typeLoc.getType()->getAnyNominal();
@@ -3469,7 +3469,7 @@ static void validateTypealiasType(TypeChecker &tc, TypeAliasDecl *typeAlias) {
34693469
}
34703470

34713471
if (tc.validateType(typeAlias->getUnderlyingTypeLoc(),
3472-
TypeResolution::forContextual(typeAlias), options)) {
3472+
TypeResolution::forInterface(typeAlias), options)) {
34733473
typeAlias->setInvalid();
34743474
typeAlias->getUnderlyingTypeLoc().setInvalidType(tc.Context);
34753475
}

test/NameBinding/name_lookup.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,11 @@ func test3() {
601601
_ = b
602602
}
603603
}
604+
605+
// rdar://problem/22587551
606+
class ShadowingGenericParameter<T> {
607+
typealias T = Int
608+
func foo (t : T) {}
609+
}
610+
611+
_ = ShadowingGenericParameter<String>().foo(t: "hi")

test/Sema/circular_decl_checking.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class X {
6666

6767
// <rdar://problem/17144076> recursive typealias causes a segfault in the type checker
6868
struct SomeStruct<A> {
69-
typealias A = A // expected-error {{type alias 'A' references itself}}
69+
typealias A = A // this is OK now -- the underlying type is the generic parameter 'A'
70+
typealias B = B // expected-error {{type alias 'B' references itself}}
7071
// expected-note@-1 {{type declared here}}
7172
}
7273

test/api-digester/Outputs/stability-stdlib-source.swift.expected

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,53 @@
1010
/* Renamed Decls */
1111

1212
/* Type Changes */
13+
Func AnyBidirectionalCollection.formIndex(_:offsetBy:) has parameter 0 changing from Default to InOut
14+
Func AnyBidirectionalCollection.formIndex(_:offsetBy:limitedBy:) has parameter 0 changing from Default to InOut
15+
Func AnyBidirectionalCollection.formIndex(after:) has parameter 0 changing from Default to InOut
16+
Func AnyBidirectionalCollection.formIndex(before:) has parameter 0 changing from Default to InOut
17+
Func AnyCollection.formIndex(_:offsetBy:) has parameter 0 changing from Default to InOut
18+
Func AnyCollection.formIndex(_:offsetBy:limitedBy:) has parameter 0 changing from Default to InOut
19+
Func AnyCollection.formIndex(after:) has parameter 0 changing from Default to InOut
20+
Func AnyRandomAccessCollection.formIndex(_:offsetBy:) has parameter 0 changing from Default to InOut
21+
Func AnyRandomAccessCollection.formIndex(_:offsetBy:limitedBy:) has parameter 0 changing from Default to InOut
22+
Func AnyRandomAccessCollection.formIndex(after:) has parameter 0 changing from Default to InOut
23+
Func AnyRandomAccessCollection.formIndex(before:) has parameter 0 changing from Default to InOut
24+
Func Array.append(_:) has parameter 0 changing from Default to Owned
25+
Func Array.append(_:) has parameter 0 type change from Array<Element>.Element to Element
26+
Func Array.insert(_:at:) has parameter 0 changing from Default to Owned
27+
Func Array.insert(_:at:) has parameter 0 type change from Array<Element>.Element to Element
28+
Func ArraySlice.append(_:) has parameter 0 changing from Default to Owned
29+
Func ArraySlice.append(_:) has parameter 0 type change from ArraySlice<Element>.Element to Element
30+
Func ArraySlice.insert(_:at:) has parameter 0 changing from Default to Owned
31+
Func ArraySlice.insert(_:at:) has parameter 0 type change from ArraySlice<Element>.Element to Element
32+
Func ContiguousArray.append(_:) has parameter 0 changing from Default to Owned
33+
Func ContiguousArray.append(_:) has parameter 0 type change from ContiguousArray<Element>.Element to Element
34+
Func ContiguousArray.insert(_:at:) has parameter 0 changing from Default to Owned
35+
Func ContiguousArray.insert(_:at:) has parameter 0 type change from ContiguousArray<Element>.Element to Element
36+
Func DefaultIndices.formIndex(after:) has parameter 0 changing from Default to InOut
37+
Func DefaultIndices.formIndex(before:) has parameter 0 changing from Default to InOut
38+
Func Dictionary.updateValue(_:forKey:) has parameter 0 changing from Default to Owned
39+
Func Dictionary.updateValue(_:forKey:) has parameter 0 type change from Dictionary<Key, Value>.Value to Value
40+
Func LazyFilterCollection.formIndex(_:offsetBy:) has parameter 0 changing from Default to InOut
41+
Func LazyFilterCollection.formIndex(_:offsetBy:limitedBy:) has parameter 0 changing from Default to InOut
42+
Func LazyFilterCollection.formIndex(after:) has parameter 0 changing from Default to InOut
43+
Func LazyFilterCollection.formIndex(before:) has parameter 0 changing from Default to InOut
44+
Func LazyMapCollection.formIndex(after:) has parameter 0 changing from Default to InOut
45+
Func LazyMapCollection.formIndex(before:) has parameter 0 changing from Default to InOut
46+
Func Set.insert(_:) has parameter 0 changing from Default to Owned
47+
Func Set.insert(_:) has parameter 0 type change from Set<Element>.Element to Element
48+
Func Set.update(with:) has parameter 0 changing from Default to Owned
49+
Func Set.update(with:) has parameter 0 type change from Set<Element>.Element to Element
50+
Func Slice.formIndex(after:) has parameter 0 changing from Default to InOut
51+
Func Slice.formIndex(before:) has parameter 0 changing from Default to InOut
52+
Func String.UTF16View.Indices.formIndex(after:) has parameter 0 changing from Default to InOut
53+
Func String.UTF16View.Indices.formIndex(before:) has parameter 0 changing from Default to InOut
54+
Func Substring.UTF16View.formIndex(after:) has parameter 0 changing from Default to InOut
55+
Func Substring.UTF16View.formIndex(before:) has parameter 0 changing from Default to InOut
56+
Func Substring.UTF8View.formIndex(after:) has parameter 0 changing from Default to InOut
57+
Func Substring.UTF8View.formIndex(before:) has parameter 0 changing from Default to InOut
58+
Func Substring.UnicodeScalarView.formIndex(after:) has parameter 0 changing from Default to InOut
59+
Func Substring.UnicodeScalarView.formIndex(before:) has parameter 0 changing from Default to InOut
1360

1461
/* Decl Attribute changes */
1562

test/decl/protocol/req/associated_type_inference.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,22 @@ protocol P20 {
508508
struct S19 : P20 { // expected-error{{type 'S19' does not conform to protocol 'P20'}}
509509
typealias TT = Int?
510510
}
511+
512+
// rdar://problem/44777661
513+
struct S30<T> where T : P30 {}
514+
515+
protocol P30 {
516+
static func bar()
517+
}
518+
519+
protocol P31 {
520+
associatedtype T : P30
521+
}
522+
523+
extension S30 : P31 where T : P31 {}
524+
525+
extension S30 {
526+
func foo() {
527+
T.bar()
528+
}
529+
}

test/decl/typealias/protocol.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,18 @@ protocol P3 {
144144

145145
// Test for not crashing on recursive aliases
146146
protocol Circular {
147-
typealias Y = Self.Y // expected-error {{type alias 'Y' is not a member type of 'Self'}}
147+
typealias Y = Self.Y // expected-error {{'Y' is not a member type of 'Self'}}
148+
// expected-note@-1 {{did you mean 'Y'?}}
148149

149150
typealias Y2 = Y2 // expected-error {{type alias 'Y2' references itself}}
150151
// expected-note@-1 {{type declared here}}
152+
// expected-note@-2 {{did you mean 'Y2'?}}
151153

152154
typealias Y3 = Y4 // expected-note {{type declared here}}
155+
// expected-note@-1 {{did you mean 'Y3'?}}
156+
153157
typealias Y4 = Y3 // expected-error {{type alias 'Y3' references itself}}
158+
// expected-note@-1 {{did you mean 'Y4'?}}
154159
}
155160

156161
// Qualified and unqualified references to protocol typealiases from concrete type

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -654,13 +654,29 @@ static bool hasSameContents(ArrayRef<StringRef> Left,
654654
return LeftMinusRight.empty() && RightMinusLeft.empty();
655655
}
656656

657-
bool static isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R) {
657+
static bool hasSameParameterFlags(const SDKNodeType *Left, const SDKNodeType *Right) {
658+
if (Left->hasDefaultArgument() != Right->hasDefaultArgument())
659+
return false;
660+
if (Left->getParamValueOwnership() != Right->getParamValueOwnership())
661+
return false;
662+
663+
return true;
664+
}
665+
666+
static bool isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R) {
658667
auto *LeftAlias = dyn_cast<SDKNodeTypeAlias>(&L);
659668
auto *RightAlias = dyn_cast<SDKNodeTypeAlias>(&R);
660669
if (LeftAlias || RightAlias) {
670+
auto Left = L.getAs<SDKNodeType>();
671+
auto Right = R.getAs<SDKNodeType>();
672+
673+
// First compare the parameter attributes.
674+
if (!hasSameParameterFlags(Left, Right))
675+
return false;
676+
661677
// Comparing the underlying types if any of the inputs are alias.
662-
const SDKNode *Left = LeftAlias ? LeftAlias->getUnderlyingType() : &L;
663-
const SDKNode *Right = RightAlias ? RightAlias->getUnderlyingType() : &R;
678+
Left = LeftAlias ? LeftAlias->getUnderlyingType() : Left;
679+
Right = RightAlias ? RightAlias->getUnderlyingType() : Right;
664680
return *Left == *Right;
665681
}
666682

@@ -676,9 +692,7 @@ bool static isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R)
676692
auto Right = R.getAs<SDKNodeType>();
677693
if (Left->hasAttributeChange(*Right))
678694
return false;
679-
if (Left->hasDefaultArgument() != Right->hasDefaultArgument())
680-
return false;
681-
if (Left->getParamValueOwnership() != Right->getParamValueOwnership())
695+
if (!hasSameParameterFlags(Left, Right))
682696
return false;
683697
if (Left->getPrintedName() == Right->getPrintedName())
684698
return true;
@@ -740,8 +754,6 @@ bool static isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R)
740754
if (auto *Right = dyn_cast<SDKNodeDeclSubscript>(&R)) {
741755
if (Left->hasSetter() != Right->hasSetter())
742756
return false;
743-
if (Left->hasStorage() != Right->hasStorage())
744-
return false;
745757
}
746758
}
747759
LLVM_FALLTHROUGH;
@@ -776,7 +788,7 @@ bool static isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R)
776788
}
777789
}
778790

779-
llvm_unreachable("Unhanlded SDKNodeKind in switch.");
791+
llvm_unreachable("Unhandled SDKNodeKind in switch.");
780792
}
781793

782794
bool SDKContext::isEqual(const SDKNode &Left, const SDKNode &Right) {
@@ -1166,16 +1178,15 @@ SwiftDeclCollector::constructTypeNode(Type T, TypeInitInfo Info) {
11661178
if (Ctx.checkingABI()) {
11671179
T = T->getCanonicalType();
11681180
}
1169-
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeNominal);
11701181

11711182
if (auto NAT = dyn_cast<NameAliasType>(T.getPointer())) {
1172-
SDKNode* Root = SDKNodeInitInfo(Ctx, T).createSDKNode(SDKNodeKind::TypeAlias);
1173-
Root->addChild(constructTypeNode(NAT->getCanonicalType()));
1183+
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeAlias);
1184+
Root->addChild(constructTypeNode(NAT->getSinglyDesugaredType()));
11741185
return Root;
11751186
}
11761187

11771188
if (auto Fun = T->getAs<AnyFunctionType>()) {
1178-
SDKNode* Root = SDKNodeInitInfo(Ctx, T).createSDKNode(SDKNodeKind::TypeFunc);
1189+
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeFunc);
11791190

11801191
// Still, return type first
11811192
Root->addChild(constructTypeNode(Fun->getResult()));
@@ -1187,6 +1198,8 @@ SwiftDeclCollector::constructTypeNode(Type T, TypeInitInfo Info) {
11871198
return Root;
11881199
}
11891200

1201+
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeNominal);
1202+
11901203
// Keep paren type as a stand-alone level.
11911204
if (auto *PT = dyn_cast<ParenType>(T.getPointer())) {
11921205
Root->addChild(constructTypeNode(PT->getSinglyDesugaredType()));

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ class InterfaceTypeChangeDetector {
12931293
if (IsVisitingLeft &&
12941294
Node->getPrintedName() != Counter->getPrintedName() &&
12951295
(Node->getName() != Counter->getName() ||
1296-
Node->getChildrenCount() != Counter->getChildrenCount())) {
1296+
Node->getChildrenCount() != Counter->getChildrenCount())) {
12971297
Node->annotate(NodeAnnotation::TypeRewritten);
12981298
Node->annotate(NodeAnnotation::TypeRewrittenLeft, Node->getPrintedName());
12991299
Node->annotate(NodeAnnotation::TypeRewrittenRight,

0 commit comments

Comments
 (0)