Skip to content

[Sema] Disallow conditional conformances on objective-c generics. #14628

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 15, 2018
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: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,10 @@ ERROR(objc_runtime_visible_cannot_conform_to_objc_protocol,none,
"class %0 cannot conform to @objc protocol %1 because "
"the class is only visible via the Objective-C runtime",
(Type, Type))
ERROR(objc_generics_cannot_conditionally_conform,none,
"type %0 cannot conditionally conform to protocol %1 because "
"the type uses the Objective-C generics model",
(Type, Type))
ERROR(protocol_has_missing_requirements,none,
"type %0 cannot conform to protocol %1 because it has requirements that "
"cannot be satisfied", (Type, Type))
Expand Down
27 changes: 1 addition & 26 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/Basic/STLExtras.h"
#include "swift/AST/AttrKind.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/TypeOrExtensionDecl.h"
#include "llvm/ADT/Optional.h"
#include <limits.h>
#include <vector>
Expand All @@ -36,32 +37,6 @@ enum DeclAttrKind : unsigned;
class SynthesizedExtensionAnalyzer;
struct PrintOptions;

/// \brief Describes either a nominal type declaration or an extension
/// declaration.
struct TypeOrExtensionDecl {
llvm::PointerUnion<NominalTypeDecl *, ExtensionDecl *> Decl;

TypeOrExtensionDecl() = default;

TypeOrExtensionDecl(NominalTypeDecl *D);
TypeOrExtensionDecl(ExtensionDecl *D);

/// \brief Return the contained *Decl as the Decl superclass.
class Decl *getAsDecl() const;
/// \brief Return the contained *Decl as the DeclContext superclass.
DeclContext *getAsDeclContext() const;
/// \brief Return the contained NominalTypeDecl or that of the extended type
/// in the ExtensionDecl.
NominalTypeDecl *getBaseNominal() const;

/// \brief Is the contained pointer null?
bool isNull() const;
explicit operator bool() const { return !isNull(); }

bool operator==(TypeOrExtensionDecl rhs) { return Decl == rhs.Decl; }
bool operator!=(TypeOrExtensionDecl rhs) { return Decl != rhs.Decl; }
bool operator<(TypeOrExtensionDecl rhs) { return Decl < rhs.Decl; }
};

/// Necessary information for archetype transformation during printing.
struct TypeTransformContext {
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeAlignments.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace swift {
class ExtensionDecl;
class GenericEnvironment;
class GenericTypeParamDecl;
class NominalTypeDecl;
class NormalProtocolConformance;
class OperatorDecl;
class Pattern;
Expand Down Expand Up @@ -90,6 +91,7 @@ LLVM_DECLARE_TYPE_ALIGNMENT(swift::OperatorDecl, swift::DeclAlignInBits)
LLVM_DECLARE_TYPE_ALIGNMENT(swift::ProtocolDecl, swift::DeclAlignInBits)
LLVM_DECLARE_TYPE_ALIGNMENT(swift::TypeDecl, swift::DeclAlignInBits)
LLVM_DECLARE_TYPE_ALIGNMENT(swift::ValueDecl, swift::DeclAlignInBits)
LLVM_DECLARE_TYPE_ALIGNMENT(swift::NominalTypeDecl, swift::DeclAlignInBits)
LLVM_DECLARE_TYPE_ALIGNMENT(swift::ExtensionDecl, swift::DeclAlignInBits)

LLVM_DECLARE_TYPE_ALIGNMENT(swift::TypeBase, swift::TypeAlignInBits)
Expand Down
56 changes: 56 additions & 0 deletions include/swift/AST/TypeOrExtensionDecl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===- TypeOrExtensionDecl.h - Swift Language Declaration ASTs -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the TypeOrExtensionDecl struct, separately to Decl.h so
// that this can be included in files that Decl.h includes.
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably doesn't actually work, because PointerUnion needs to know the alignment of the types in it to know where to stick spare bits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to compile.

Copy link
Contributor

Choose a reason for hiding this comment

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

It compiles as long as you also include Decl.h before the template actually has to be instantiated, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do I address this then?

Copy link
Contributor

Choose a reason for hiding this comment

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

Untangle the headers? Or…ah, it looks like we have swift/AST/TypeAlignments.h for this. ASTNode.h uses it in a similar way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Neat; changed it to use that header.

//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_TYPE_OR_EXTENSION_DECL_H
#define SWIFT_TYPE_OR_EXTENSION_DECL_H

#include "swift/AST/TypeAlignments.h"
#include "llvm/ADT/PointerUnion.h"

namespace swift {

/// \brief Describes either a nominal type declaration or an extension
/// declaration.
struct TypeOrExtensionDecl {
// (The definitions are in Decl.cpp.)
llvm::PointerUnion<NominalTypeDecl *, ExtensionDecl *> Decl;

TypeOrExtensionDecl() = default;

TypeOrExtensionDecl(NominalTypeDecl *D);
TypeOrExtensionDecl(ExtensionDecl *D);

/// \brief Return the contained *Decl as the Decl superclass.
class Decl *getAsDecl() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Stray class here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The field is called Decl: clang specifically said I should write class Decl to disambiguate.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I guess I would have just renamed the field.

/// \brief Return the contained *Decl as the DeclContext superclass.
DeclContext *getAsDeclContext() const;
/// \brief Return the contained NominalTypeDecl or that of the extended type
/// in the ExtensionDecl.
NominalTypeDecl *getBaseNominal() const;

/// \brief Is the contained pointer null?
bool isNull() const;
explicit operator bool() const { return !isNull(); }

bool operator==(TypeOrExtensionDecl rhs) { return Decl == rhs.Decl; }
bool operator!=(TypeOrExtensionDecl rhs) { return Decl != rhs.Decl; }
bool operator<(TypeOrExtensionDecl rhs) { return Decl < rhs.Decl; }
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this one makes sense to provide.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just mindlessly duplicating the PointerUnion API.

};

} // end namespace swift

#endif
17 changes: 0 additions & 17 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,6 @@ void PrintOptions::clearSynthesizedExtension() {
TransformContext.reset();
}

TypeOrExtensionDecl::TypeOrExtensionDecl(NominalTypeDecl *D) : Decl(D) {}
TypeOrExtensionDecl::TypeOrExtensionDecl(ExtensionDecl *D) : Decl(D) {}

Decl *TypeOrExtensionDecl::getAsDecl() const {
if (auto NTD = Decl.dyn_cast<NominalTypeDecl *>())
return NTD;

return Decl.get<ExtensionDecl *>();
}
DeclContext *TypeOrExtensionDecl::getAsDeclContext() const {
return getAsDecl()->getInnermostDeclContext();
}
NominalTypeDecl *TypeOrExtensionDecl::getBaseNominal() const {
return getAsDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
}
bool TypeOrExtensionDecl::isNull() const { return Decl.isNull(); }

TypeTransformContext::TypeTransformContext(Type T)
: BaseType(T.getPointer()) {
assert(T->mayHaveMembers());
Expand Down
17 changes: 17 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5689,3 +5689,20 @@ UnifiedStatsReporter::getStatsTracer(StringRef EventName, const Decl *D) {
// Return inert tracer object.
return FrontendStatsTracer();
}

TypeOrExtensionDecl::TypeOrExtensionDecl(NominalTypeDecl *D) : Decl(D) {}
TypeOrExtensionDecl::TypeOrExtensionDecl(ExtensionDecl *D) : Decl(D) {}

Decl *TypeOrExtensionDecl::getAsDecl() const {
if (auto NTD = Decl.dyn_cast<NominalTypeDecl *>())
return NTD;

return Decl.get<ExtensionDecl *>();
}
DeclContext *TypeOrExtensionDecl::getAsDeclContext() const {
return getAsDecl()->getInnermostDeclContext();
}
NominalTypeDecl *TypeOrExtensionDecl::getBaseNominal() const {
return getAsDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
}
bool TypeOrExtensionDecl::isNull() const { return Decl.isNull(); }
21 changes: 21 additions & 0 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,27 @@ checkIndividualConformance(NormalProtocolConformance *conformance,
}
}

// Not every protocol/type is compatible with conditional conformances.
if (!conformance->getConditionalRequirements().empty()) {
auto nestedType = canT;
// Obj-C generics cannot be looked up at runtime, so we don't support
// conditional conformances involving them. Check the full stack of nested
// types for any obj-c ones.
while (nestedType) {
if (auto clas = nestedType->getClassOrBoundGenericClass()) {
if (clas->usesObjCGenericsModel()) {
TC.diagnose(ComplainLoc,
diag::objc_generics_cannot_conditionally_conform, T,
Proto->getDeclaredType());
conformance->setInvalid();
return conformance;
}
}

nestedType = nestedType.getNominalParent();
}
}

// If the protocol contains missing requirements, it can't be conformed to
// at all.
if (Proto->hasMissingRequirements()) {
Expand Down
7 changes: 7 additions & 0 deletions test/Generics/Inputs/conditional_conformances_objc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import Foundation;

@interface ObjC <ObjectType>: NSObject
@end

@interface ObjC2: NSObject
@end
34 changes: 34 additions & 0 deletions test/Generics/conditional_conformances_objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %target-typecheck-verify-swift -import-objc-header %S/Inputs/conditional_conformances_objc.h

// REQUIRES: objc_interop

protocol Foo {}
extension ObjC: Foo where ObjectType == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}

protocol Bar {}
extension ObjC: Bar where ObjectType: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}

extension ObjC {
struct Struct {
enum Enum {}
}
class Class<T> {}
}

extension ObjC.Struct: Foo where ObjectType == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}
extension ObjC.Struct: Bar where ObjectType: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}

extension ObjC.Struct.Enum: Foo where ObjectType == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct.Enum' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}
extension ObjC.Struct.Enum: Bar where ObjectType: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct.Enum' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}

extension ObjC.Class: Foo where T == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>.Class<T>' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}
extension ObjC.Class: Bar where T: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>.Class<T>' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}