Skip to content

Sema: Fix order dependency in @objc inference from witnessed protocol requirement [5.1] #24256

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
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
13 changes: 8 additions & 5 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ static ValueDecl *getStandinForAccessor(AbstractStorageDecl *witnessStorage,

RequirementMatch
swift::matchWitness(
TypeChecker &tc,
DeclContext *dc, ValueDecl *req, ValueDecl *witness,
llvm::function_ref<
std::tuple<Optional<RequirementMatch>, Type, Type>(void)>
Expand All @@ -440,10 +439,17 @@ swift::matchWitness(
if (req->getKind() != witness->getKind())
return RequirementMatch(witness, MatchKind::KindConflict);

// If the witness has not been validated yet, do so now.
if (!witness->hasValidSignature()) {
auto &ctx = dc->getASTContext();
ctx.getLazyResolver()->resolveDeclSignature(witness);
}

// If the witness is invalid, record that and stop now.
if (witness->isInvalid())
return RequirementMatch(witness, MatchKind::WitnessInvalid);

// If we're currently validating the witness, bail out.
if (!witness->hasValidSignature())
return RequirementMatch(witness, MatchKind::Circularity);

Expand Down Expand Up @@ -946,7 +952,7 @@ swift::matchWitness(TypeChecker &tc,
return result;
};

return matchWitness(tc, dc, req, witness, setup, matchTypes, finalize);
return matchWitness(dc, req, witness, setup, matchTypes, finalize);
}

static bool
Expand Down Expand Up @@ -1149,9 +1155,6 @@ bool WitnessChecker::findBestWitness(
continue;
}

if (!witness->hasValidSignature())
TC.validateDecl(witness);

auto match = matchWitness(TC, ReqEnvironmentCache, Proto, conformance, DC,
requirement, witness);
if (match.isViable()) {
Expand Down
1 change: 0 additions & 1 deletion lib/Sema/TypeCheckProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,6 @@ class AssociatedTypeInference {
///
/// \returns the result of performing the match.
RequirementMatch matchWitness(
TypeChecker &tc,
DeclContext *dc, ValueDecl *req, ValueDecl *witness,
llvm::function_ref<
std::tuple<Optional<RequirementMatch>, Type, Type>(void)>
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocolInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ AssociatedTypeInference::inferTypeWitnessesViaValueWitness(ValueDecl *req,
// Match the witness. If we don't succeed, throw away the inference
// information.
// FIXME: A renamed match might be useful to retain for the failure case.
if (matchWitness(tc, dc, req, witness, setup, matchTypes, finalize)
if (matchWitness(dc, req, witness, setup, matchTypes, finalize)
.Kind != MatchKind::ExactMatch) {
inferred.Inferred.clear();
}
Expand Down
31 changes: 31 additions & 0 deletions test/decl/protocol/conforms/objc_from_witness_corner_case.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -print-ast %s | %FileCheck %s

// REQUIRES: objc_interop

// This bug required an elaborate setup where isObjC() was checked prior
// to validateDecl() getting called on a declaration. In this case, we
// did not infer @objc from witnessed protocol requirements as required.
//
// https://bugs.swift.org/browse/SR-10257

@objc public protocol P {
@objc optional func f()
}

public class Other {
// This triggers a walk over all nominals in the file, collecting
// @objc members into the dynamic dispatch lookup table.
let a = (Base() as AnyObject).g()
}

@objc public class Base : P {
@objc public func g() -> Int { return 0 }
}

public class D : Base {
// This method witnesses P.f() and so it should be @objc.
//
// CHECK-LABEL: @objc public func f()
public func f() {}
}