Skip to content

Commit 205bfee

Browse files
authored
Merge pull request #79537 from tshortli/potentially-unavailable-witness-in-unavailable-conformance
Sema: Skip diagnosing witness availability for unavailable conformances
2 parents 25fa312 + 54e6900 commit 205bfee

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,14 @@ bool TypeChecker::isAvailabilitySafeForConformance(
16881688
assert(dc->getSelfNominalTypeDecl() &&
16891689
"Must have a nominal or extension context");
16901690

1691+
AvailabilityContext contextForConformingDecl =
1692+
availabilityForDeclSignature(dc->getAsDecl());
1693+
1694+
// If the conformance is unavailable then it's irrelevant whether the witness
1695+
// is potentially unavailable.
1696+
if (contextForConformingDecl.isUnavailable())
1697+
return true;
1698+
16911699
// Make sure that any access of the witness through the protocol
16921700
// can only occur when the witness is available. That is, make sure that
16931701
// on every version where the conforming declaration is available, if the
@@ -1703,7 +1711,7 @@ bool TypeChecker::isAvailabilitySafeForConformance(
17031711
requirementInfo = AvailabilityInference::availableRange(requirement);
17041712

17051713
AvailabilityRange infoForConformingDecl =
1706-
overApproximateAvailabilityAtLocation(dc->getAsDecl()->getLoc(), dc);
1714+
contextForConformingDecl.getPlatformRange();
17071715

17081716
// Relax the requirements for @_spi witnesses by treating the requirement as
17091717
// if it were introduced at the deployment target. This is not strictly sound

test/Sema/availability_scopes.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ extension SomeEnum {
373373
func neverAvailable() {}
374374
}
375375

376+
// CHECK-NEXT: {{^}} (decl version=50 unavailable=macOS decl=unavailableOnMacOSAndIntroduced()
377+
378+
@available(macOS, unavailable, introduced: 52)
379+
func unavailableOnMacOSAndIntroduced() {
380+
}
381+
376382
// CHECK-NEXT: {{^}} (decl version=50 unavailable=* decl=NeverAvailable
377383
// CHECK-NEXT: {{^}} (decl version=50 unavailable=* decl=unavailableOnMacOS()
378384

test/Sema/conformance_availability.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ extension Bike : Vehicle {
466466
func move() {}
467467
}
468468

469+
@available(macOS, introduced: 100)
470+
struct Wagon {}
471+
472+
@available(macOS, introduced: 100)
473+
extension Wagon : Vehicle {
474+
func move() {}
475+
}
476+
469477
class Car {}
470478
class ClownCar : Car {}
471479

@@ -478,6 +486,63 @@ extension Car {
478486
extension ClownCar : Vehicle {}
479487
// expected-error@-1 {{protocol 'Vehicle' requires 'move()' to be available in macOS 100 and newer}}
480488

489+
@available(macOS, unavailable)
490+
struct Truck : Vehicle {
491+
func move() {}
492+
}
493+
494+
struct Scooter {}
495+
496+
@available(macOS, unavailable)
497+
extension Scooter : Vehicle {
498+
func move() {}
499+
}
500+
501+
struct Motorcycle {}
502+
503+
@available(macOS, unavailable)
504+
extension Motorcycle : Vehicle {
505+
@available(macOS, introduced: 100)
506+
func move() {}
507+
}
508+
509+
@available(macOS, unavailable)
510+
struct AircraftCarrier {
511+
struct Jet : Vehicle {
512+
@available(macOS, introduced: 100)
513+
func move() {}
514+
}
515+
}
516+
517+
struct Unicycle {
518+
@available(macOS, introduced: 100)
519+
func move() {}
520+
}
521+
522+
@available(macOS, unavailable)
523+
extension Unicycle : Vehicle {}
524+
525+
@available(macOS, unavailable, introduced: 100)
526+
struct Train : Vehicle {
527+
func move() {}
528+
}
529+
530+
struct Blimp {}
531+
532+
@available(macOS, unavailable, introduced: 100)
533+
extension Blimp : Vehicle {
534+
func move() {}
535+
}
536+
537+
@available(macOS, unavailable, introduced: 100)
538+
struct Spaceship {
539+
}
540+
541+
@available(macOS, unavailable, introduced: 100)
542+
extension Spaceship : Vehicle {
543+
func move() {}
544+
}
545+
481546
// rdar://problem/75430966 - Allow using unavailable conformances from unavailable contexts.
482547
@available(*, unavailable)
483548
public enum UnavailableEnum {

0 commit comments

Comments
 (0)