Skip to content

TypeResolution: Stop resolving unqualified protocol type aliases to DependentMemberType in structural stage #79322

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
merged 2 commits into from
Feb 14, 2025
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
5 changes: 0 additions & 5 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,6 @@ Type StructuralTypeRequest::evaluate(Evaluator &evaluator,
/*packElementOpener*/ nullptr)
.resolveType(underlyingTypeRepr);

// Don't build a generic siganture for a protocol extension, because this
// request might be evaluated while building a protocol requirement signature.
if (parentDC->getSelfProtocolDecl())
return result;

Type parent;
if (parentDC->isTypeContext())
parent = parentDC->getSelfInterfaceType();
Expand Down
69 changes: 34 additions & 35 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,17 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
selfType = foundDC->getSelfInterfaceType();

if (selfType->is<GenericTypeParamType>()) {
if (isa<ProtocolDecl>(typeDecl->getDeclContext())) {
if (isa<AssociatedTypeDecl>(typeDecl) ||
(isa<TypeAliasDecl>(typeDecl) &&
!cast<TypeAliasDecl>(typeDecl)->isGeneric() &&
!isSpecialized)) {
if (getStage() == TypeResolutionStage::Structural) {
return DependentMemberType::get(selfType, typeDecl->getName());
} else if (auto assocType = dyn_cast<AssociatedTypeDecl>(typeDecl)) {
typeDecl = assocType->getAssociatedTypeAnchor();
if (auto assocType = dyn_cast<AssociatedTypeDecl>(typeDecl)) {
if (getStage() == TypeResolutionStage::Structural) {
return DependentMemberType::get(selfType, typeDecl->getName());
}

typeDecl = assocType->getAssociatedTypeAnchor();
} else if (auto *aliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
if (isa<ProtocolDecl>(typeDecl->getDeclContext()) &&
getStage() == TypeResolutionStage::Structural) {
if (aliasDecl && !aliasDecl->isGeneric()) {
return adjustAliasType(aliasDecl->getStructuralType());
}
}
}
Expand Down Expand Up @@ -5902,42 +5904,39 @@ TypeResolver::resolveExistentialType(ExistentialTypeRepr *repr,
if (constraintType->hasError())
return ErrorType::get(getASTContext());

if (constraintType->isConstraintType()) {
return ExistentialType::get(constraintType);
}

//TO-DO: generalize this and emit the same erorr for some P?
if (!constraintType->isConstraintType()) {
// Emit a tailored diagnostic for the incorrect optional
// syntax 'any P?' with a fix-it to add parenthesis.
auto wrapped = constraintType->getOptionalObjectType();
if (wrapped && (wrapped->is<ExistentialType>() ||
wrapped->is<ExistentialMetatypeType>())) {
std::string fix;
llvm::raw_string_ostream OS(fix);
constraintType->print(OS, PrintOptions::forDiagnosticArguments());
diagnose(repr->getLoc(), diag::incorrect_optional_any,
constraintType)
//
// Emit a tailored diagnostic for the incorrect optional
// syntax 'any P?' with a fix-it to add parenthesis.
auto wrapped = constraintType->getOptionalObjectType();
if (wrapped && (wrapped->is<ExistentialType>() ||
wrapped->is<ExistentialMetatypeType>())) {
std::string fix;
llvm::raw_string_ostream OS(fix);
constraintType->print(OS, PrintOptions::forDiagnosticArguments());
diagnose(repr->getLoc(), diag::incorrect_optional_any, constraintType)
.fixItReplace(repr->getSourceRange(), fix);

// Recover by returning the intended type, but mark the type
// representation as invalid to prevent it from being diagnosed elsewhere.
repr->setInvalid();
return constraintType;
}

// Recover by returning the intended type, but mark the type
// representation as invalid to prevent it from being diagnosed elsewhere.
repr->setInvalid();
} else if (constraintType->is<ExistentialType>()) {
// Diagnose redundant `any` on an already existential type e.g. any (any P)
// with a fix-it to remove first any.
if (constraintType->is<ExistentialType>()) {
diagnose(repr->getLoc(), diag::redundant_any_in_existential,
ExistentialType::get(constraintType))
.fixItRemove(repr->getAnyLoc());
return constraintType;
}

diagnose(repr->getLoc(), diag::redundant_any_in_existential,
ExistentialType::get(constraintType))
.fixItRemove(repr->getAnyLoc());
} else {
diagnose(repr->getLoc(), diag::any_not_existential,
constraintType->isTypeParameter(), constraintType)
.fixItRemove(repr->getAnyLoc());
return constraintType;
}

return ExistentialType::get(constraintType);
return constraintType;
}

NeverNullType TypeResolver::resolveMetatypeType(MetatypeTypeRepr *repr,
Expand Down
16 changes: 16 additions & 0 deletions test/type/explicit_existential_protocol_typealias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-typecheck-verify-swift -target %target-swift-5.9-abi-triple -enable-upcoming-feature ExistentialAny

// REQUIRES: swift_feature_ExistentialAny

protocol P {
typealias PAlias1 = P

func f1() -> any PAlias1
func g1<T>(_: T) -> any PAlias1
}
extension P {
typealias PAlias2 = P

func f2() -> any PAlias2 {}
func g2<T>(_: T) -> any PAlias2 {}
}