Skip to content

Sema: Fix order dependency in @objc inference from witnessed protocol requirement #24255

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 @@ -426,7 +426,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 @@ -442,10 +441,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.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment seems like it shouldn't be correct. Either this function is called re-entrantly and will loop infinitely because of the above call, or the above call notices the circularity and returns without setting anything up. If you're talking about the latter, maybe this should move inside the first if to make it clearer what's going on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, it's not this function that's called re-entrantly; the bad case is when validateDecl() calls us, and we call validateDecl(). In this case validateDecl() returns without doing anything.

if (!witness->hasValidSignature())
return RequirementMatch(witness, MatchKind::Circularity);

Expand Down Expand Up @@ -948,7 +954,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 @@ -1151,9 +1157,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() {}
}