Skip to content

[3.0] [Import as Member] Error on convenience inits in extensions of CFTypes #4420

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 1 commit into from
Aug 20, 2016
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: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2034,6 +2034,9 @@ ERROR(enumstruct_convenience_init,none,
ERROR(nonclass_convenience_init,none,
"convenience initializer not allowed in non-class type %0",
(Type))
ERROR(cfclass_convenience_init,none,
"convenience initializers are not supported in extensions of CF types",
())

ERROR(dynamic_construct_class,none,
"constructing an object of class type %0 with a metatype value must use "
Expand Down
12 changes: 10 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6337,8 +6337,16 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
// extensions thereof.
if (CD->isConvenienceInit()) {
if (auto extType = CD->getExtensionType()) {
if (!extType->getClassOrBoundGenericClass() &&
!extType->is<ErrorType>()) {
auto extClass = extType->getClassOrBoundGenericClass();

// Forbid convenience inits on Foreign CF types, as Swift does not yet
// support user-defined factory inits.
if (extClass &&
extClass->getForeignClassKind() == ClassDecl::ForeignKind::CFType) {
TC.diagnose(CD->getLoc(), diag::cfclass_convenience_init);
}

if (!extClass && !extType->is<ErrorType>()) {
auto ConvenienceLoc =
CD->getAttrs().getAttribute<ConvenienceAttr>()->getLocation();

Expand Down
23 changes: 23 additions & 0 deletions test/decl/init/cf-types.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-parse-verify-swift
// REQUIRES: OS=macosx

import CoreGraphics

extension CGMutablePath {
public convenience init(p: Bool) { // expected-error{{convenience initializers are not supported in extensions of CF types}}
self.init()
}
public convenience init?(maybe: Bool) { // expected-error{{convenience initializers are not supported in extensions of CF types}}
self.init()
}

public convenience init(toss: Bool) throws { // expected-error{{convenience initializers are not supported in extensions of CF types}}
self.init()
}
}

public func useInit() {
let _ = CGMutablePath(p: true)
let _ = CGMutablePath(maybe: true)
let _ = try! CGMutablePath(toss: true)
}