Allow construction of archetype values via archetype metatypes.

Swift SVN r14325
This commit is contained in:
Doug Gregor
2014-02-25 00:11:24 +00:00
parent c4cc906210
commit 6a532e05c6
4 changed files with 22 additions and 20 deletions

View File

@@ -1236,7 +1236,7 @@ struct ASTNodeBase {};
void verifyChecked(ConstructorDecl *CD) { void verifyChecked(ConstructorDecl *CD) {
auto *ND = CD->getExtensionType()->getNominalOrBoundGenericNominal(); auto *ND = CD->getExtensionType()->getNominalOrBoundGenericNominal();
if (!isa<ClassDecl>(ND) && !isa<StructDecl>(ND) && !isa<EnumDecl>(ND) && if (!isa<ClassDecl>(ND) && !isa<StructDecl>(ND) && !isa<EnumDecl>(ND) &&
!CD->isInvalid()) { !isa<ProtocolDecl>(ND) && !CD->isInvalid()) {
Out << "ConstructorDecls outside structs, classes or enums " Out << "ConstructorDecls outside structs, classes or enums "
"should be marked invalid"; "should be marked invalid";
abort(); abort();

View File

@@ -3415,7 +3415,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
// We're constructing a value of nominal type. Look for the constructor or // We're constructing a value of nominal type. Look for the constructor or
// enum element to use. // enum element to use.
assert(ty->getNominalOrBoundGenericNominal() || ty->is<DynamicSelfType>()); assert(ty->getNominalOrBoundGenericNominal() || ty->is<DynamicSelfType>() ||
ty->is<ArchetypeType>());
auto selected = getOverloadChoiceIfAvailable( auto selected = getOverloadChoiceIfAvailable(
cs.getConstraintLocator( cs.getConstraintLocator(
locator.withPathElement( locator.withPathElement(

View File

@@ -941,8 +941,11 @@ ConstraintSystem::getTypeOfMemberReference(Type baseTy, ValueDecl *value,
} }
} }
// Alternatively, if this is a constructor referenced from a DynamicSelf base // Alternatively, if this is a constructor referenced from a DynamicSelf base
// object, replace the result type with DynamicSelf. // object, or a constructor within a protocol, replace the result type with
else if (baseObjTy->is<DynamicSelfType>() && isa<ConstructorDecl>(value)) { // the base object type DynamicSelf.
else if (isa<ConstructorDecl>(value) &&
(baseObjTy->is<DynamicSelfType>() ||
isa<ProtocolDecl>(value->getDeclContext()))) {
auto outerFnType = openedType->castTo<FunctionType>(); auto outerFnType = openedType->castTo<FunctionType>();
auto innerFnType = outerFnType->getResult()->castTo<FunctionType>(); auto innerFnType = outerFnType->getResult()->castTo<FunctionType>();

View File

@@ -52,11 +52,8 @@ LookupResult TypeChecker::lookupMember(Type type, Identifier name,
if (auto metaTy = type->getAs<MetatypeType>()) if (auto metaTy = type->getAs<MetatypeType>())
type = metaTy->getInstanceType(); type = metaTy->getInstanceType();
// We only have constructors for nominal declarations. // For nominal types, make sure we have the right constructors available.
auto nominalDecl = type->getAnyNominal(); if (auto nominalDecl = type->getAnyNominal()) {
if (!nominalDecl)
return result;
// Define implicit default constructor for a struct/class. // Define implicit default constructor for a struct/class.
if (isa<StructDecl>(nominalDecl) || isa<ClassDecl>(nominalDecl)) if (isa<StructDecl>(nominalDecl) || isa<ClassDecl>(nominalDecl))
addImplicitConstructors(nominalDecl); addImplicitConstructors(nominalDecl);
@@ -70,6 +67,7 @@ LookupResult TypeChecker::lookupMember(Type type, Identifier name,
result.addResult(element); result.addResult(element);
} }
} }
}
// Fall through to look for constructors via the normal means. // Fall through to look for constructors via the normal means.
options = NL_Constructor; options = NL_Constructor;