Skip to content

Commit e9ff334

Browse files
authored
Fix missing indexing data with overloaded type (#65729)
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
1 parent 6607f05 commit e9ff334

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
@@ -1176,6 +1176,33 @@ class FixedTypeRepr : public TypeRepr {
11761176
friend class TypeRepr;
11771177
};
11781178

1179+
/// A TypeRepr for uses of 'Self' in the type of a declaration.
1180+
class SelfTypeRepr : public TypeRepr {
1181+
Type Ty;
1182+
SourceLoc Loc;
1183+
1184+
public:
1185+
SelfTypeRepr(Type Ty, SourceLoc Loc)
1186+
: TypeRepr(TypeReprKind::Self), Ty(Ty), Loc(Loc) {}
1187+
1188+
/// Retrieve the location.
1189+
SourceLoc getLoc() const { return Loc; }
1190+
1191+
/// Retrieve the fixed type.
1192+
Type getType() const { return Ty; }
1193+
1194+
static bool classof(const TypeRepr *T) {
1195+
return T->getKind() == TypeReprKind::Self;
1196+
}
1197+
static bool classof(const SelfTypeRepr *T) { return true; }
1198+
1199+
private:
1200+
SourceLoc getStartLocImpl() const { return Loc; }
1201+
SourceLoc getEndLocImpl() const { return Loc; }
1202+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
1203+
friend class TypeRepr;
1204+
};
1205+
11791206
class SILBoxTypeReprField {
11801207
SourceLoc VarOrLetLoc;
11811208
llvm::PointerIntPair<TypeRepr*, 1, bool> FieldTypeAndMutable;
@@ -1436,6 +1463,7 @@ inline bool TypeRepr::isSimple() const {
14361463
case TypeReprKind::Pack:
14371464
case TypeReprKind::Tuple:
14381465
case TypeReprKind::Fixed:
1466+
case TypeReprKind::Self:
14391467
case TypeReprKind::Array:
14401468
case TypeReprKind::SILBox:
14411469
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
@@ -3352,6 +3352,22 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
33523352
PrintWithColorRAII(OS, ParenthesisColor) << ')';
33533353
}
33543354

3355+
void visitSelfTypeRepr(SelfTypeRepr *T) {
3356+
printCommon("type_self");
3357+
auto Ty = T->getType();
3358+
if (Ty) {
3359+
auto &srcMgr = Ty->getASTContext().SourceMgr;
3360+
if (T->getLoc().isValid()) {
3361+
OS << " location=@";
3362+
T->getLoc().print(OS, srcMgr);
3363+
} else {
3364+
OS << " location=<<invalid>>";
3365+
}
3366+
}
3367+
OS << " type="; Ty.dump(OS);
3368+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
3369+
}
3370+
33553371
void visitSILBoxTypeRepr(SILBoxTypeRepr *T) {
33563372
printCommon("sil_box");
33573373
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
@@ -2931,6 +2931,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
29312931

29322932
case TypeReprKind::Fixed:
29332933
llvm_unreachable("Cannot get fixed TypeReprs in name lookup");
2934+
case TypeReprKind::Self:
2935+
llvm_unreachable("Cannot get fixed SelfTypeRepr in name lookup");
29342936

29352937
case TypeReprKind::Optional:
29362938
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

0 commit comments

Comments
 (0)