Skip to content

Commit 3c8a8a8

Browse files
authored
Merge pull request #82292 from hamishknight/bigger-hammer
[AST] Replace type variables and placeholders in original ErrorTypes
2 parents 286f975 + 6257828 commit 3c8a8a8

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

lib/AST/ASTContext.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,12 +3576,35 @@ void LocatableType::Profile(llvm::FoldingSetNodeID &id, SourceLoc loc,
35763576
// Simple accessors.
35773577
Type ErrorType::get(const ASTContext &C) { return C.TheErrorType; }
35783578

3579+
static Type replacingTypeVariablesAndPlaceholders(Type ty) {
3580+
if (!ty || !ty->hasTypeVariableOrPlaceholder())
3581+
return ty;
3582+
3583+
auto &ctx = ty->getASTContext();
3584+
3585+
return ty.transformRec([&](Type ty) -> std::optional<Type> {
3586+
if (!ty->hasTypeVariableOrPlaceholder())
3587+
return ty;
3588+
3589+
// Match the logic in `Solution::simplifyType` and use UnresolvedType.
3590+
// FIXME: Ideally we'd get rid of UnresolvedType and just use a fresh
3591+
// PlaceholderType, but we don't currently support placeholders with no
3592+
// originators.
3593+
auto *typePtr = ty.getPointer();
3594+
if (isa<TypeVariableType>(typePtr) || isa<PlaceholderType>(typePtr))
3595+
return ctx.TheUnresolvedType;
3596+
3597+
return std::nullopt;
3598+
});
3599+
}
3600+
35793601
Type ErrorType::get(Type originalType) {
3602+
// The original type is only used for printing/debugging, and we don't support
3603+
// solver-allocated ErrorTypes. As such, fold any type variables and
3604+
// placeholders into UnresolvedTypes, which print as placeholders.
3605+
originalType = replacingTypeVariablesAndPlaceholders(originalType);
3606+
35803607
ASSERT(originalType);
3581-
// We don't currently support solver-allocated error types, if we want
3582-
// this in the future we'll need to adjust `Solution::simplifyType` to fold
3583-
// them into regular ErrorTypes. Additionally, any clients of
3584-
// `typesSatisfyConstraint` will need to be taught not to pass such types.
35853608
ASSERT(!originalType->getRecursiveProperties().isSolverAllocated() &&
35863609
"Solver-allocated error types not supported");
35873610

lib/AST/TypeSubstitution.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,8 @@ Type TypeSubstituter::transformDependentMemberType(DependentMemberType *dependen
446446

447447
auto result = conformance.getTypeWitness(assocType, IFS.getOptions());
448448
if (result->is<ErrorType>()) {
449-
// Substitute the base type for the original ErrorType for type printing.
450-
// Avoid doing this if the substitutions introduce type variables or
451-
// placeholders since ErrorTypes can't be solver-allocated currently (and
452-
// type variables aren't helpful when printing anyway).
453449
auto substBase = origBase.subst(IFS);
454-
if (!substBase->hasTypeVariableOrPlaceholder())
455-
return DependentMemberType::get(ErrorType::get(substBase), assocType);
450+
return DependentMemberType::get(ErrorType::get(substBase), assocType);
456451
}
457452
return result;
458453
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"signature":"swift::ErrorType::get(swift::Type)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
@convention(c) _->Int

0 commit comments

Comments
 (0)