Skip to content

[FixCode] Help users construct mutable pointer from a pointer #3971

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 3, 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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ ERROR(cannot_call_with_params, none,
"cannot invoke %select{|initializer for type }2'%0' with an argument list"
" of type '%1'", (StringRef, StringRef, bool))

NOTE(pointer_init_add_mutating,none,
"do you want to add 'mutating'", ())
ERROR(expected_do_in_statement,none,
"expected 'do' keyword to designate a block of statements", ())

Expand Down
25 changes: 25 additions & 0 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4824,6 +4824,25 @@ bool FailureDiagnosis::diagnoseNilLiteralComparison(
return true;
}

static bool shouldAddMutating(ASTContext &Ctx, const Expr *Fn,
const Expr* Arg) {
auto *TypeExp = dyn_cast<TypeExpr>(Fn);
auto *ParenExp = dyn_cast<ParenExpr>(Arg);
if (!TypeExp || !ParenExp)
return false;
auto InitType = TypeExp->getInstanceType();
auto ArgType = ParenExp->getSubExpr()->getType();
if (InitType.isNull() || ArgType.isNull())
return false;
if (auto *InitNom = InitType->getAnyNominal()) {
if (auto *ArgNom = ArgType->getAnyNominal()) {
return InitNom == Ctx.getUnsafeMutablePointerDecl() &&
ArgNom == Ctx.getUnsafePointerDecl();
}
}
return false;
}

bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
// Type check the function subexpression to resolve a type for it if possible.
auto fnExpr = typeCheckChildIndependently(callExpr->getFn());
Expand Down Expand Up @@ -5138,6 +5157,12 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
diagnose(fnExpr->getLoc(), diag::cannot_call_with_params,
overloadName, argString, isInitializer);
}


if (shouldAddMutating(CS->DC->getASTContext(), fnExpr, argExpr)) {
diagnose(fnExpr->getLoc(), diag::pointer_init_add_mutating).fixItInsert(
dyn_cast<ParenExpr>(argExpr)->getSubExpr()->getStartLoc(), "mutating: ");
}

// Did the user intend on invoking a different overload?
calleeInfo.suggestPotentialOverloads(fnExpr->getLoc());
Expand Down
5 changes: 5 additions & 0 deletions test/Sema/diag_init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: %target-parse-verify-swift

func foo(a : UnsafePointer<Void>)->UnsafeMutablePointer<Void> {
return UnsafeMutablePointer(a) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<_>' with an argument list of type '(UnsafePointer<Void>)'}} // expected-note {{do you want to add 'mutating'}}{{31-31=mutating: }} // expected-note {{overloads for 'UnsafeMutablePointer<_>' exist with these partially matching parameter lists: (RawPointer), (OpaquePointer), (OpaquePointer?), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
}