Skip to content
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
4 changes: 2 additions & 2 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ namespace swift {
/// \brief Disable API availability checking.
bool DisableAvailabilityChecking = false;

/// \brief Disable typo correction.
bool DisableTypoCorrection = false;
/// \brief Maximum number of typo corrections we are allowed to perform.
unsigned TypoCorrectionLimit = 10;

/// Should access control be respected?
bool EnableAccessControl = true;
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ class CompilerInvocation {
CodeCompletionBuffer = Buf;
CodeCompletionOffset = Offset;
// We don't need typo-correction for code-completion.
LangOpts.DisableTypoCorrection = true;
// FIXME: This isn't really true, but is a performance issue.
LangOpts.TypoCorrectionLimit = 0;
}

std::pair<llvm::MemoryBuffer *, unsigned> getCodeCompletionPoint() const {
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ def warn_swift3_objc_inference_minimal :
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Warn about deprecated @objc inference in Swift 3 based on direct uses of the Objective-C entrypoint">;

def typo_correction_limit : Separate<["-"], "typo-correction-limit">,
Flags<[FrontendOption, HelpHidden]>,
MetaVarName<"<n>">,
HelpText<"Limit the number of times the compiler will attempt typo correction to <n>">;

def warn_swift3_objc_inference : Flag<["-"], "warn-swift3-objc-inference">,
Alias<warn_swift3_objc_inference_complete>,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, HelpHidden]>;
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ static void addCommonFrontendArgs(const ToolChain &TC,
inputArgs.AddLastArg(arguments,
options::OPT_warn_swift3_objc_inference_minimal,
options::OPT_warn_swift3_objc_inference_complete);
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);
inputArgs.AddLastArg(arguments, options::OPT_enable_testing);
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
Expand Down
16 changes: 15 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,21 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
= A->getOption().matches(OPT_enable_access_control);
}

Opts.DisableTypoCorrection |= Args.hasArg(OPT_disable_typo_correction);
if (auto A = Args.getLastArg(OPT_disable_typo_correction,
OPT_typo_correction_limit)) {
if (A->getOption().matches(OPT_disable_typo_correction))
Opts.TypoCorrectionLimit = 0;
else {
unsigned limit;
if (StringRef(A->getValue()).getAsInteger(10, limit)) {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
return true;
}

Opts.TypoCorrectionLimit = limit;
}
}

Opts.CodeCompleteInitsInPostfixExpr |=
Args.hasArg(OPT_code_complete_inits_in_postfix_expr);
Expand Down
7 changes: 5 additions & 2 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,15 @@ void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
LookupResult &result,
GenericSignatureBuilder *gsb,
unsigned maxResults) {
// Disable typo-correction if we won't show the diagnostic anyway.
if (getLangOpts().DisableTypoCorrection ||
// Disable typo-correction if we won't show the diagnostic anyway or if
// we've hit our typo correction limit.
if (NumTypoCorrections >= getLangOpts().TypoCorrectionLimit ||
(Diags.hasFatalErrorOccurred() &&
!Diags.getShowDiagnosticsAfterFatalError()))
return;

++NumTypoCorrections;

// Fill in a collection of the most reasonable entries.
TopCollection<unsigned, ValueDecl*> entries(maxResults);
auto consumer = makeDeclConsumer([&](ValueDecl *decl,
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,9 @@ class TypeChecker final : public LazyResolver {
llvm::DenseMap<AbstractFunctionDecl *, llvm::DenseSet<ApplyExpr *>>
FunctionAsEscapingArg;

/// The # of times we have performed typo correction.
unsigned NumTypoCorrections = 0;

public:
/// Record an occurrence of a function that captures inout values as an
/// argument.
Expand Down
2 changes: 1 addition & 1 deletion test/NameBinding/name_lookup.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -typo-correction-limit 100

class ThisBase1 {
init() { }
Expand Down
3 changes: 2 additions & 1 deletion test/Sema/typo_correction.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -typo-correction-limit 20
// RUN: not %target-swift-frontend -typecheck -disable-typo-correction %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
// RUN: not %target-swift-frontend -typecheck -typo-correction-limit 0 %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
// RUN: not %target-swift-frontend -typecheck -DIMPORT_FAIL %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
// DISABLED-NOT: did you mean

Expand Down
12 changes: 12 additions & 0 deletions test/Sema/typo_correction_limit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-typecheck-verify-swift -typo-correction-limit 5

// This is close enough to get typo-correction.
func test_short_and_close() {
let foo = 4 // expected-note 5 {{did you mean 'foo'?}}
let _ = fob + 1 // expected-error {{use of unresolved identifier}}
let _ = fob + 1 // expected-error {{use of unresolved identifier}}
let _ = fob + 1 // expected-error {{use of unresolved identifier}}
let _ = fob + 1 // expected-error {{use of unresolved identifier}}
let _ = fob + 1 // expected-error {{use of unresolved identifier}}
let _ = fob + 1 // expected-error {{use of unresolved identifier}}
}
2 changes: 1 addition & 1 deletion tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,7 @@ static int doPrintIndexedSymbols(const CompilerInvocation &InitInvok,
CompilerInvocation Invocation(InitInvok);
Invocation.addInputFilename(SourceFileName);
Invocation.getLangOptions().DisableAvailabilityChecking = false;
Invocation.getLangOptions().DisableTypoCorrection = true;
Invocation.getLangOptions().TypoCorrectionLimit = 0;

CompilerInstance CI;

Expand Down