Skip to content

Add a couple of regression tests and make a small cleanup #7346

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 3 commits into from
Feb 9, 2017
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
7 changes: 0 additions & 7 deletions include/swift/AST/ProtocolConformance.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "swift/AST/ConcreteDeclRef.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/AST/Substitution.h"
#include "swift/AST/Type.h"
#include "swift/AST/Types.h"
Expand Down Expand Up @@ -160,12 +159,6 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
getTypeWitnessSubstAndDecl(AssociatedTypeDecl *assocType,
LazyResolver *resolver) const;

static Type
getTypeWitnessByName(Type type,
ProtocolConformanceRef conformance,
Identifier name,
LazyResolver *resolver);

/// Apply the given function object to each type witness within this
/// protocol conformance.
///
Expand Down
7 changes: 7 additions & 0 deletions include/swift/AST/ProtocolConformanceRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/PointerUnion.h"
#include "swift/AST/TypeAlignments.h"
#include "swift/AST/Type.h"

namespace llvm {
class raw_ostream;
Expand Down Expand Up @@ -101,6 +102,12 @@ class ProtocolConformanceRef {
friend llvm::hash_code hash_value(ProtocolConformanceRef conformance) {
return llvm::hash_value(conformance.Union.getOpaqueValue());
}

static Type
getTypeWitnessByName(Type type,
ProtocolConformanceRef conformance,
Identifier name,
LazyResolver *resolver);
};

} // end namespace swift
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3848,7 +3848,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,

// Find the Objective-C class type we bridge to.
if (conformance->isConcrete()) {
return ProtocolConformance::getTypeWitnessByName(
return ProtocolConformanceRef::getTypeWitnessByName(
type, *conformance, Id_ObjectiveCType,
getLazyResolver());
} else {
Expand Down
68 changes: 34 additions & 34 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,40 @@ ProtocolConformanceRef::getInherited(ProtocolDecl *parent) const {
llvm_unreachable("unhandled ProtocolConformanceRef");
}

Type
ProtocolConformanceRef::getTypeWitnessByName(Type type,
ProtocolConformanceRef conformance,
Identifier name,
LazyResolver *resolver) {
// For an archetype, retrieve the nested type with the appropriate
// name. There are no conformance tables.
if (auto archetype = type->getAs<ArchetypeType>()) {
return archetype->getNestedType(name);
}

// Find the named requirement.
AssociatedTypeDecl *assocType = nullptr;
auto members = conformance.getRequirement()->lookupDirect(name);
for (auto member : members) {
assocType = dyn_cast<AssociatedTypeDecl>(member);
if (assocType)
break;
}

// FIXME: Shouldn't this be a hard error?
if (!assocType)
return nullptr;

if (conformance.isAbstract())
return DependentMemberType::get(type, assocType);

auto concrete = conformance.getConcrete();
if (!concrete->hasTypeWitness(assocType, resolver)) {
return nullptr;
}
return concrete->getTypeWitness(assocType, resolver).getReplacement();
}

void *ProtocolConformance::operator new(size_t bytes, ASTContext &context,
AllocationArena arena,
unsigned alignment) {
Expand Down Expand Up @@ -154,40 +188,6 @@ ProtocolConformance::getTypeWitness(AssociatedTypeDecl *assocType,
return getTypeWitnessSubstAndDecl(assocType, resolver).first;
}

Type
ProtocolConformance::getTypeWitnessByName(Type type,
ProtocolConformanceRef conformance,
Identifier name,
LazyResolver *resolver) {
// For an archetype, retrieve the nested type with the appropriate
// name. There are no conformance tables.
if (auto archetype = type->getAs<ArchetypeType>()) {
return archetype->getNestedType(name);
}

// Find the named requirement.
AssociatedTypeDecl *assocType = nullptr;
auto members = conformance.getRequirement()->lookupDirect(name);
for (auto member : members) {
assocType = dyn_cast<AssociatedTypeDecl>(member);
if (assocType)
break;
}

// FIXME: Shouldn't this be a hard error?
if (!assocType)
return nullptr;

if (conformance.isAbstract())
return DependentMemberType::get(type, assocType);

auto concrete = conformance.getConcrete();
if (!concrete->hasTypeWitness(assocType, resolver)) {
return nullptr;
}
return concrete->getTypeWitness(assocType, resolver).getReplacement();
}

Witness ProtocolConformance::getWitness(ValueDecl *requirement,
LazyResolver *resolver) const {
CONFORMANCE_SUBCLASS_DISPATCH(getWitness, (requirement, resolver))
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,

// Dig out the Objective-C type.
auto conformance = conformances.front();
Type objcType = ProtocolConformance::getTypeWitnessByName(
Type objcType = ProtocolConformanceRef::getTypeWitnessByName(
nominal->getDeclaredType(),
ProtocolConformanceRef(conformance),
ctx.Id_ObjectiveCType,
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/Bridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
auto conformance = foreignRepresentation.second;
assert(conformance && "Missing conformance?");
Type bridgedTy =
ProtocolConformance::getTypeWitnessByName(
ProtocolConformanceRef::getTypeWitnessByName(
t, ProtocolConformanceRef(conformance),
M.getASTContext().Id_ObjectiveCType,
nullptr);
Expand Down
43 changes: 22 additions & 21 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3686,10 +3686,11 @@ static Type isRawRepresentable(Type fromType,
if (!conformance)
return Type();

Type rawTy = ProtocolConformance::getTypeWitnessByName(fromType,
*conformance,
CS->getASTContext().Id_RawValue,
&CS->TC);
Type rawTy = ProtocolConformanceRef::getTypeWitnessByName(
fromType,
*conformance,
CS->getASTContext().Id_RawValue,
&CS->TC);
return rawTy;
}

Expand Down Expand Up @@ -4056,9 +4057,9 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
ConformanceCheckFlags::InExpression)) {
Type errorCodeType = expr->getType();
Type errorType =
ProtocolConformance::getTypeWitnessByName(errorCodeType, *conformance,
TC.Context.Id_ErrorType,
&TC)->getCanonicalType();
ProtocolConformanceRef::getTypeWitnessByName(errorCodeType, *conformance,
TC.Context.Id_ErrorType,
&TC)->getCanonicalType();
if (errorType) {
auto diag = diagnose(expr->getLoc(), diag::cannot_throw_error_code,
errorCodeType, errorType);
Expand Down Expand Up @@ -6698,12 +6699,12 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
return true;
}

contextualElementType = ProtocolConformance::getTypeWitnessByName(
contextualType,
*Conformance,
CS->getASTContext().Id_Element,
&CS->TC)
->getDesugaredType();
contextualElementType = ProtocolConformanceRef::getTypeWitnessByName(
contextualType,
*Conformance,
CS->getASTContext().Id_Element,
&CS->TC)
->getDesugaredType();
elementTypePurpose = CTP_ArrayElement;
}

Expand Down Expand Up @@ -6749,17 +6750,17 @@ bool FailureDiagnosis::visitDictionaryExpr(DictionaryExpr *E) {
}

contextualKeyType =
ProtocolConformance::getTypeWitnessByName(contextualType,
*Conformance,
CS->getASTContext().Id_Key,
&CS->TC)
ProtocolConformanceRef::getTypeWitnessByName(contextualType,
*Conformance,
CS->getASTContext().Id_Key,
&CS->TC)
->getDesugaredType();

contextualValueType =
ProtocolConformance::getTypeWitnessByName(contextualType,
*Conformance,
CS->getASTContext().Id_Value,
&CS->TC)
ProtocolConformanceRef::getTypeWitnessByName(contextualType,
*Conformance,
CS->getASTContext().Id_Value,
&CS->TC)
->getDesugaredType();

assert(contextualKeyType && contextualValueType &&
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,8 @@ Type TypeChecker::getWitnessType(Type type, ProtocolDecl *protocol,
ProtocolConformanceRef conformance,
Identifier name,
Diag<> brokenProtocolDiag) {
Type ty = ProtocolConformance::getTypeWitnessByName(type, conformance,
name, this);
Type ty = ProtocolConformanceRef::getTypeWitnessByName(type, conformance,
name, this);
if (!ty &&
!(conformance.isConcrete() && conformance.getConcrete()->isInvalid()))
diagnose(protocol->getLoc(), brokenProtocolDiag);
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5095,7 +5095,7 @@ void TypeChecker::useBridgedNSErrorConformances(DeclContext *dc, Type type) {
// Hack: If we've used a conformance to the _BridgedStoredNSError
// protocol, also use the RawRepresentable and _ErrorCodeProtocol
// conformances on the Code associated type witness.
if (auto codeType = ProtocolConformance::getTypeWitnessByName(
if (auto codeType = ProtocolConformanceRef::getTypeWitnessByName(
type, *conformance, Context.Id_Code, this)) {
(void)conformsToProtocol(codeType, errorCodeProto, dc,
ConformanceCheckFlags::Used);
Expand All @@ -5110,7 +5110,7 @@ void TypeChecker::useBridgedNSErrorConformances(DeclContext *dc, Type type) {
(ConformanceCheckFlags::SuppressDependencyTracking|
ConformanceCheckFlags::Used));
if (conformance && conformance->isConcrete()) {
if (Type errorType = ProtocolConformance::getTypeWitnessByName(
if (Type errorType = ProtocolConformanceRef::getTypeWitnessByName(
type, *conformance, Context.Id_ErrorType, this)) {
(void)conformsToProtocol(errorType, bridgedStoredNSError, dc,
ConformanceCheckFlags::Used);
Expand Down
72 changes: 72 additions & 0 deletions validation-test/compiler_crashers_2_fixed/0072-sr3895-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// RUN: %target-swift-frontend %s -emit-ir

public class TiPresenter<V> {

private var view: V? = nil

public func create() {

}

public func attchView(view: V) {
self.view = view
}

public func detachView() {
self.view = nil
}

public func destroy() {

}
}


public class TiViewController<P: TiPresenter<V>, V>
/*: UiViewController*/ // should extend UiViewController but this is not problem here
{

lazy var presenter: P = {
return self.providePresenter()
}()

public init() {
presenter.create()
}

func providePresenter() -> P {
fatalError("must override")
}

func provideView() -> V {
if (self is V) {
return self as! V
} else {
fatalError("UIViewController doesn't implement TiView interface")
}
}
}


protocol MyView {
func setDataItems(_: [String])
}

class MyPresenter: TiPresenter<MyView> {
}


class MyController: TiViewController<MyPresenter, MyView>, MyView {


override func providePresenter() -> MyPresenter {
return MyPresenter()
}

internal func setDataItems(_: [String]) {
//TODO
}
}

let vc = MyController()
let p = vc.presenter
Loading