Skip to content

Commit ba7628c

Browse files
authored
Merge pull request #66898 from keith/ks/5.9-fix-missing-indexing-data-with-overloaded-type
[5.9] Fix missing indexing data with overloaded type
2 parents 0fe1f78 + 246d27e commit ba7628c

File tree

12 files changed

+153
-11
lines changed

12 files changed

+153
-11
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,33 @@ class FixedTypeRepr : public TypeRepr {
11741174
friend class TypeRepr;
11751175
};
11761176

1177+
/// A TypeRepr for uses of 'Self' in the type of a declaration.
1178+
class SelfTypeRepr : public TypeRepr {
1179+
Type Ty;
1180+
SourceLoc Loc;
1181+
1182+
public:
1183+
SelfTypeRepr(Type Ty, SourceLoc Loc)
1184+
: TypeRepr(TypeReprKind::Self), Ty(Ty), Loc(Loc) {}
1185+
1186+
/// Retrieve the location.
1187+
SourceLoc getLoc() const { return Loc; }
1188+
1189+
/// Retrieve the fixed type.
1190+
Type getType() const { return Ty; }
1191+
1192+
static bool classof(const TypeRepr *T) {
1193+
return T->getKind() == TypeReprKind::Self;
1194+
}
1195+
static bool classof(const SelfTypeRepr *T) { return true; }
1196+
1197+
private:
1198+
SourceLoc getStartLocImpl() const { return Loc; }
1199+
SourceLoc getEndLocImpl() const { return Loc; }
1200+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1201+
friend class TypeRepr;
1202+
};
1203+
11771204
class SILBoxTypeReprField {
11781205
SourceLoc VarOrLetLoc;
11791206
llvm::PointerIntPair<TypeRepr*, 1, bool> FieldTypeAndMutable;
@@ -1434,6 +1461,7 @@ inline bool TypeRepr::isSimple() const {
14341461
case TypeReprKind::Pack:
14351462
case TypeReprKind::Tuple:
14361463
case TypeReprKind::Fixed:
1464+
case TypeReprKind::Self:
14371465
case TypeReprKind::Array:
14381466
case TypeReprKind::SILBox:
14391467
case TypeReprKind::Isolated:

include/swift/AST/TypeReprNodes.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ ABSTRACT_TYPEREPR(Specifier, TypeRepr)
7474
SPECIFIER_TYPEREPR(CompileTimeConst, SpecifierTypeRepr)
7575
TYPEREPR(Fixed, TypeRepr)
7676
TYPEREPR(SILBox, TypeRepr)
77-
LAST_TYPEREPR(SILBox)
77+
TYPEREPR(Self, TypeRepr)
78+
LAST_TYPEREPR(Self)
7879

7980
#undef SPECIFIER_TYPEREPR
8081
#undef ABSTRACT_TYPEREPR

lib/AST/ASTDumper.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,22 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
33413341
PrintWithColorRAII(OS, ParenthesisColor) << ')';
33423342
}
33433343

3344+
void visitSelfTypeRepr(SelfTypeRepr *T) {
3345+
printCommon("type_self");
3346+
auto Ty = T->getType();
3347+
if (Ty) {
3348+
auto &srcMgr = Ty->getASTContext().SourceMgr;
3349+
if (T->getLoc().isValid()) {
3350+
OS << " location=@";
3351+
T->getLoc().print(OS, srcMgr);
3352+
} else {
3353+
OS << " location=<<invalid>>";
3354+
}
3355+
}
3356+
OS << " type="; Ty.dump(OS);
3357+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
3358+
}
3359+
33443360
void visitSILBoxTypeRepr(SILBoxTypeRepr *T) {
33453361
printCommon("sil_box");
33463362
Indent += 2;

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,10 @@ bool Traversal::visitFixedTypeRepr(FixedTypeRepr *T) {
21932193
return false;
21942194
}
21952195

2196+
bool Traversal::visitSelfTypeRepr(SelfTypeRepr *T) {
2197+
return false;
2198+
}
2199+
21962200
bool Traversal::visitSILBoxTypeRepr(SILBoxTypeRepr *T) {
21972201
for (auto &field : T->getFields()) {
21982202
if (doIt(field.getFieldType()))

lib/AST/NameLookup.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,6 +3000,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
30003000

30013001
case TypeReprKind::Fixed:
30023002
llvm_unreachable("Cannot get fixed TypeReprs in name lookup");
3003+
case TypeReprKind::Self:
3004+
llvm_unreachable("Cannot get fixed SelfTypeRepr in name lookup");
30033005

30043006
case TypeReprKind::Optional:
30053007
case TypeReprKind::ImplicitlyUnwrappedOptional:

lib/AST/TypeRepr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,11 @@ void FixedTypeRepr::printImpl(ASTPrinter &Printer,
629629
getType().print(Printer, Opts);
630630
}
631631

632+
void SelfTypeRepr::printImpl(ASTPrinter &Printer,
633+
const PrintOptions &Opts) const {
634+
getType().print(Printer, Opts);
635+
}
636+
632637
void SILBoxTypeRepr::printImpl(ASTPrinter &Printer,
633638
const PrintOptions &Opts) const {
634639
// TODO

lib/IDE/SourceEntityWalker.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,15 +651,23 @@ ASTWalker::PreWalkAction SemaAnnotator::walkToTypeReprPre(TypeRepr *T) {
651651
return Action::StopIf(!Continue);
652652
}
653653
} else if (auto FT = dyn_cast<FixedTypeRepr>(T)) {
654-
ValueDecl *VD = FT->getType()->getAnyGeneric();
655-
if (auto DT = FT->getType()->getAs<DynamicSelfType>())
654+
if (ValueDecl *VD = FT->getType()->getAnyGeneric()) {
655+
auto Data = ReferenceMetaData(SemaReferenceKind::TypeRef, None);
656+
Data.isImplicitCtorType = true;
657+
auto Continue = passReference(VD, FT->getType(), FT->getLoc(),
658+
FT->getSourceRange(), Data);
659+
return Action::StopIf(!Continue);
660+
}
661+
} else if (auto ST = dyn_cast<SelfTypeRepr>(T)) {
662+
ValueDecl *VD = ST->getType()->getAnyGeneric();
663+
if (auto DT = ST->getType()->getAs<DynamicSelfType>())
656664
VD = DT->getSelfType()->getAnyGeneric();
657665

658666
if (VD) {
659667
auto Data = ReferenceMetaData(SemaReferenceKind::TypeRef, llvm::None);
660668
Data.isImplicitCtorType = true;
661-
auto Continue = passReference(VD, FT->getType(), FT->getLoc(),
662-
FT->getSourceRange(), Data);
669+
auto Continue = passReference(VD, ST->getType(), ST->getLoc(),
670+
ST->getSourceRange(), Data);
663671
return Action::StopIf(!Continue);
664672
}
665673
}

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
220220
FoundResult visitFixedTypeRepr(FixedTypeRepr *T) {
221221
return handleParent(T, ArrayRef<TypeRepr*>());
222222
}
223+
224+
FoundResult visitSelfTypeRepr(SelfTypeRepr *T) {
225+
return handleParent(T, ArrayRef<TypeRepr*>());
226+
}
223227
};
224228

225229
struct ConversionFunctionInfo {

lib/Sema/CSApply.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,19 @@ namespace {
624624
locator, implicit, semantics);
625625
}
626626

627-
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
628-
auto typeExpr = TypeExpr::createImplicitHack(
629-
loc.getBaseNameLoc(), adjustedFullType->getMetatypeInstanceType(), ctx);
630-
cs.cacheType(typeExpr);
631-
return typeExpr;
627+
if (auto *typeDecl = dyn_cast<TypeDecl>(decl)) {
628+
if (!isa<ModuleDecl>(decl)) {
629+
TypeExpr *typeExpr = nullptr;
630+
if (implicit) {
631+
typeExpr = TypeExpr::createImplicitHack(
632+
loc.getBaseNameLoc(), adjustedFullType->getMetatypeInstanceType(), ctx);
633+
} else {
634+
typeExpr = TypeExpr::createForDecl(loc, typeDecl, dc);
635+
typeExpr->setType(adjustedFullType);
636+
}
637+
cs.cacheType(typeExpr);
638+
return typeExpr;
639+
}
632640
}
633641

634642
auto ref = resolveConcreteDeclRef(decl, locator);

lib/Sema/PreCheckExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
597597
if (typeContext->getSelfClassDecl())
598598
SelfType = DynamicSelfType::get(SelfType, Context);
599599
return new (Context)
600-
TypeExpr(new (Context) FixedTypeRepr(SelfType, Loc));
600+
TypeExpr(new (Context) SelfTypeRepr(SelfType, Loc));
601601
}
602602
}
603603

lib/Sema/TypeCheckType.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,9 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
25732573

25742574
case TypeReprKind::Fixed:
25752575
return cast<FixedTypeRepr>(repr)->getType();
2576+
2577+
case TypeReprKind::Self:
2578+
return cast<SelfTypeRepr>(repr)->getType();
25762579
}
25772580
llvm_unreachable("all cases should be handled");
25782581
}
@@ -5221,6 +5224,7 @@ class ExistentialTypeVisitor
52215224
case TypeReprKind::ImplicitlyUnwrappedOptional:
52225225
case TypeReprKind::Tuple:
52235226
case TypeReprKind::Fixed:
5227+
case TypeReprKind::Self:
52245228
case TypeReprKind::Array:
52255229
case TypeReprKind::SILBox:
52265230
case TypeReprKind::Isolated:

test/Index/index_ambiguous_type.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// Validate that we get index references for almost ambiguous types
5+
6+
// RUN: %target-swift-frontend -emit-module -o %t %t/file1.swift
7+
// RUN: %target-swift-frontend -emit-module -o %t %t/file2.swift
8+
9+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %t/file3.swift -I %t > %t/output.txt
10+
// RUN: %FileCheck %s < %t/output.txt
11+
12+
//--- file1.swift
13+
public struct Thing {
14+
public init(string: String) {}
15+
16+
public struct Nested {
17+
public init(string: String) {}
18+
}
19+
20+
public struct Nested2<T> {
21+
public init(value: T) {}
22+
}
23+
}
24+
//--- file2.swift
25+
public struct Thing {}
26+
27+
//--- file3.swift
28+
import file1
29+
import file2
30+
31+
func foo() {
32+
// CHECK: 7:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
33+
// CHECK: 7:7 | constructor/Swift | init(string:) | s:5file15ThingV6stringACSS_tcfc | Ref,Call,RelCall,RelCont | rel: 1
34+
_ = Thing(string: "lol")
35+
// CHECK: 10:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
36+
// CHECK: 10:13 | constructor/Swift | init(string:) | s:5file15ThingV6stringACSS_tcfc | Ref,Call,RelCall,RelCont | rel: 1
37+
_ = Thing.init(string: "lol")
38+
// CHECK: 14:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
39+
// CHECK: 14:13 | struct/Swift | Nested | s:5file15ThingV6NestedV | Ref,RelCont | rel: 1
40+
// TODO: 14:13 | constructor/Swift | init(string:)
41+
_ = Thing.Nested(string: "lol")
42+
// CHECK: 18:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
43+
// CHECK: 18:13 | struct/Swift | Nested | s:5file15ThingV6NestedV | Ref,RelCont | rel: 1
44+
// CHECK: 18:20 | constructor/Swift | init(string:) | s:5file15ThingV6NestedV6stringAESS_tcfc | Ref,Call,RelCall,RelCont | rel: 1
45+
_ = Thing.Nested.init(string: "lol")
46+
// CHECK: 23:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
47+
// CHECK: 23:13 | struct/Swift | Nested2 | s:5file15ThingV7Nested2V | Ref,RelCont | rel: 1
48+
// TODO: 23:13 | constructor/Swift | init(value:)
49+
// TODO: 23:21 | struct/Swift | Int | s:Si | Ref,RelCont | rel: 1
50+
_ = Thing.Nested2<Int>(value: 0)
51+
// CHECK: 28:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
52+
// CHECK: 28:13 | struct/Swift | Nested2 | s:5file15ThingV7Nested2V | Ref,RelCont | rel: 1
53+
// TODO: 28:21 | struct/Swift | Int | s:Si | Ref,RelCont | rel: 1
54+
// CHECK: 28:26 | constructor/Swift | init(value:) | s:5file15ThingV7Nested2V5valueAEy_xGx_tcfc | Ref,Call,RelCall,RelCont | rel: 1
55+
_ = Thing.Nested2<Int>.init(value: 0)
56+
// CHECK: 34:7 | module/Swift | file1 | c:@M@file1 | Ref,RelCont | rel: 1
57+
// CHECK: 34:13 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
58+
// CHECK: 34:19 | struct/Swift | Nested2 | s:5file15ThingV7Nested2V | Ref,RelCont | rel: 1
59+
// CHECK: 34:19 | constructor/Swift | init(value:) | s:5file15ThingV7Nested2V5valueAEy_xGx_tcfc | Ref,Call,RelCall,RelCont | rel: 1
60+
// CHECK: 34:27 | struct/Swift | Int | s:Si | Ref,RelCont | rel: 1
61+
_ = file1.Thing.Nested2<Int>(value: 0)
62+
}

0 commit comments

Comments
 (0)