Skip to content

Sema: Check conditional availability earlier in conformance checking #38329

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
21 changes: 21 additions & 0 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,27 @@ bool WitnessChecker::findBestWitness(
}
}

// If there are multiple viable matches, drop any that are less available than the
// requirement.
if (numViable > 1) {
SmallVector<RequirementMatch, 2> checkedMatches;
bool foundCheckedMatch = false;

for (auto match : matches) {
if (!match.isViable()) {
checkedMatches.push_back(match);
} else if (checkWitness(requirement, match).Kind != CheckKind::Availability) {
foundCheckedMatch = true;
checkedMatches.push_back(match);
}
}

// If none of the matches were at least as available as the requirement, don't
// drop any of them; this will produce better diagnostics.
if (foundCheckedMatch)
std::swap(checkedMatches, matches);
}

if (numViable == 0) {
// Assume any missing value witnesses for a conformance in a module
// interface can be treated as opaque.
Expand Down
17 changes: 7 additions & 10 deletions test/Sema/availability_versions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ protocol ProtocolWithRequirementMentioningUnavailable {

protocol HasMethodF {
associatedtype T
func f(_ p: T) // expected-note 4{{protocol requirement here}}
func f(_ p: T) // expected-note 3{{protocol requirement here}}
}

class TriesToConformWithFunctionIntroducedOn10_51 : HasMethodF {
Expand Down Expand Up @@ -1499,17 +1499,14 @@ extension TakesClassAvailableOn10_51_B : HasTakesClassAvailableOn10_51 {
}


// We do not want potential unavailability to play a role in picking a witness for a
// protocol requirement. Rather, the witness should be chosen, regardless of its
// potential unavailability, and then it should be diagnosed if it is less available
// than the protocol requires.
class TestAvailabilityDoesNotAffectWitnessCandidacy : HasMethodF {
// Test that we choose the more specialized witness even though it is
// less available than the protocol requires and there is a less specialized
// witness that has suitable availability.
// We want conditional availability to play a role in picking a witness for a
// protocol requirement.
class TestAvailabilityAffectsWitnessCandidacy : HasMethodF {
// Test that we choose the less specialized witness, because the more specialized
// witness is conditionally unavailable.

@available(OSX, introduced: 10.51)
func f(_ p: Int) { } // expected-error {{protocol 'HasMethodF' requires 'f' to be available in macOS 10.50.0 and newer}}
func f(_ p: Int) { }

func f<T>(_ p: T) { }
}
Expand Down
43 changes: 43 additions & 0 deletions test/Sema/conformance_availability_disambiguate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s

// REQUIRES: OS=macosx

public protocol Potato {
func eat()
}

public protocol Fried {}

extension Potato {
public func eat() {}
}

@available(macOS 100, *)
extension Potato where Self : Fried {
public func eat() {}
}

// We ought to pick the unconstrained Potato.eat(), not
// the one from the extension, because the extension has
// narrower availability than the conformance.
public struct CurlyFries : Potato, Fried {}

public struct TaterTots {}

// This conformance on the other hand should use the
// constrained Potato.eat(), since the generic signature
// is more specific than the unconstrained protocol
// extension.
@available(macOS 100, *)
extension TaterTots : Potato, Fried {}

// We FileCheck the SILGen output to verify that the correct
// witnesses were chosen above.

// CHECK-LABEL: sil shared [transparent] [thunk] @$s37conformance_availability_disambiguate10CurlyFriesVAA6PotatoA2aDP3eatyyFTW : $@convention(witness_method: Potato) (@in_guaranteed CurlyFries) -> () {
// CHECK: function_ref @$s37conformance_availability_disambiguate6PotatoPAAE3eatyyF : $@convention(method) <τ_0_0 where τ_0_0 : Potato> (@in_guaranteed τ_0_0) -> ()
// CHECK: return

// sil shared [transparent] [thunk] @$s37conformance_availability_disambiguate9TaterTotsVAA6PotatoA2aDP3eatyyFTW : $@convention(witness_method: Potato) (@in_guaranteed TaterTots) -> () {
// CHECK: function_ref @$s37conformance_availability_disambiguate6PotatoPA2A5FriedRzrlE3eatyyF : $@convention(method) <τ_0_0 where τ_0_0 : Fried, τ_0_0 : Potato> (@in_guaranteed τ_0_0) -> ()
// CHECK: return