Skip to content

Commit fbb55ce

Browse files
committed
[Diagnostics] Add a tailored diagnostic for no accessible initializers
1 parent fa9c3f3 commit fbb55ce

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,6 +2899,27 @@ bool MissingMemberFailure::diagnoseAsError() {
28992899
diagnostic.highlight(baseExpr->getSourceRange())
29002900
.highlight(nameLoc.getSourceRange());
29012901
correction->addFixits(diagnostic);
2902+
} else if (instanceTy->getAnyNominal() &&
2903+
getName().getBaseName() == DeclBaseName::createConstructor()) {
2904+
auto &cs = getConstraintSystem();
2905+
2906+
auto memberName = getName().getBaseName();
2907+
auto result = cs.performMemberLookup(
2908+
ConstraintKind::ValueMember, memberName, metatypeTy,
2909+
FunctionRefKind::DoubleApply, getLocator(),
2910+
/*includeInaccessibleMembers=*/true);
2911+
2912+
// If there are no `init` members at all produce a tailored
2913+
// diagnostic for that, otherwise fallback to generic
2914+
// "no such member" one.
2915+
if (result.ViableCandidates.empty() &&
2916+
result.UnviableCandidates.empty()) {
2917+
emitDiagnostic(anchor->getLoc(), diag::no_accessible_initializers,
2918+
instanceTy)
2919+
.highlight(baseExpr->getSourceRange());
2920+
} else {
2921+
emitBasicError(baseType);
2922+
}
29022923
} else {
29032924
emitBasicError(baseType);
29042925
}

test/decl/class/circular_inheritance.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Outer3 // expected-error {{circular reference}}
5959
protocol Initable {
6060
init()
6161
// expected-note@-1 {{protocol requires initializer 'init()' with type '()'; do you want to add a stub?}}
62+
// expected-note@-2 {{did you mean 'init'?}}
6263
}
6364

6465
protocol Shape : Circle {}

test/decl/protocol/special/coding/class_codable_default_initializer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class NoInitializers { // expected-error {{class 'NoInitializers' has no initial
77

88
func foo() {
99
// The class should not receive a default constructor.
10-
let _ = NoInitializers.init() // expected-error {{type 'NoInitializers' has no member 'init'}}
10+
let _ = NoInitializers.init() // expected-error {{'NoInitializers' cannot be constructed because it has no accessible initializers}}
1111
}
1212
}
1313

0 commit comments

Comments
 (0)