Skip to content

Commit b8d2f26

Browse files
authored
Merge pull request #38662 from RAJAGOPALAN-GANGADHARAN/sr_14951
Fixes SR-14951 : Print message of @available attribute within Protocol
2 parents 37a48ea + 585350d commit b8d2f26

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,8 +2317,8 @@ NOTE(declared_protocol_conformance_here,none,
23172317
(Type, unsigned, Identifier, Identifier))
23182318

23192319
ERROR(witness_unavailable,none,
2320-
"unavailable %0 %1 was used to satisfy a requirement of protocol %2",
2321-
(DescriptiveDeclKind, DeclName, Identifier))
2320+
"unavailable %0 %1 was used to satisfy a requirement of protocol %2%select{|: %3}3",
2321+
(DescriptiveDeclKind, DeclName, Identifier, StringRef))
23222322

23232323
ERROR(redundant_conformance,none,
23242324
"redundant conformance of %0 to protocol %1", (Type, Identifier))

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,11 +4198,12 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
41984198
NormalProtocolConformance *conformance) {
41994199
auto &diags = witness->getASTContext().Diags;
42004200
SourceLoc diagLoc = getLocForDiagnosingWitness(conformance, witness);
4201-
diags.diagnose(diagLoc,
4202-
diag::witness_unavailable,
4203-
witness->getDescriptiveKind(),
4204-
witness->getName(),
4205-
conformance->getProtocol()->getName());
4201+
auto *attr = AvailableAttr::isUnavailable(witness);
4202+
EncodedDiagnosticMessage EncodedMessage(attr->Message);
4203+
diags.diagnose(diagLoc, diag::witness_unavailable,
4204+
witness->getDescriptiveKind(), witness->getName(),
4205+
conformance->getProtocol()->getName(),
4206+
EncodedMessage.Message);
42064207
emitDeclaredHereIfNeeded(diags, diagLoc, witness);
42074208
diags.diagnose(requirement, diag::kind_declname_declared_here,
42084209
DescriptiveDeclKind::Requirement,

test/decl/protocol/req/unavailable.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,24 @@ struct ConformsToP2 {
4040
}
4141
extension ConformsToP2: P {} // expected-error{{type 'ConformsToP2' does not conform to protocol 'P'}}
4242
// expected-error@-1 {{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
43+
44+
45+
// Include message string from @available attribute if provided
46+
protocol Unavail {
47+
associatedtype T
48+
func req() // expected-note {{requirement 'req()' declared here}}
49+
}
50+
extension Unavail {
51+
func req() {}
52+
}
53+
extension Unavail where T == Self {
54+
@available(*, unavailable, message: "write it yourself") func req() {} // expected-note {{'req()' declared here}}
55+
}
56+
57+
struct NonSelfT: Unavail {
58+
typealias T = Int
59+
}
60+
struct SelfT: Unavail { // expected-error {{type 'SelfT' does not conform to protocol 'Unavail'}}
61+
// expected-error@-1 {{unavailable instance method 'req()' was used to satisfy a requirement of protocol 'Unavail': write it yourself}}
62+
typealias T = SelfT
63+
}

0 commit comments

Comments
 (0)