Skip to content

Provide a custom diagnostic for conformance to NSObjectProtocol #28591

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 2 commits into from
Dec 5, 2019
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: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,10 @@ ERROR(type_does_not_conform,none,
ERROR(cannot_use_nil_with_this_type,none,
"'nil' cannot be used in context expecting type %0", (Type))

ERROR(type_cannot_conform_to_nsobject,none,
"cannot declare conformance to 'NSObjectProtocol' in Swift; %0 should "
"inherit 'NSObject' instead", (Type))

ERROR(use_of_equal_instead_of_equality,none,
"use of '=' in a boolean context, did you mean '=='?", ())

Expand Down
54 changes: 54 additions & 0 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,14 @@ void ConformanceChecker::recordTypeWitness(AssociatedTypeDecl *assocType,
}
}

/// Whether this protocol is the Objective-C "NSObject" protocol.
static bool isNSObjectProtocol(ProtocolDecl *proto) {
if (proto->getNameStr() != "NSObjectProtocol")
return false;

return proto->hasClangNode();
}

bool swift::
printRequirementStub(ValueDecl *Requirement, DeclContext *Adopter,
Type AdopterTy, SourceLoc TypeLoc, raw_ostream &OS) {
Expand Down Expand Up @@ -2707,6 +2715,7 @@ diagnoseMissingWitnesses(MissingWitnessDiagnosisKind Kind) {
// If this conformance has nothing to complain, return.
if (LocalMissing.empty())
return;

SourceLoc ComplainLoc = Loc;
bool EditorMode = getASTContext().LangOpts.DiagnosticsEditorMode;
llvm::SetVector<ValueDecl*> MissingWitnesses(GlobalMissingWitnesses.begin(),
Expand Down Expand Up @@ -2748,6 +2757,11 @@ diagnoseMissingWitnesses(MissingWitnessDiagnosisKind Kind) {
auto &SM = DC->getASTContext().SourceMgr;
auto FixitBufferId = SM.findBufferContainingLoc(FixitLocation);
for (auto VD : MissingWitnesses) {
// Don't ever emit a diagnostic for a requirement in the NSObject
// protocol. They're not implementable.
if (isNSObjectProtocol(VD->getDeclContext()->getSelfProtocolDecl()))
continue;

// Whether this VD has a stub printed.
bool AddFixit = !NoStubRequirements.count(VD);
bool SameFile = VD->getLoc().isValid() ?
Expand All @@ -2773,6 +2787,7 @@ diagnoseMissingWitnesses(MissingWitnessDiagnosisKind Kind) {
}
continue;
}

// Issue diagnostics for witness values.
Type RequirementType =
getRequirementTypeForDisplay(DC->getParentModule(), Conf, VD);
Expand Down Expand Up @@ -4052,6 +4067,45 @@ static void diagnoseConformanceFailure(Type T,
}
}

// One cannot meaningfully declare conformance to the NSObject protocol
// in Swift. Suggest inheritance from NSObject instead.
if (isNSObjectProtocol(Proto)) {
if (T->getClassOrBoundGenericClass()) {
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 not familiar with the surrounding code—does this call return the same class you dyn_cast<ClassDecl>() to get later?

auto diag =
diags.diagnose(ComplainLoc, diag::type_cannot_conform_to_nsobject,
T);

// Try to suggest inheriting from NSObject instead.
auto classDecl = dyn_cast<ClassDecl>(DC);
if (!classDecl)
return;

auto inheritedClause = classDecl->getInherited();
for (unsigned i : indices(inheritedClause)) {
auto &inherited = inheritedClause[i];

// Find the inherited type.
InheritedTypeRequest request{classDecl, i, TypeResolutionStage::Interface};
Type inheritedTy = evaluateOrDefault(ctx.evaluator, request, Type());

// If it's a class, we cannot suggest a different class to inherit
// from.
if (inheritedTy->getClassOrBoundGenericClass())
return;

// Is it the NSObject protocol?
if (auto protoTy = inheritedTy->getAs<ProtocolType>()) {
if (isNSObjectProtocol(protoTy->getDecl())) {
diag.fixItReplace(inherited.getSourceRange(), "NSObject");
return;
}
}
}

return;
}
}

diags.diagnose(ComplainLoc, diag::type_does_not_conform,
T, Proto->getDeclaredType());
}
Expand Down
19 changes: 19 additions & 0 deletions test/decl/protocol/conforms/nsobject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -parse-as-library -swift-version 4 %s -verify

// REQUIRES: objc_interop

import Foundation

class A: NSObjectProtocol { } // expected-error{{cannot declare conformance to 'NSObjectProtocol' in Swift; 'A' should inherit 'NSObject' instead}}{{10-26=NSObject}}

@objc protocol Other: NSObjectProtocol { }

class B: Other { } // expected-error{{cannot declare conformance to 'NSObjectProtocol' in Swift; 'B' should inherit 'NSObject' instead}}

class C { }

class D: C, NSObjectProtocol { } // expected-error{{cannot declare conformance to 'NSObjectProtocol' in Swift; 'D' should inherit 'NSObject' instead}}

class E { }

extension E: NSObjectProtocol { } // expected-error{{cannot declare conformance to 'NSObjectProtocol' in Swift; 'E' should inherit 'NSObject' instead}}