Skip to content

Commit 1fe2e4b

Browse files
committed
Relax Restrictions on Placeholder Types Appearing in Interface Types
These restrictions are meant to keep placeholder types from escaping TypeCheckType. But there's really no harm in that happening as long as we diagnose it on the way out in the places it's banned. (We also need to make sure we're only diagnosing things in primaries, but that's a minor issue). The end result is that we lose information because a lot of the AST that has placeholders in it becomes filled with error types instead. Lift the restriction on placeholders appearing in the interface type, teach the mangler to treat them as unresolved types, and teach serialization to treat them as error types.
1 parent 22ad19f commit 1fe2e4b

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,6 @@ GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
36403640
Optional<ExtInfo> info) {
36413641
assert(sig && "no generic signature for generic function type?!");
36423642
assert(!result->hasTypeVariable());
3643-
assert(!result->hasPlaceholder());
36443643

36453644
llvm::FoldingSetNodeID id;
36463645
GenericFunctionType::Profile(id, sig, params, result, info);

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,14 +1073,14 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
10731073
TypeBase *tybase = type.getPointer();
10741074
switch (type->getKind()) {
10751075
case TypeKind::TypeVariable:
1076-
case TypeKind::Placeholder:
10771076
llvm_unreachable("mangling type variable");
10781077

10791078
case TypeKind::Module:
10801079
llvm_unreachable("Cannot mangle module type yet");
10811080

10821081
case TypeKind::Error:
10831082
case TypeKind::Unresolved:
1083+
case TypeKind::Placeholder:
10841084
appendOperator("Xe");
10851085
return;
10861086

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8643,6 +8643,7 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
86438643
Type convertType = target.getExprConversionType();
86448644
auto shouldCoerceToContextualType = [&]() {
86458645
return convertType &&
8646+
!convertType->hasPlaceholder() &&
86468647
!target.isOptionalSomePatternInit() &&
86478648
!(solution.getType(resultExpr)->isUninhabited() &&
86488649
cs.getContextualTypePurpose(target.getAsExpr())

lib/Sema/TypeCheckDecl.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,8 +2213,9 @@ ResultTypeRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
22132213
const auto options =
22142214
TypeResolutionOptions(TypeResolverContext::FunctionResult);
22152215
auto *const dc = decl->getInnermostDeclContext();
2216-
return TypeResolution::forInterface(dc, options, /*unboundTyOpener*/ nullptr,
2217-
/*placeholderHandler*/ nullptr)
2216+
return TypeResolution::forInterface(dc, options,
2217+
/*unboundTyOpener*/ nullptr,
2218+
PlaceholderType::get)
22182219
.resolveType(resultTyRepr);
22192220
}
22202221

@@ -2298,7 +2299,6 @@ static Type validateParameterType(ParamDecl *decl) {
22982299

22992300
TypeResolutionOptions options(None);
23002301
OpenUnboundGenericTypeFn unboundTyOpener = nullptr;
2301-
HandlePlaceholderTypeReprFn placeholderHandler = nullptr;
23022302
if (isa<AbstractClosureExpr>(dc)) {
23032303
options = TypeResolutionOptions(TypeResolverContext::ClosureExpr);
23042304
options |= TypeResolutionFlags::AllowUnspecifiedTypes;
@@ -2309,7 +2309,6 @@ static Type validateParameterType(ParamDecl *decl) {
23092309
};
23102310
// FIXME: Don't let placeholder types escape type resolution.
23112311
// For now, just return the placeholder type.
2312-
placeholderHandler = PlaceholderType::get;
23132312
} else if (isa<AbstractFunctionDecl>(dc)) {
23142313
options = TypeResolutionOptions(TypeResolverContext::AbstractFunctionDecl);
23152314
} else if (isa<SubscriptDecl>(dc)) {
@@ -2332,7 +2331,7 @@ static Type validateParameterType(ParamDecl *decl) {
23322331

23332332
const auto resolution =
23342333
TypeResolution::forInterface(dc, options, unboundTyOpener,
2335-
placeholderHandler);
2334+
PlaceholderType::get);
23362335
auto Ty = resolution.resolveType(decl->getTypeRepr());
23372336

23382337
if (Ty->hasError()) {

lib/Serialization/Serialization.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4289,6 +4289,13 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
42894289
}
42904290

42914291
void visitPlaceholderType(const PlaceholderType *) {
4292+
// If for some reason we have a placeholder type while compiling with
4293+
// errors, just serialize an ErrorType and continue.
4294+
if (S.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
4295+
visitErrorType(
4296+
cast<ErrorType>(ErrorType::get(S.getASTContext()).getPointer()));
4297+
return;
4298+
}
42924299
llvm_unreachable("should not serialize a PlaceholderType");
42934300
}
42944301

0 commit comments

Comments
 (0)