Skip to content

[Diagnostics] Cleanup and fix for incorrect 'type(of:)' suggestion on initializer #25903

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
Jul 1, 2019
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
32 changes: 0 additions & 32 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5262,39 +5262,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
}
}

// If we have an argument list (i.e., a scalar, or a non-zero-element tuple)
// then diagnose with some specificity about the arguments.
bool isInitializer = isa<TypeExpr>(fnExpr);
if (!fnType->is<AnyMetatypeType>() &&
((isa<TupleExpr>(argExpr) &&
cast<TupleExpr>(argExpr)->getNumElements() == 1) ||
(isa<ParenExpr>(argExpr) &&
!isa<LoadExpr>(cast<ParenExpr>(argExpr)->getSubExpr())))) {
if (auto ctorRef = dyn_cast<UnresolvedDotExpr>(fnExpr)) {
if (ctorRef->getName().isSimpleName(DeclBaseName::createConstructor())) {
// Diagnose 'super.init', which can only appear inside another
// initializer, specially.
if (isa<SuperRefExpr>(ctorRef->getBase())) {
diagnose(fnExpr->getLoc(),
diag::super_initializer_not_in_initializer);
calleeInfo.suggestPotentialOverloads(fnExpr->getLoc());
return true;
}

// Suggest inserting a call to 'type(of:)' to construct another object
// of the same dynamic type.
SourceRange fixItRng = ctorRef->getNameLoc().getSourceRange();

// Surround the caller in `type(of:)`.
diagnose(fnExpr->getLoc(), diag::init_not_instance_member)
.fixItInsert(fixItRng.Start, "type(of: ")
.fixItInsertAfter(fixItRng.End, ")");
calleeInfo.suggestPotentialOverloads(fnExpr->getLoc());
return true;
}
}
}

if (isa<TupleExpr>(argExpr) &&
cast<TupleExpr>(argExpr)->getNumElements() == 0) {
// Emit diagnostics that say "no arguments".
Expand Down
10 changes: 10 additions & 0 deletions test/expr/postfix/dot/init_ref_delegation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,13 @@ struct MultipleMemberAccesses {
y2.x2.init() // expected-error {{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{5-5=type(of: }} {{10-10=)}}
}
}

func sr10670() {
struct S {
init(_ x: inout String) {}
init(_ x: inout [Int]) {}
}
var a = 0
S.init(&a) // expected-error {{cannot invoke 'S.Type.init' with an argument list of type '(inout Int)'}}
// expected-note@-1 {{overloads for 'S.Type.init' exist with these partially matching parameter lists: (inout String), (inout [Int])}}
}