Skip to content
Closed
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ ERROR(use_undeclared_type,none,
ERROR(use_undeclared_type_did_you_mean,none,
"use of undeclared type %0; did you mean to use '%1'?", (Identifier, StringRef))
NOTE(note_typo_candidate,none,
"did you mean '%0'?", (StringRef))
"did you mean %0?", (DeclName))
NOTE(note_typo_candidate_implicit_member,none,
"did you mean the implicitly-synthesized %1 '%0'?", (StringRef, StringRef))
NOTE(note_remapped_type,none,
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {

// Note all the correction candidates.
for (auto &result : Lookup) {
noteTypoCorrection(Name, nameLoc, result);
noteTypoCorrection(Name, nameLoc, result, UDRE->getFunctionRefKind());
}

// TODO: consider recovering from here. We may want some way to suppress
Expand Down
22 changes: 16 additions & 6 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,21 @@ void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
}

static InFlightDiagnostic
diagnoseTypoCorrection(TypeChecker &tc, DeclNameLoc loc, ValueDecl *decl) {
diagnoseTypoCorrection(TypeChecker &tc,
DeclNameLoc loc,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't necessarily have to fold the entire signature over, just those that go over the line-limit. If it makes it easier, you can run clang-format with the default LLVM style to have it wrap these for you.

ValueDecl *decl,
FunctionRefKind refKind) {

auto name = refKind == FunctionRefKind::Compound ? decl->getFullName() :
Copy link
Contributor

@CodaFi CodaFi Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVM style moves the colon to a new line in-line with the else condition and aligns it with the question mark.

auto name = refKind == FunctionRefKind::Compound ? decl->getFullName() : decl->getName();
decl->getName();

if (auto var = dyn_cast<VarDecl>(decl)) {
// Suggest 'self' at the use point instead of pointing at the start
// of the function.

if (var->isSelfParameter())
return tc.diagnose(loc.getBaseNameLoc(), diag::note_typo_candidate,
decl->getName().str());
name);
}

if (!decl->getLoc().isValid() && decl->getDeclContext()->isTypeContext()) {
Expand All @@ -579,13 +587,15 @@ diagnoseTypoCorrection(TypeChecker &tc, DeclNameLoc loc, ValueDecl *decl) {
}
}

return tc.diagnose(decl, diag::note_typo_candidate, decl->getName().str());
return tc.diagnose(decl, diag::note_typo_candidate, name);
}

void TypeChecker::noteTypoCorrection(DeclName writtenName, DeclNameLoc loc,
const LookupResult::Result &suggestion) {
void TypeChecker::noteTypoCorrection(DeclName writtenName,
DeclNameLoc loc,
const LookupResult::Result &suggestion,
FunctionRefKind refKind) {
auto decl = suggestion.Decl;
auto &&diagnostic = diagnoseTypoCorrection(*this, loc, decl);
auto &&diagnostic = diagnoseTypoCorrection(*this, loc, decl, refKind);

DeclName declName = decl->getFullName();

Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,8 @@ class TypeChecker final : public LazyResolver {
unsigned maxResults = 4);

void noteTypoCorrection(DeclName name, DeclNameLoc nameLoc,
const LookupResult::Result &suggestion);
const LookupResult::Result &suggestion,
FunctionRefKind refKind = FunctionRefKind::Unapplied);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not 100% sure this is the right type for this. For names that aren't function references that go through typo correction this is kind of nonsensical.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other type that would work in this case?


/// Check if the given decl has a @_semantics attribute that gives it
/// special case type-checking behavior.
Expand Down
4 changes: 4 additions & 0 deletions test/Sema/compound_references.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// RUN: %target-typecheck-verify-swift

func foo(x: Int) {} // expected-note * {{did you mean 'foo(x:)'?}}
let f = foo(_:) // expected-error {{use of unresolved identifier 'foo'}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this file and add these tests to Sema/typo_correction.swift (sans * on the expected-note).