Skip to content

Commit bdfa8d9

Browse files
committed
[AST] Allow hole types to originate from declarations
Associating holes directly with declarations is useful in cases when declarations are invalid e.g. directly `ErrorType` or have error types inside.
1 parent 1345a2b commit bdfa8d9

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

include/swift/AST/Types.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5736,19 +5736,19 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(TypeVariableType, Type)
57365736
/// because the expression is ambiguous. This type is only used by the
57375737
/// constraint solver and transformed into UnresolvedType to be used in AST.
57385738
class HoleType : public TypeBase {
5739-
using OriginatorType =
5740-
llvm::PointerUnion<TypeVariableType *, DependentMemberType *>;
5739+
using Originator = llvm::PointerUnion<TypeVariableType *,
5740+
DependentMemberType *, VarDecl *>;
57415741

5742-
OriginatorType Originator;
5742+
Originator O;
57435743

5744-
HoleType(ASTContext &C, OriginatorType originator,
5744+
HoleType(ASTContext &C, Originator originator,
57455745
RecursiveTypeProperties properties)
5746-
: TypeBase(TypeKind::Hole, &C, properties), Originator(originator) {}
5746+
: TypeBase(TypeKind::Hole, &C, properties), O(originator) {}
57475747

57485748
public:
5749-
static Type get(ASTContext &ctx, OriginatorType originatorType);
5749+
static Type get(ASTContext &ctx, Originator originator);
57505750

5751-
OriginatorType getOriginatorType() const { return Originator; }
5751+
Originator getOriginator() const { return O; }
57525752

57535753
static bool classof(const TypeBase *T) {
57545754
return T->getKind() == TypeKind::Hole;

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,13 +2400,9 @@ Type ErrorType::get(Type originalType) {
24002400
return entry = new (mem) ErrorType(ctx, originalType, properties);
24012401
}
24022402

2403-
Type HoleType::get(ASTContext &ctx, OriginatorType originator) {
2403+
Type HoleType::get(ASTContext &ctx, Originator originator) {
24042404
assert(originator);
2405-
auto properties = reinterpret_cast<TypeBase *>(originator.getOpaqueValue())
2406-
->getRecursiveProperties();
2407-
properties |= RecursiveTypeProperties::HasTypeHole;
2408-
2409-
auto arena = getArena(properties);
2405+
auto arena = getArena(RecursiveTypeProperties::HasTypeHole);
24102406
return new (ctx, arena)
24112407
HoleType(ctx, originator, RecursiveTypeProperties::HasTypeHole);
24122408
}

lib/AST/ASTDumper.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3512,12 +3512,14 @@ namespace {
35123512

35133513
void visitHoleType(HoleType *T, StringRef label) {
35143514
printCommon(label, "hole_type");
3515-
auto originatorTy = T->getOriginatorType();
3516-
if (auto *typeVar = originatorTy.dyn_cast<TypeVariableType *>()) {
3515+
auto originator = T->getOriginator();
3516+
if (auto *typeVar = originator.dyn_cast<TypeVariableType *>()) {
35173517
printRec("type_variable", typeVar);
3518+
} else if (auto *VD = originator.dyn_cast<VarDecl *>()) {
3519+
VD->dumpRef(PrintWithColorRAII(OS, DeclColor).getOS());
35183520
} else {
35193521
printRec("dependent_member_type",
3520-
originatorTy.get<DependentMemberType *>());
3522+
originator.get<DependentMemberType *>());
35213523
}
35223524
PrintWithColorRAII(OS, ParenthesisColor) << ')';
35233525
}

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3822,8 +3822,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
38223822
void visitHoleType(HoleType *T) {
38233823
if (Options.PrintTypesForDebugging) {
38243824
Printer << "<<hole for ";
3825-
auto originatorTy = T->getOriginatorType();
3826-
visit(Type(reinterpret_cast<TypeBase *>(originatorTy.getOpaqueValue())));
3825+
auto originator = T->getOriginator();
3826+
if (auto *typeVar = originator.dyn_cast<TypeVariableType *>()) {
3827+
visit(typeVar);
3828+
} else if (auto *VD = originator.dyn_cast<VarDecl *>()) {
3829+
Printer << "decl = ";
3830+
Printer << VD->getName();
3831+
} else {
3832+
visit(originator.get<DependentMemberType *>());
3833+
}
38273834
Printer << ">>";
38283835
} else {
38293836
Printer << "<<hole>>";

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7016,7 +7016,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
70167016
// type but a hole.
70177017
auto shouldRecordFixForHole = [&](HoleType *baseType) {
70187018
auto *originator =
7019-
baseType->getOriginatorType().dyn_cast<TypeVariableType *>();
7019+
baseType->getOriginator().dyn_cast<TypeVariableType *>();
70207020

70217021
if (!originator)
70227022
return false;

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ sawSolution(const constraints::Solution &S) {
10121012
return S.simplifyType(completionTy.transform([&](Type type) {
10131013
if (auto *hole = type->getAs<HoleType>()) {
10141014
if (auto *typeVar =
1015-
hole->getOriginatorType().dyn_cast<TypeVariableType *>()) {
1015+
hole->getOriginator().dyn_cast<TypeVariableType *>()) {
10161016
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
10171017
// Code completion depends on generic parameter type being
10181018
// represented in terms of `ArchetypeType` since it's easy

0 commit comments

Comments
 (0)