Skip to content

[AST] Replace type variables and placeholders in original ErrorTypes #82292

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 27 additions & 4 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3576,12 +3576,35 @@ void LocatableType::Profile(llvm::FoldingSetNodeID &id, SourceLoc loc,
// Simple accessors.
Type ErrorType::get(const ASTContext &C) { return C.TheErrorType; }

static Type replacingTypeVariablesAndPlaceholders(Type ty) {
if (!ty || !ty->hasTypeVariableOrPlaceholder())
return ty;

auto &ctx = ty->getASTContext();

return ty.transformRec([&](Type ty) -> std::optional<Type> {
if (!ty->hasTypeVariableOrPlaceholder())
return ty;

// Match the logic in `Solution::simplifyType` and use UnresolvedType.
// FIXME: Ideally we'd get rid of UnresolvedType and just use a fresh
// PlaceholderType, but we don't currently support placeholders with no
// originators.
auto *typePtr = ty.getPointer();
if (isa<TypeVariableType>(typePtr) || isa<PlaceholderType>(typePtr))
return ctx.TheUnresolvedType;

return std::nullopt;
});
}

Type ErrorType::get(Type originalType) {
// The original type is only used for printing/debugging, and we don't support
// solver-allocated ErrorTypes. As such, fold any type variables and
// placeholders into UnresolvedTypes, which print as placeholders.
originalType = replacingTypeVariablesAndPlaceholders(originalType);

ASSERT(originalType);
// We don't currently support solver-allocated error types, if we want
// this in the future we'll need to adjust `Solution::simplifyType` to fold
// them into regular ErrorTypes. Additionally, any clients of
// `typesSatisfyConstraint` will need to be taught not to pass such types.
ASSERT(!originalType->getRecursiveProperties().isSolverAllocated() &&
"Solver-allocated error types not supported");

Expand Down
7 changes: 1 addition & 6 deletions lib/AST/TypeSubstitution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,8 @@ Type TypeSubstituter::transformDependentMemberType(DependentMemberType *dependen

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