Skip to content

TypeResolution: Map any constraint into context before checking validity #79296

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

Closed
wants to merge 2 commits into from
Closed
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
80 changes: 53 additions & 27 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5902,42 +5902,68 @@ TypeResolver::resolveExistentialType(ExistentialTypeRepr *repr,
if (constraintType->hasError())
return ErrorType::get(getASTContext());

auto contextualConstraintType = constraintType;

// Map the constraint type into context before performing validity checks.
switch (this->resolution.getStage()) {
case TypeResolutionStage::Structural:
// In the structural stage we are not allowed to compute the generic
// signature because we may already be in the middle of computing it.
// If validity is questionable, i.e., the constraint type is a type
// parameter, assume the constraint type is valid.
if (constraintType->isTypeParameter()) {
return ExistentialType::get(constraintType);
}

break;
case TypeResolutionStage::Interface:
contextualConstraintType = this->resolution.getGenericSignature()
.getGenericEnvironment()
->mapTypeIntoContext(constraintType);

// If there was a problem with the type, don't emit another diagnostic.
if (contextualConstraintType->hasError()) {
return ErrorType::get(getASTContext());
}

break;
}

if (contextualConstraintType->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 = contextualConstraintType->getOptionalObjectType();
if (wrapped && (wrapped->is<ExistentialType>() ||
wrapped->is<ExistentialMetatypeType>())) {
std::string fix;
llvm::raw_string_ostream OS(fix);
contextualConstraintType->print(OS, PrintOptions::forDiagnosticArguments());
diagnose(repr->getLoc(), diag::incorrect_optional_any,
contextualConstraintType)
.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 (contextualConstraintType->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(contextualConstraintType))
.fixItRemove(repr->getAnyLoc());
} else {
diagnose(repr->getLoc(), diag::any_not_existential,
constraintType->isTypeParameter(), constraintType)
contextualConstraintType->is<ArchetypeType>(),
contextualConstraintType)
.fixItRemove(repr->getAnyLoc());
return constraintType;
}

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

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

// REQUIRES: swift_feature_ExistentialAny

do {
protocol P {
typealias PAlias = P

func f() -> any PAlias
func g<T>(_: T) -> any PAlias
}
}
do {
protocol P {
associatedtype A

func f1() -> any A
// expected-error@-1:18 {{'any' has no effect on type parameter 'Self.A'}}

typealias Alias = A

func f2() -> any Alias
// expected-error@-1:18 {{'any' has no effect on type parameter 'Self.Alias' (aka 'Self.A')}}
}
}
do {
protocol P {
associatedtype A where A == Int

func f1() -> any A
// expected-error@-1:18 {{'any' has no effect on concrete type 'Int'}}
func g1<T>(_: T) -> any A
// expected-error@-1:25 {{'any' has no effect on concrete type 'Int'}}

typealias Alias = A

func f2() -> any Alias
// expected-error@-1:18 {{'any' has no effect on concrete type 'Self.Alias' (aka 'Int')}}
func g2<T>(_: T) -> any Alias
// expected-error@-1:25 {{'any' has no effect on concrete type 'Self.Alias' (aka 'Int')}}
}
}
do {
protocol P {
associatedtype A where A == Invalid
// expected-error@-1 {{cannot find type 'Invalid' in scope}}

func f() -> any A
}
}