Skip to content

Commit 367f57e

Browse files
committed
Merge pull request swiftlang#16489 from jrose-apple/to-put-it-in-context
Make sure protocol witness errors don't leave the conformance context (cherry picked from commit 88169d6)
1 parent 1600ae0 commit 367f57e

25 files changed

+423
-208
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ ERROR(witness_argument_name_mismatch,none,
14951495
"%select{method|initializer}0 %1 has different argument labels from those "
14961496
"required by protocol %2 (%3)", (bool, DeclName, Type, DeclName))
14971497
ERROR(witness_initializer_not_required,none,
1498-
"initializer requirement %0 can only be satisfied by a `required` "
1498+
"initializer requirement %0 can only be satisfied by a 'required' "
14991499
"initializer in%select{| the definition of}1 non-final class %2",
15001500
(DeclName, bool, Type))
15011501
ERROR(witness_initializer_failability,none,
@@ -1517,12 +1517,12 @@ NOTE(witness_self_weaken_same_type,none,
15171517
"consider weakening the same-type requirement %0 == %1 to a superclass "
15181518
"requirement", (Type, Type))
15191519
ERROR(witness_requires_dynamic_self,none,
1520-
"method %0 in non-final class %1 must return `Self` to conform to "
1520+
"method %0 in non-final class %1 must return 'Self' to conform to "
15211521
"protocol %2",
15221522
(DeclName, Type, Type))
15231523
ERROR(witness_requires_class_implementation,none,
15241524
"method %0 in non-final class %1 cannot be implemented in a "
1525-
"protocol extension because it returns `Self` and has associated type "
1525+
"protocol extension because it returns 'Self' and has associated type "
15261526
"requirements",
15271527
(DeclName, Type))
15281528
ERROR(witness_not_accessible_proto,none,
@@ -1550,6 +1550,9 @@ ERROR(type_witness_objc_generic_parameter,none,
15501550
"type %0 involving Objective-C type parameter%select{| %1}2 cannot be "
15511551
"used for associated type %3 of protocol %4",
15521552
(Type, Type, bool, DeclName, DeclName))
1553+
NOTE(witness_fix_access,none,
1554+
"mark the %0 as '%select{%error|fileprivate|internal|public|%error}1' to "
1555+
"satisfy the requirement", (DescriptiveDeclKind, AccessLevel))
15531556

15541557
ERROR(protocol_refine_access,none,
15551558
"%select{protocol must be declared %select{"
@@ -3377,10 +3380,10 @@ WARNING(objc_inference_swift3_objc_derived,none,
33773380
"deprecated", ())
33783381

33793382
NOTE(objc_inference_swift3_addobjc,none,
3380-
"add `@objc` to continue exposing an Objective-C entry point (Swift 3 "
3383+
"add '@objc' to continue exposing an Objective-C entry point (Swift 3 "
33813384
"behavior)", ())
33823385
NOTE(objc_inference_swift3_addnonobjc,none,
3383-
"add `@nonobjc` to suppress the Objective-C entry point (Swift 4 "
3386+
"add '@nonobjc' to suppress the Objective-C entry point (Swift 4 "
33843387
"behavior)", ())
33853388

33863389
ERROR(objc_for_generic_class,none,
@@ -3775,9 +3778,6 @@ ERROR(availability_protocol_requires_version,
37753778
NOTE(availability_protocol_requirement_here, none,
37763779
"protocol requirement here", ())
37773780

3778-
NOTE(availability_conformance_introduced_here, none,
3779-
"conformance introduced here", ())
3780-
37813781
// This doesn't display as an availability diagnostic, but it's
37823782
// implemented there and fires when these subscripts are marked
37833783
// unavailable, so it seems appropriate to put it here.

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,9 @@ bool swift::fixDeclarationName(InFlightDiagnostic &diag, ValueDecl *decl,
22672267
bool swift::fixDeclarationObjCName(InFlightDiagnostic &diag, ValueDecl *decl,
22682268
Optional<ObjCSelector> targetNameOpt,
22692269
bool ignoreImpliedName) {
2270+
if (decl->isImplicit())
2271+
return false;
2272+
22702273
// Subscripts cannot be renamed, so handle them directly.
22712274
if (isa<SubscriptDecl>(decl)) {
22722275
diag.fixItInsert(decl->getAttributeInsertionLoc(/*forModifier=*/false),

lib/AST/Decl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,9 @@ bool ValueDecl::canInferObjCFromRequirement(ValueDecl *requirement) {
20952095
}
20962096

20972097
SourceLoc ValueDecl::getAttributeInsertionLoc(bool forModifier) const {
2098+
if (isImplicit())
2099+
return SourceLoc();
2100+
20982101
if (auto var = dyn_cast<VarDecl>(this)) {
20992102
// [attrs] var ...
21002103
// The attributes are part of the VarDecl, but the 'var' is part of the PBD.

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 181 additions & 84 deletions
Large diffs are not rendered by default.

test/ClangImporter/objc_generics_conformance.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ extension GenericClass : WithAssocOther {
2424
typealias Other = [T] // expected-error{{type 'GenericClass<T>.Other' involving Objective-C type parameter 'T' cannot be used for associated type 'Other' of protocol 'WithAssocOther'}}
2525
}
2626

27+
protocol WithAssocSeparate {
28+
associatedtype Separate
29+
}
30+
31+
extension GenericClass {
32+
typealias Separate = T // expected-note {{'Separate' declared here}}
33+
}
34+
extension GenericClass : WithAssocSeparate { // expected-error {{type 'GenericClass<T>.Separate' involving Objective-C type parameter 'T' cannot be used for associated type 'Separate' of protocol 'WithAssocSeparate'}}
35+
}
36+
2737
protocol WithAssocElement {
2838
associatedtype Element
2939
}

test/Compatibility/accessibility.swift

Lines changed: 37 additions & 32 deletions
Large diffs are not rendered by default.

test/Compatibility/accessibility_private.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,21 @@ protocol VeryImportantProto {
118118
}
119119

120120
private struct VIPPrivateType : VeryImportantProto {
121-
private typealias Assoc = Int // expected-error {{type alias 'Assoc' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}}
121+
private typealias Assoc = Int // expected-error {{type alias 'Assoc' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}} {{none}}
122+
// expected-note@-1 {{mark the type alias as 'fileprivate' to satisfy the requirement}} {{3-10=fileprivate}}
122123
var value: Int
123124
}
124125

125126
private struct VIPPrivateProp : VeryImportantProto {
126127
typealias Assoc = Int
127-
private var value: Int // expected-error {{property 'value' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}} {{3-10=fileprivate}}
128+
private var value: Int // expected-error {{property 'value' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}} {{none}}
129+
// expected-note@-1 {{mark the var as 'fileprivate' to satisfy the requirement}} {{3-10=fileprivate}}
128130
}
129131

130132
private struct VIPPrivateSetProp : VeryImportantProto {
131133
typealias Assoc = Int
132-
private(set) var value: Int // expected-error {{setter for property 'value' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}} {{3-10=fileprivate}}
134+
private(set) var value: Int // expected-error {{setter for property 'value' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}} {{none}}
135+
// expected-note@-1 {{mark the var as 'fileprivate' to satisfy the requirement}} {{3-10=fileprivate}}
133136
}
134137

135138
private class VIPPrivateSetBase {
@@ -140,9 +143,11 @@ private class VIPPrivateSetSub : VIPPrivateSetBase, VeryImportantProto { // expe
140143
}
141144

142145
private class VIPPrivateSetPropBase {
143-
private(set) var value: Int = 0 // expected-error {{setter for property 'value' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}} {{3-10=fileprivate}}
146+
private(set) var value: Int = 0
147+
// expected-note@-1 {{mark the var as 'fileprivate' to satisfy the requirement}} {{3-10=fileprivate}}
144148
}
145149
private class VIPPrivateSetPropSub : VIPPrivateSetPropBase, VeryImportantProto {
150+
// expected-error@-1 {{setter for property 'value' must be as accessible as its enclosing type because it matches a requirement in protocol 'VeryImportantProto'}} {{none}}
146151
typealias Assoc = Int
147152
}
148153

test/NameBinding/accessibility.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,22 @@ func privateInOtherFile() {}
132132

133133
#if !ACCESS_DISABLED
134134
struct ConformerByTypeAlias : TypeProto {
135-
private typealias TheType = Int // expected-error {{type alias 'TheType' must be declared internal because it matches a requirement in internal protocol 'TypeProto'}} {{3-10=internal}}
135+
private typealias TheType = Int // expected-error {{type alias 'TheType' must be declared internal because it matches a requirement in internal protocol 'TypeProto'}} {{none}}
136+
// expected-note@-1 {{mark the type alias as 'internal' to satisfy the requirement}} {{3-10=internal}}
136137
}
137138

138139
struct ConformerByLocalType : TypeProto {
139-
private struct TheType {} // expected-error {{struct 'TheType' must be declared internal because it matches a requirement in internal protocol 'TypeProto'}} {{3-10=internal}}
140+
private struct TheType {} // expected-error {{struct 'TheType' must be declared internal because it matches a requirement in internal protocol 'TypeProto'}} {{none}}
141+
// expected-note@-1 {{mark the struct as 'internal' to satisfy the requirement}} {{3-10=internal}}
140142
}
141143

142144
private struct PrivateConformerByLocalType : TypeProto {
143145
struct TheType {} // okay
144146
}
145147

146148
private struct PrivateConformerByLocalTypeBad : TypeProto {
147-
private struct TheType {} // expected-error {{struct 'TheType' must be as accessible as its enclosing type because it matches a requirement in protocol 'TypeProto'}} {{3-10=fileprivate}}
149+
private struct TheType {} // expected-error {{struct 'TheType' must be as accessible as its enclosing type because it matches a requirement in protocol 'TypeProto'}} {{none}}
150+
// expected-note@-1 {{mark the struct as 'fileprivate' to satisfy the requirement}} {{3-10=fileprivate}}
148151
}
149152
#endif
150153

0 commit comments

Comments
 (0)