Skip to content

[DiagnosticsQoI] Inform the User When Unavailable Inits Are Called #36824

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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5291,6 +5291,10 @@ ERROR(availabilty_string_subscript_migration, none,
"subscripts returning String were obsoleted in Swift 4; explicitly "
"construct a String from subscripted result", ())

NOTE(availability_unavailable_implicit_init, none,
"call to unavailable %0 %1 from superclass %2 occurs implicitly at the "
"end of this initializer", (DescriptiveDeclKind, DeclName, DeclName))

// Conformance availability checking diagnostics

ERROR(conformance_availability_unavailable, none,
Expand Down
9 changes: 7 additions & 2 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ static bool checkSuperInit(ConstructorDecl *fromCtor,

for (auto decl : lookupResults) {
auto superclassCtor = dyn_cast<ConstructorDecl>(decl);
if (!superclassCtor || !superclassCtor->isDesignatedInit() ||
if (!superclassCtor || !superclassCtor->isDesignatedInit() ||
superclassCtor == ctor)
continue;

Expand All @@ -1673,9 +1673,14 @@ static bool checkSuperInit(ConstructorDecl *fromCtor,

// Make sure we can reference the designated initializer correctly.
auto loc = fromCtor->getLoc();
diagnoseDeclAvailability(
const bool didDiagnose = diagnoseDeclAvailability(
ctor, loc, nullptr,
ExportContext::forFunctionBody(fromCtor, loc));
if (didDiagnose) {
fromCtor->diagnose(diag::availability_unavailable_implicit_init,
ctor->getDescriptiveKind(), ctor->getName(),
superclassDecl->getName());
}
}


Expand Down
11 changes: 11 additions & 0 deletions test/attr/attr_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,14 @@ func testBadRename() {

struct AvailableGenericParam<@available(*, deprecated) T> {}
// expected-error@-1 {{'@available' attribute cannot be applied to this declaration}}

class UnavailableNoArgsSuperclassInit {
@available(*, unavailable)
init() {} // expected-note {{'init()' has been explicitly marked unavailable here}}
}

class UnavailableNoArgsSubclassInit: UnavailableNoArgsSuperclassInit {
init(marker: ()) {}
// expected-error@-1 {{'init()' is unavailable}}
// expected-note@-2 {{call to unavailable initializer 'init()' from superclass 'UnavailableNoArgsSuperclassInit' occurs implicitly at the end of this initializer}}
}
2 changes: 2 additions & 0 deletions test/attr/attr_inlinable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public class Derived3 : Base3 {
@inlinable
public init(_: Int) {}
// expected-error@-1 {{initializer 'init()' is internal and cannot be referenced from an '@inlinable' function}}
// expected-note@-2 {{call to unavailable initializer 'init()' from superclass 'Base3' occurs implicitly at the end of this initializer}}
}

@_fixed_layout
Expand All @@ -245,6 +246,7 @@ class Derived4 : Middle4 {
@inlinable
public init(_: Int) {}
// expected-error@-1 {{initializer 'init()' is internal and cannot be referenced from an '@inlinable' function}}
// expected-note@-2 {{call to unavailable initializer 'init()' from superclass 'Middle4' occurs implicitly at the end of this initializer}}
}


Expand Down