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
20 changes: 14 additions & 6 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,19 @@ void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
}

static InFlightDiagnostic
diagnoseTypoCorrection(TypeChecker &tc, DeclNameLoc loc, ValueDecl *decl) {
diagnoseTypoCorrection(TypeChecker &tc, DeclNameLoc loc, ValueDecl *decl,
FunctionRefKind refKind) {

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

Choose a reason for hiding this comment

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

Here you must add the quotes back or it'll fall any existing typo correction tests that rely on it.


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 +585,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
6 changes: 6 additions & 0 deletions test/Sema/typo_correction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ _ = [Any]().withUnsafeBufferPointer { (buf) -> [Any] in
guard let base = buf.baseAddress else { return [] }
return (base ..< base + buf.count).m // expected-error {{value of type 'CountableRange<_>' has no member 'm'}}
}


// SR-3254: Test for correct diagnostics for compound references
foo(x: Int) {} // expected-note {{did you mean 'foo(x:)'?}}
let f = foo(_:) // expected-error {{use of unresolved identifier 'foo'}}