Skip to content

Don't canonicalize invalid GenericFunctionTypes in diagnostics #38327

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
24 changes: 17 additions & 7 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
TypeBase *getWithoutSyntaxSugar();

/// getASTContext - Return the ASTContext that this type belongs to.
ASTContext &getASTContext() {
// If this type is canonical, it has the ASTContext in it.
if (isCanonical())
return *const_cast<ASTContext*>(Context);
// If not, canonicalize it to get the Context.
return *const_cast<ASTContext*>(getCanonicalType()->Context);
}
ASTContext &getASTContext();

/// isEqual - Return true if these two types are equal, ignoring sugar.
///
Expand Down Expand Up @@ -5994,6 +5988,22 @@ class PlaceholderType : public TypeBase {
};
DEFINE_EMPTY_CAN_TYPE_WRAPPER(PlaceholderType, Type)

/// getASTContext - Return the ASTContext that this type belongs to.
inline ASTContext &TypeBase::getASTContext() {
// If this type is canonical, it has the ASTContext in it.
if (isCanonical())
return *const_cast<ASTContext*>(Context);

// getCanonicalType() on GenericFunctionType is very expensive;
// instead we can more easily fish the ASTContext out of any
// structural sub-component.
if (auto *genericFnType = dyn_cast<GenericFunctionType>(this))
return genericFnType->getGenericParams()[0]->getASTContext();

// If not, canonicalize it to get the Context.
return *const_cast<ASTContext*>(getCanonicalType()->Context);
}

inline bool TypeBase::isTypeVariableOrMember() {
if (is<TypeVariableType>())
return true;
Expand Down
30 changes: 15 additions & 15 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,19 @@ static bool isInterestingTypealias(Type type) {
return true;
}

/// Walks the type recursivelly desugaring types to display, but skipping
/// `GenericTypeParamType` because we would lose association with its original
/// declaration and end up presenting the parameter in τ_0_0 format on
/// diagnostic.
static Type getAkaTypeForDisplay(Type type) {
return type.transform([](Type visitTy) -> Type {
if (isa<SugarType>(visitTy.getPointer()) &&
!isa<GenericTypeParamType>(visitTy.getPointer()))
return getAkaTypeForDisplay(visitTy->getDesugaredType());
return visitTy;
});
}

/// Decide whether to show the desugared type or not. We filter out some
/// cases to avoid too much noise.
static bool shouldShowAKA(Type type, StringRef typeName) {
Expand All @@ -517,7 +530,7 @@ static bool shouldShowAKA(Type type, StringRef typeName) {
// If they are textually the same, don't show them. This can happen when
// they are actually different types, because they exist in different scopes
// (e.g. everyone names their type parameters 'T').
if (typeName == type->getCanonicalType()->getString())
if (typeName == getAkaTypeForDisplay(type).getString())
return false;

return true;
Expand All @@ -532,7 +545,7 @@ static bool typeSpellingIsAmbiguous(Type type,
for (auto arg : Args) {
if (arg.getKind() == DiagnosticArgumentKind::Type) {
auto argType = arg.getAsType();
if (argType && !argType->isEqual(type) &&
if (argType && argType->getWithoutParens().getPointer() != type.getPointer() &&
argType->getWithoutParens().getString(PO) == type.getString(PO)) {
return true;
}
Expand All @@ -541,19 +554,6 @@ static bool typeSpellingIsAmbiguous(Type type,
return false;
}

/// Walks the type recursivelly desugaring types to display, but skipping
/// `GenericTypeParamType` because we would lose association with its original
/// declaration and end up presenting the parameter in τ_0_0 format on
/// diagnostic.
static Type getAkaTypeForDisplay(Type type) {
return type.transform([](Type visitTy) -> Type {
if (isa<SugarType>(visitTy.getPointer()) &&
!isa<GenericTypeParamType>(visitTy.getPointer()))
return getAkaTypeForDisplay(visitTy->getDesugaredType());
return visitTy;
});
}

/// Determine whether this is the main actor type.
static bool isMainActor(Type type) {
if (auto nominal = type->getAnyNominal()) {
Expand Down