Skip to content

Tighten up unavailable witness error and add a command-line option limiting typo correction #10846

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 3 commits into from
Jul 10, 2017
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
5 changes: 4 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,10 @@ NOTE(declared_protocol_conformance_here,none,
"%0 implicitly conforms to protocol %2}1 here",
(Type, unsigned, DeclName, DeclName))

WARNING(witness_unavailable,none,
WARNING(witness_unavailable_warn,none,
"unavailable %0 %1 was used to satisfy a requirement of protocol %2",
(DescriptiveDeclKind, DeclName, DeclName))
ERROR(witness_unavailable,none,
"unavailable %0 %1 was used to satisfy a requirement of protocol %2",
(DescriptiveDeclKind, DeclName, DeclName))

Expand Down
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 @@ -564,12 +564,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
13 changes: 9 additions & 4 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3003,11 +3003,15 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {

break;

case CheckKind::WitnessUnavailable:
diagnoseOrDefer(requirement, /*isError=*/false,
[witness, requirement](NormalProtocolConformance *conformance) {
case CheckKind::WitnessUnavailable: {
bool emitError = !witness->getASTContext().LangOpts.isSwiftVersion3();
diagnoseOrDefer(requirement, /*isError=*/emitError,
[witness, requirement, emitError](
NormalProtocolConformance *conformance) {
auto &diags = witness->getASTContext().Diags;
diags.diagnose(witness, diag::witness_unavailable,
diags.diagnose(witness,
emitError ? diag::witness_unavailable
: diag::witness_unavailable_warn,
witness->getDescriptiveKind(),
witness->getFullName(),
conformance->getProtocol()->getFullName());
Expand All @@ -3016,6 +3020,7 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
});
break;
}
}

ClassDecl *classDecl = Adoptee->getClassOrBoundGenericClass();

Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,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
14 changes: 14 additions & 0 deletions test/Compatibility/unavailable_witness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-typecheck-verify-swift -swift-version 3

class Foo { }

// Complain about unavailable witnesses (error in Swift 4, warning in Swift 3)
protocol P {
func foo(bar: Foo) // expected-note{{requirement 'foo(bar:)' declared here}}
}

struct ConformsToP : P {
@available(*, unavailable)
func foo(bar: Foo) { } // expected-warning{{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
}

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}}
}
8 changes: 4 additions & 4 deletions test/decl/protocol/req/unavailable.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 -swift-version 4

// An @objc protocol can have 'unavailable'
// methods. They are treated as if they
Expand All @@ -24,12 +24,12 @@ class Bar : NonObjCProto { // expected-error {{type 'Bar' does not conform to pr
}


// Warn about unavailable witnesses.
// Complain about unavailable witnesses (error in Swift 4, warning in Swift 3)
protocol P {
func foo(bar: Foo) // expected-note{{requirement 'foo(bar:)' declared here}}
}

struct ConformsToP : P {
struct ConformsToP : P { // expected-error{{type 'ConformsToP' does not conform to protocol 'P'}}
@available(*, unavailable)
func foo(bar: Foo) { } // expected-warning{{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
func foo(bar: Foo) { } // expected-error{{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
}
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