Skip to content

Fix crash-on-invalid when calling non-required init on a metatype #16901

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
19 changes: 14 additions & 5 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,15 +639,24 @@ bool Expr::isTypeReference(

bool Expr::isStaticallyDerivedMetatype(
llvm::function_ref<Type(const Expr *)> getType) const {
// The type must first be a type reference.
// The expression must first be a type reference.
if (!isTypeReference(getType))
return false;

auto type = getType(this)
->castTo<AnyMetatypeType>()
->getInstanceType();

// Archetypes are never statically derived.
return !getType(this)
->getAs<AnyMetatypeType>()
->getInstanceType()
->is<ArchetypeType>();
if (type->is<ArchetypeType>())
return false;

// Dynamic Self is never statically derived.
if (type->is<DynamicSelfType>())
return false;

// Everything else is statically derived.
return true;
}

bool Expr::isSuperExpr() const {
Expand Down
27 changes: 11 additions & 16 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,6 @@ diagnoseInvalidDynamicConstructorReferences(ConstraintSystem &cs,
return cs.getType(expr);
});

// 'super.' is always OK
if (isa<SuperRefExpr>(base))
return true;

// 'self.' reference with concrete type is OK
if (isa<DeclRefExpr>(base) &&
cast<DeclRefExpr>(base)->getDecl()->getBaseName() == tc.Context.Id_self &&
!baseTy->is<ArchetypeType>())
return true;

// FIXME: The "hasClangNode" check here is a complete hack.
if (isNonFinalClass(instanceTy) &&
!isStaticallyDerived &&
Expand Down Expand Up @@ -2697,12 +2687,6 @@ namespace {

auto *ctor = cast<ConstructorDecl>(choice.getDecl());

// If the member is a constructor, verify that it can be legally
// referenced from this base.
if (!diagnoseInvalidDynamicConstructorReferences(cs, base, nameLoc,
ctor, SuppressDiagnostics))
return nullptr;

// If the subexpression is a metatype, build a direct reference to the
// constructor.
if (cs.getType(base)->is<AnyMetatypeType>()) {
Expand All @@ -2725,6 +2709,17 @@ namespace {
// We have a reference to 'self'.
diagnoseBadInitRef = false;

// Special case -- in a protocol extension initializer with a class
// constrainted Self type, 'self' has archetype type, and only
// required initializers can be called.
if (cs.getType(dre)->getRValueType()->is<ArchetypeType>()) {
if (!diagnoseInvalidDynamicConstructorReferences(cs, base,
nameLoc,
ctor,
SuppressDiagnostics))
return nullptr;
}

// Make sure the reference to 'self' occurs within an initializer.
if (!dyn_cast_or_null<ConstructorDecl>(
cs.DC->getInnermostMethodContext())) {
Expand Down
29 changes: 28 additions & 1 deletion test/expr/postfix/call/construction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,40 @@ enum E {
}

class C {
init(i: Int) { } // expected-note{{selected non-required initializer 'init(i:)'}}
init(i: Int) { } // expected-note 4{{selected non-required initializer 'init(i:)'}}

required init(d: Double) { }

class Inner {
init(i: Int) { }
}

static func makeCBad() -> C {
return self.init(i: 0)
// expected-error@-1 {{constructing an object of class type 'C' with a metatype value must use a 'required' initializer}}
}

static func makeCGood() -> C {
return self.init(d: 0)
}

static func makeSelfBad() -> Self {
return self.init(i: 0)
// expected-error@-1 {{constructing an object of class type 'Self' with a metatype value must use a 'required' initializer}}
}

static func makeSelfGood() -> Self {
return self.init(d: 0)
}

static func makeSelfImplicitBaseBad() -> Self {
return .init(i: 0)
// expected-error@-1 {{constructing an object of class type 'Self' with a metatype value must use a 'required' initializer}}
}

static func makeSelfImplicitBaseGood() -> Self {
return .init(d: 0)
}
}

final class D {
Expand Down