Skip to content

Commit 246d27e

Browse files
committed
[5.9] Fix missing indexing data with overloaded type
When you have a type that's ambiguous because it's defined in 2 imported modules, but you don't have to disambiguate by using the module name, previously no index references were produced. Now most are for the common case, but notably nested type constructors and generics still aren't emitted, partially because of #65726 Fixes: #64598 (cherry picked from commit e9ff334 / #65729)
1 parent b89e4d6 commit 246d27e

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
@@ -2182,6 +2182,10 @@ bool Traversal::visitFixedTypeRepr(FixedTypeRepr *T) {
21822182
return false;
21832183
}
21842184

2185+
bool Traversal::visitSelfTypeRepr(SelfTypeRepr *T) {
2186+
return false;
2187+
}
2188+
21852189
bool Traversal::visitSILBoxTypeRepr(SILBoxTypeRepr *T) {
21862190
for (auto &field : T->getFields()) {
21872191
if (doIt(field.getFieldType()))

lib/AST/NameLookup.cpp

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

29312931
case TypeReprKind::Fixed:
29322932
llvm_unreachable("Cannot get fixed TypeReprs in name lookup");
2933+
case TypeReprKind::Self:
2934+
llvm_unreachable("Cannot get fixed SelfTypeRepr in name lookup");
29332935

29342936
case TypeReprKind::Optional:
29352937
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
@@ -650,15 +650,23 @@ ASTWalker::PreWalkAction SemaAnnotator::walkToTypeReprPre(TypeRepr *T) {
650650
return Action::StopIf(!Continue);
651651
}
652652
} else if (auto FT = dyn_cast<FixedTypeRepr>(T)) {
653-
ValueDecl *VD = FT->getType()->getAnyGeneric();
654-
if (auto DT = FT->getType()->getAs<DynamicSelfType>())
653+
if (ValueDecl *VD = FT->getType()->getAnyGeneric()) {
654+
auto Data = ReferenceMetaData(SemaReferenceKind::TypeRef, None);
655+
Data.isImplicitCtorType = true;
656+
auto Continue = passReference(VD, FT->getType(), FT->getLoc(),
657+
FT->getSourceRange(), Data);
658+
return Action::StopIf(!Continue);
659+
}
660+
} else if (auto ST = dyn_cast<SelfTypeRepr>(T)) {
661+
ValueDecl *VD = ST->getType()->getAnyGeneric();
662+
if (auto DT = ST->getType()->getAs<DynamicSelfType>())
655663
VD = DT->getSelfType()->getAnyGeneric();
656664

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

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
@@ -2563,6 +2563,9 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
25632563

25642564
case TypeReprKind::Fixed:
25652565
return cast<FixedTypeRepr>(repr)->getType();
2566+
2567+
case TypeReprKind::Self:
2568+
return cast<SelfTypeRepr>(repr)->getType();
25662569
}
25672570
llvm_unreachable("all cases should be handled");
25682571
}
@@ -5192,6 +5195,7 @@ class ExistentialTypeVisitor
51925195
case TypeReprKind::ImplicitlyUnwrappedOptional:
51935196
case TypeReprKind::Tuple:
51945197
case TypeReprKind::Fixed:
5198+
case TypeReprKind::Self:
51955199
case TypeReprKind::Array:
51965200
case TypeReprKind::SILBox:
51975201
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)