Skip to content

[5.9] Fix missing indexing data with overloaded type #66898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions include/swift/AST/TypeRepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,33 @@ class FixedTypeRepr : public TypeRepr {
friend class TypeRepr;
};

/// A TypeRepr for uses of 'Self' in the type of a declaration.
class SelfTypeRepr : public TypeRepr {
Type Ty;
SourceLoc Loc;

public:
SelfTypeRepr(Type Ty, SourceLoc Loc)
: TypeRepr(TypeReprKind::Self), Ty(Ty), Loc(Loc) {}

/// Retrieve the location.
SourceLoc getLoc() const { return Loc; }

/// Retrieve the fixed type.
Type getType() const { return Ty; }

static bool classof(const TypeRepr *T) {
return T->getKind() == TypeReprKind::Self;
}
static bool classof(const SelfTypeRepr *T) { return true; }

private:
SourceLoc getStartLocImpl() const { return Loc; }
SourceLoc getEndLocImpl() const { return Loc; }
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr;
};

class SILBoxTypeReprField {
SourceLoc VarOrLetLoc;
llvm::PointerIntPair<TypeRepr*, 1, bool> FieldTypeAndMutable;
Expand Down Expand Up @@ -1434,6 +1461,7 @@ inline bool TypeRepr::isSimple() const {
case TypeReprKind::Pack:
case TypeReprKind::Tuple:
case TypeReprKind::Fixed:
case TypeReprKind::Self:
case TypeReprKind::Array:
case TypeReprKind::SILBox:
case TypeReprKind::Isolated:
Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/TypeReprNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ ABSTRACT_TYPEREPR(Specifier, TypeRepr)
SPECIFIER_TYPEREPR(CompileTimeConst, SpecifierTypeRepr)
TYPEREPR(Fixed, TypeRepr)
TYPEREPR(SILBox, TypeRepr)
LAST_TYPEREPR(SILBox)
TYPEREPR(Self, TypeRepr)
LAST_TYPEREPR(Self)

#undef SPECIFIER_TYPEREPR
#undef ABSTRACT_TYPEREPR
Expand Down
16 changes: 16 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3341,6 +3341,22 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

void visitSelfTypeRepr(SelfTypeRepr *T) {
printCommon("type_self");
auto Ty = T->getType();
if (Ty) {
auto &srcMgr = Ty->getASTContext().SourceMgr;
if (T->getLoc().isValid()) {
OS << " location=@";
T->getLoc().print(OS, srcMgr);
} else {
OS << " location=<<invalid>>";
}
}
OS << " type="; Ty.dump(OS);
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

void visitSILBoxTypeRepr(SILBoxTypeRepr *T) {
printCommon("sil_box");
Indent += 2;
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,10 @@ bool Traversal::visitFixedTypeRepr(FixedTypeRepr *T) {
return false;
}

bool Traversal::visitSelfTypeRepr(SelfTypeRepr *T) {
return false;
}

bool Traversal::visitSILBoxTypeRepr(SILBoxTypeRepr *T) {
for (auto &field : T->getFields()) {
if (doIt(field.getFieldType()))
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2930,6 +2930,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,

case TypeReprKind::Fixed:
llvm_unreachable("Cannot get fixed TypeReprs in name lookup");
case TypeReprKind::Self:
llvm_unreachable("Cannot get fixed SelfTypeRepr in name lookup");

case TypeReprKind::Optional:
case TypeReprKind::ImplicitlyUnwrappedOptional:
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/TypeRepr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ void FixedTypeRepr::printImpl(ASTPrinter &Printer,
getType().print(Printer, Opts);
}

void SelfTypeRepr::printImpl(ASTPrinter &Printer,
const PrintOptions &Opts) const {
getType().print(Printer, Opts);
}

void SILBoxTypeRepr::printImpl(ASTPrinter &Printer,
const PrintOptions &Opts) const {
// TODO
Expand Down
16 changes: 12 additions & 4 deletions lib/IDE/SourceEntityWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,15 +650,23 @@ ASTWalker::PreWalkAction SemaAnnotator::walkToTypeReprPre(TypeRepr *T) {
return Action::StopIf(!Continue);
}
} else if (auto FT = dyn_cast<FixedTypeRepr>(T)) {
ValueDecl *VD = FT->getType()->getAnyGeneric();
if (auto DT = FT->getType()->getAs<DynamicSelfType>())
if (ValueDecl *VD = FT->getType()->getAnyGeneric()) {
auto Data = ReferenceMetaData(SemaReferenceKind::TypeRef, None);
Data.isImplicitCtorType = true;
auto Continue = passReference(VD, FT->getType(), FT->getLoc(),
FT->getSourceRange(), Data);
return Action::StopIf(!Continue);
}
} else if (auto ST = dyn_cast<SelfTypeRepr>(T)) {
ValueDecl *VD = ST->getType()->getAnyGeneric();
if (auto DT = ST->getType()->getAs<DynamicSelfType>())
VD = DT->getSelfType()->getAnyGeneric();

if (VD) {
auto Data = ReferenceMetaData(SemaReferenceKind::TypeRef, None);
Data.isImplicitCtorType = true;
auto Continue = passReference(VD, FT->getType(), FT->getLoc(),
FT->getSourceRange(), Data);
auto Continue = passReference(VD, ST->getType(), ST->getLoc(),
ST->getSourceRange(), Data);
return Action::StopIf(!Continue);
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
FoundResult visitFixedTypeRepr(FixedTypeRepr *T) {
return handleParent(T, ArrayRef<TypeRepr*>());
}

FoundResult visitSelfTypeRepr(SelfTypeRepr *T) {
return handleParent(T, ArrayRef<TypeRepr*>());
}
};

struct ConversionFunctionInfo {
Expand Down
18 changes: 13 additions & 5 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,19 @@ namespace {
locator, implicit, semantics);
}

if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
auto typeExpr = TypeExpr::createImplicitHack(
loc.getBaseNameLoc(), adjustedFullType->getMetatypeInstanceType(), ctx);
cs.cacheType(typeExpr);
return typeExpr;
if (auto *typeDecl = dyn_cast<TypeDecl>(decl)) {
if (!isa<ModuleDecl>(decl)) {
TypeExpr *typeExpr = nullptr;
if (implicit) {
typeExpr = TypeExpr::createImplicitHack(
loc.getBaseNameLoc(), adjustedFullType->getMetatypeInstanceType(), ctx);
} else {
typeExpr = TypeExpr::createForDecl(loc, typeDecl, dc);
typeExpr->setType(adjustedFullType);
}
cs.cacheType(typeExpr);
return typeExpr;
}
}

auto ref = resolveConcreteDeclRef(decl, locator);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/PreCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
if (typeContext->getSelfClassDecl())
SelfType = DynamicSelfType::get(SelfType, Context);
return new (Context)
TypeExpr(new (Context) FixedTypeRepr(SelfType, Loc));
TypeExpr(new (Context) SelfTypeRepr(SelfType, Loc));
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,9 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,

case TypeReprKind::Fixed:
return cast<FixedTypeRepr>(repr)->getType();

case TypeReprKind::Self:
return cast<SelfTypeRepr>(repr)->getType();
}
llvm_unreachable("all cases should be handled");
}
Expand Down Expand Up @@ -5192,6 +5195,7 @@ class ExistentialTypeVisitor
case TypeReprKind::ImplicitlyUnwrappedOptional:
case TypeReprKind::Tuple:
case TypeReprKind::Fixed:
case TypeReprKind::Self:
case TypeReprKind::Array:
case TypeReprKind::SILBox:
case TypeReprKind::Isolated:
Expand Down
62 changes: 62 additions & 0 deletions test/Index/index_ambiguous_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// Validate that we get index references for almost ambiguous types

// RUN: %target-swift-frontend -emit-module -o %t %t/file1.swift
// RUN: %target-swift-frontend -emit-module -o %t %t/file2.swift

// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %t/file3.swift -I %t > %t/output.txt
// RUN: %FileCheck %s < %t/output.txt

//--- file1.swift
public struct Thing {
public init(string: String) {}

public struct Nested {
public init(string: String) {}
}

public struct Nested2<T> {
public init(value: T) {}
}
}
//--- file2.swift
public struct Thing {}

//--- file3.swift
import file1
import file2

func foo() {
// CHECK: 7:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
// CHECK: 7:7 | constructor/Swift | init(string:) | s:5file15ThingV6stringACSS_tcfc | Ref,Call,RelCall,RelCont | rel: 1
_ = Thing(string: "lol")
// CHECK: 10:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
// CHECK: 10:13 | constructor/Swift | init(string:) | s:5file15ThingV6stringACSS_tcfc | Ref,Call,RelCall,RelCont | rel: 1
_ = Thing.init(string: "lol")
// CHECK: 14:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
// CHECK: 14:13 | struct/Swift | Nested | s:5file15ThingV6NestedV | Ref,RelCont | rel: 1
// TODO: 14:13 | constructor/Swift | init(string:)
_ = Thing.Nested(string: "lol")
// CHECK: 18:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
// CHECK: 18:13 | struct/Swift | Nested | s:5file15ThingV6NestedV | Ref,RelCont | rel: 1
// CHECK: 18:20 | constructor/Swift | init(string:) | s:5file15ThingV6NestedV6stringAESS_tcfc | Ref,Call,RelCall,RelCont | rel: 1
_ = Thing.Nested.init(string: "lol")
// CHECK: 23:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
// CHECK: 23:13 | struct/Swift | Nested2 | s:5file15ThingV7Nested2V | Ref,RelCont | rel: 1
// TODO: 23:13 | constructor/Swift | init(value:)
// TODO: 23:21 | struct/Swift | Int | s:Si | Ref,RelCont | rel: 1
_ = Thing.Nested2<Int>(value: 0)
// CHECK: 28:7 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
// CHECK: 28:13 | struct/Swift | Nested2 | s:5file15ThingV7Nested2V | Ref,RelCont | rel: 1
// TODO: 28:21 | struct/Swift | Int | s:Si | Ref,RelCont | rel: 1
// CHECK: 28:26 | constructor/Swift | init(value:) | s:5file15ThingV7Nested2V5valueAEy_xGx_tcfc | Ref,Call,RelCall,RelCont | rel: 1
_ = Thing.Nested2<Int>.init(value: 0)
// CHECK: 34:7 | module/Swift | file1 | c:@M@file1 | Ref,RelCont | rel: 1
// CHECK: 34:13 | struct/Swift | Thing | s:5file15ThingV | Ref,RelCont | rel: 1
// CHECK: 34:19 | struct/Swift | Nested2 | s:5file15ThingV7Nested2V | Ref,RelCont | rel: 1
// CHECK: 34:19 | constructor/Swift | init(value:) | s:5file15ThingV7Nested2V5valueAEy_xGx_tcfc | Ref,Call,RelCall,RelCont | rel: 1
// CHECK: 34:27 | struct/Swift | Int | s:Si | Ref,RelCont | rel: 1
_ = file1.Thing.Nested2<Int>(value: 0)
}