Skip to content

Fix a couple of silly problems with extensions [4.0] #10443

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
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
3 changes: 0 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1337,9 +1337,6 @@ ERROR(extension_constrained_inheritance,none,
"inheritance clause", (Type))
ERROR(extension_protocol_inheritance,none,
"extension of protocol %0 cannot have an inheritance clause", (Type))
ERROR(extension_protocol_via_typealias,none,
"protocol %0 in the module being compiled cannot be extended via a "
"type alias", (Type))
ERROR(objc_generic_extension_using_type_parameter,none,
"extension of a generic Objective-C class cannot access the class's "
"generic parameters at runtime", ())
Expand Down
11 changes: 2 additions & 9 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,10 @@ DeclContext::getAsTypeOrTypeExtensionContext() const {
auto ED = cast<ExtensionDecl>(this);
auto type = ED->getExtendedType();

if (type.isNull() || type->hasError())
if (!type)
return nullptr;

if (auto ND = type->getNominalOrBoundGenericNominal())
return ND;

if (auto unbound = dyn_cast<UnboundGenericType>(type.getPointer())) {
return unbound->getDecl();
}

return nullptr;
return type->getAnyNominal();
}

case DeclContextKind::GenericTypeDecl:
Expand Down
12 changes: 0 additions & 12 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7804,18 +7804,6 @@ void TypeChecker::validateExtension(ExtensionDecl *ext) {
// FIXME: Probably the above comes up elsewhere, perhaps getAs<>()
// should be fixed.
if (auto proto = extendedType->getCanonicalType()->getAs<ProtocolType>()) {
if (!isa<ProtocolType>(extendedType.getPointer()) &&
proto->getDecl()->getParentModule() == ext->getParentModule()) {
// Protocols in the same module cannot be extended via a typealias;
// we could end up being unable to resolve the generic signature.
diagnose(ext->getLoc(), diag::extension_protocol_via_typealias, proto)
.fixItReplace(ext->getExtendedTypeLoc().getSourceRange(),
proto->getDecl()->getName().str());
ext->setInvalid();
ext->getExtendedTypeLoc().setInvalidType(Context);
return;
}

GenericEnvironment *env;
std::tie(env, extendedType) =
checkExtensionGenericParams(*this, ext, proto, ext->getGenericParams());
Expand Down
6 changes: 6 additions & 0 deletions test/decl/ext/generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ struct S5<Q> {
}

extension S5 : P4 {}

// rdar://problem/21607421
public typealias Array2 = Array
extension Array2 where QQQ : VVV {}
// expected-error@-1 {{use of undeclared type 'QQQ'}}
// expected-error@-2 {{use of undeclared type 'VVV'}}
5 changes: 3 additions & 2 deletions test/decl/ext/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -921,9 +921,10 @@ extension BadProto1 {
}
}

// rdar://problem/20756244
protocol BadProto3 { }
typealias BadProto4 = BadProto3
extension BadProto4 { } // expected-error{{protocol 'BadProto3' in the module being compiled cannot be extended via a type alias}}{{11-20=BadProto3}}
extension BadProto4 { } // okay

typealias RawRepresentableAlias = RawRepresentable
extension RawRepresentableAlias { } // okay
Expand All @@ -948,6 +949,6 @@ class BadClass5 : BadProto5 {} // expected-error{{type 'BadClass5' does not conf
typealias A = BadProto1
typealias B = BadProto1

extension A & B { // expected-error{{protocol 'BadProto1' in the module being compiled cannot be extended via a type alias}}
extension A & B { // okay

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: asserts
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
protocol P}extension P{{}typealias a:P}extension P.a{protocol P