Skip to content

Improve diagnostics when trying to extend existential type #60680

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
4 changes: 3 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1972,10 +1972,12 @@ NOTE(objc_generic_extension_using_type_parameter_here,none,
"generic parameter used here", ())
NOTE(objc_generic_extension_using_type_parameter_try_objc,none,
"add '@objc' to allow uses of 'self' within the function body", ())
ERROR(unsupported_existential_extension,none,
"extension of existential type %0 is not supported", (Type))
ERROR(invalid_nominal_extension,none,
"extension of type %0 must be declared as an extension of %1",
(Type, Type))
NOTE(invalid_nominal_extension_rewrite,none,
NOTE(invalid_extension_rewrite,none,
"did you mean to extend %0 instead?", (Type))
ERROR(synthesized_nominal_extension,none,
"cannot extend synthesized type %0", (Type))
Expand Down
22 changes: 16 additions & 6 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3084,21 +3084,31 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
const bool wasAlreadyInvalid = ED->isInvalid();
ED->setInvalid();
if (!extType->hasError() && extType->getAnyNominal()) {
auto canExtType = extType->getCanonicalType();
if (auto existential = canExtType->getAs<ExistentialType>()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can call getAs() on a non-canonical type, it will automatically desugar it, so you don’t need to move the canonical type computation from below

Copy link
Contributor Author

@cbjeukendrup cbjeukendrup Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using auto existential = extType->getAs<ExistentialType>() instead still gives a difference. The comment for the fixit in the test case becomes "did you mean to extend 'A4' (aka 'P4') instead?", while it will still replace the type with just P4. Wouldn't it be better if the comment just says "did you mean to extend 'P4' instead?"?

Or do we want that the fixit replaces the type with A4 in this case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think it's better for the error message to mention 'A4' (aka 'P4') because A4 is what's written in the extension, so it helps the programmer make the connection between A4 and P4. However, I don't feel too strongly about this and I'm fine with merging this change as-is.

ED->diagnose(diag::unsupported_existential_extension, extType)
.highlight(ED->getExtendedTypeRepr()->getSourceRange());
ED->diagnose(diag::invalid_extension_rewrite,
existential->getConstraintType())
.fixItReplace(ED->getExtendedTypeRepr()->getSourceRange(),
existential->getConstraintType()->getString());
return;
}

// If we've got here, then we have some kind of extension of a prima
// fascie non-nominal type. This can come up when we're projecting
// facie non-nominal type. This can come up when we're projecting
// typealiases out of bound generic types.
//
// struct Array<T> { typealias Indices = Range<Int> }
// extension Array.Indices.Bound {}
//
// Offer to rewrite it to the underlying nominal type.
auto canExtType = extType->getCanonicalType();
if (canExtType.getPointer() != extType.getPointer()) {
ED->diagnose(diag::invalid_nominal_extension, extType, canExtType)
.highlight(ED->getExtendedTypeRepr()->getSourceRange());
ED->diagnose(diag::invalid_nominal_extension_rewrite, canExtType)
.fixItReplace(ED->getExtendedTypeRepr()->getSourceRange(),
canExtType->getString());
.highlight(ED->getExtendedTypeRepr()->getSourceRange());
ED->diagnose(diag::invalid_extension_rewrite, canExtType)
.fixItReplace(ED->getExtendedTypeRepr()->getSourceRange(),
canExtType->getString());
return;
}
}
Expand Down
24 changes: 23 additions & 1 deletion test/decl/ext/extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ extension Tree.LimbContent.Contents {

extension Tree.BoughPayload.Contents {
// expected-error@-1 {{extension of type 'Tree.BoughPayload.Contents' (aka 'Nest<Int>') must be declared as an extension of 'Nest<Int>'}}
// expected-note@-2 {{did you mean to extend 'Nest<Int>' instead?}}
// expected-note@-2 {{did you mean to extend 'Nest<Int>' instead?}} {{11-37=Nest<Int>}}
}

// https://github.com/apple/swift/issues/52866
Expand All @@ -367,3 +367,25 @@ protocol Rdar66943328 {
}
extension Rdar66943328 where Assoc == Int // expected-error {{expected '{' in extension}}
#endif

// Reject extension of existential type

protocol P4 {}

extension any P4 {
// expected-error@-1 {{extension of existential type 'any P4' is not supported}}
// expected-note@-2 {{did you mean to extend 'P4' instead?}} {{11-17=P4}}
}

typealias A4 = P4

extension any A4 {
// expected-error@-1 {{extension of existential type 'any A4' (aka 'any P4') is not supported}}
// expected-note@-2 {{did you mean to extend 'P4' instead?}} {{11-17=P4}}
}

typealias B4 = any P4
extension B4 {
// expected-error@-1 {{extension of existential type 'B4' (aka 'any P4') is not supported}}
// expected-note@-2 {{did you mean to extend 'P4' instead?}} {{11-13=P4}}
}