Skip to content

Commit 3054433

Browse files
committed
Addressing discussion feedback
Keeping the original duplicate message if the access levels match Changing the note message Avoid extra attribute list traversals Also avoiding extra variable declarations
1 parent f1e3c82 commit 3054433

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ NOTE(previous_attribute,none,
14881488
ERROR(multiple_access_level_modifiers,none,
14891489
"multiple incompatible access-level modifiers specified", ())
14901490
NOTE(previous_access_level_modifier,none,
1491-
"incompatible modifier specified here", (StringRef))
1491+
"previous modifier specified here", (StringRef))
14921492
ERROR(mutually_exclusive_attrs,none,
14931493
"'%0' contradicts previous %select{attribute|modifier}2 '%1'", (StringRef, StringRef, bool))
14941494

lib/Parse/ParseDecl.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,20 +2987,21 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
29872987
// Normal access control attribute.
29882988
AttrRange = Loc;
29892989

2990-
// Try to find a previously parsed access control attribute.
2991-
auto PrevAccessControlAttr = Attributes.getAttribute<AccessControlAttr>();
29922990

2993-
if (!PrevAccessControlAttr) {
2991+
if (!DiscardAttribute) {
29942992
Attributes.add(new (Context) AccessControlAttr(AtLoc, Loc, access));
2995-
} else {
2993+
break;
2994+
}
2995+
2996+
if (access != cast<AccessControlAttr>(*DuplicateAttribute).getAccess()) {
29962997
// Diagnose if there is already an access control attribute on
2997-
// this declaration.
2998+
// this declaration with a different access level.
29982999
diagnose(Loc, diag::multiple_access_level_modifiers)
29993000
.highlight(AttrRange);
3000-
diagnose(PrevAccessControlAttr->getLocation(),
3001+
diagnose(DuplicateAttribute->getLocation(),
30013002
diag::previous_access_level_modifier,
3002-
PrevAccessControlAttr->getAttrName())
3003-
.highlight(PrevAccessControlAttr->getRange());
3003+
DuplicateAttribute->getAttrName())
3004+
.highlight(DuplicateAttribute->getRange());
30043005

30053006
// Remove the reference to the duplicate attribute
30063007
// to avoid the extra diagnostic.
@@ -3037,20 +3038,20 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
30373038

30383039
DuplicateAttribute = Attributes.getAttribute<SetterAccessAttr>();
30393040

3040-
// Try to find a previously parsed setter access control attribute.
3041-
auto PrevSetterAccessControlAttr = Attributes.getAttribute<SetterAccessAttr>();
3042-
3043-
if (!PrevSetterAccessControlAttr) {
3041+
if (!DuplicateAttribute) {
30443042
Attributes.add(new (Context) SetterAccessAttr(AtLoc, AttrRange, access));
3045-
} else {
3046-
// Diagnose if there is already a setter access control attribute on
3047-
// this declaration.
3043+
break;
3044+
}
3045+
3046+
if (access != cast<SetterAccessAttr>(*DuplicateAttribute).getAccess()) {
3047+
// Diagnose if there is already an access control attribute on
3048+
// this declaration with a different access level.
30483049
diagnose(Loc, diag::multiple_access_level_modifiers)
30493050
.highlight(AttrRange);
3050-
diagnose(PrevSetterAccessControlAttr->getLocation(),
3051+
diagnose(DuplicateAttribute->getLocation(),
30513052
diag::previous_access_level_modifier,
3052-
PrevSetterAccessControlAttr->getAttrName())
3053-
.highlight(PrevSetterAccessControlAttr->getRange());
3053+
DuplicateAttribute->getAttrName())
3054+
.highlight(DuplicateAttribute->getRange());
30543055

30553056
// Remove the reference to the duplicate attribute
30563057
// to avoid the extra diagnostic.

test/attr/accessibility.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// RUN: %target-typecheck-verify-swift -package-name myPkg
22

33
// CHECK PARSING
4-
private // expected-note {{incompatible modifier specified here}}
5-
private // expected-error {{multiple incompatible access-level modifiers specified}}
4+
private // expected-note {{modifier already specified here}}
5+
private // expected-error {{duplicate modifier}}
66
func duplicateAttr() {}
77

8-
private // expected-note {{incompatible modifier specified here}}
8+
private // expected-note {{previous modifier specified here}}
99
public // expected-error {{multiple incompatible access-level modifiers specified}}
1010
func duplicateAttrChanged() {}
1111

12-
private // expected-note 2 {{incompatible modifier specified here}}
12+
private // expected-note 2 {{previous modifier specified here}}
1313
public // expected-error {{multiple incompatible access-level modifiers specified}}
1414
internal // expected-error {{multiple incompatible access-level modifiers specified}}
1515
func triplicateAttrChanged() {}
1616

17-
private // expected-note 3 {{incompatible modifier specified here}}
17+
private // expected-note 3 {{previous modifier specified here}}
1818
public // expected-error {{multiple incompatible access-level modifiers specified}}
1919
package // expected-error {{multiple incompatible access-level modifiers specified}}
2020
internal // expected-error {{multiple incompatible access-level modifiers specified}}
2121
func quadruplicateAttrChanged() {}
2222

23-
private // expected-note 4 {{incompatible modifier specified here}}
23+
private // expected-note 4 {{previous modifier specified here}}
2424
public // expected-error {{multiple incompatible access-level modifiers specified}}
2525
package // expected-error {{multiple incompatible access-level modifiers specified}}
2626
internal // expected-error {{multiple incompatible access-level modifiers specified}}
@@ -51,21 +51,21 @@ internal(set)
5151
package
5252
var customSetter6 = 0
5353

54-
private(set) // expected-note {{incompatible modifier specified here}}
54+
private(set) // expected-note {{previous modifier specified here}}
5555
public(set) // expected-error {{multiple incompatible access-level modifiers specified}}
5656
var customSetterDuplicateAttr = 0
5757

58-
private(set) // expected-note {{incompatible modifier specified here}}
59-
public // expected-note {{incompatible modifier specified here}}
58+
private(set) // expected-note {{previous modifier specified here}}
59+
public // expected-note {{previous modifier specified here}}
6060
public(set) // expected-error {{multiple incompatible access-level modifiers specified}}
6161
private // expected-error {{multiple incompatible access-level modifiers specified}}
6262
var customSetterDuplicateAttrsAllAround = 0
6363

64-
private(set) // expected-note {{incompatible modifier specified here}}
64+
private(set) // expected-note {{previous modifier specified here}}
6565
package(set) // expected-error {{multiple incompatible access-level modifiers specified}}
6666
var customSetterDuplicateAttr2 = 0
6767

68-
package(set) // expected-note {{incompatible modifier specified here}}
68+
package(set) // expected-note {{previous modifier specified here}}
6969
public(set) // expected-error {{multiple incompatible access-level modifiers specified}}
7070
public var customSetterDuplicateAttr3 = 0
7171

test/attr/open.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import OpenHelpers
66

77
/**** General structural limitations on open. ****/
88

9-
open private class OpenIsNotCompatibleWithPrivate {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{incompatible modifier specified here}}
10-
open fileprivate class OpenIsNotCompatibleWithFilePrivate {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{incompatible modifier specified here}}
11-
open internal class OpenIsNotCompatibleWithInternal {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{incompatible modifier specified here}}
12-
open public class OpenIsNotCompatibleWithPublic {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{incompatible modifier specified here}}
13-
open open class OpenIsNotCompatibleWithOpen {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{incompatible modifier specified here}}
9+
open private class OpenIsNotCompatibleWithPrivate {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{previous modifier specified here}}
10+
open fileprivate class OpenIsNotCompatibleWithFilePrivate {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{previous modifier specified here}}
11+
open internal class OpenIsNotCompatibleWithInternal {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{previous modifier specified here}}
12+
open public class OpenIsNotCompatibleWithPublic {} // expected-error {{multiple incompatible access-level modifiers specified}} expected-note{{previous modifier specified here}}
13+
open open class OpenIsNotCompatibleWithOpen {} // expected-error {{duplicate modifier}} expected-note{{modifier already specified here}}
1414

1515
open typealias OpenIsNotAllowedOnTypeAliases = Int // expected-error {{only classes and overridable class members can be declared 'open'; use 'public'}}
1616
open struct OpenIsNotAllowedOnStructs {} // expected-error {{only classes and overridable class members can be declared 'open'; use 'public'}}

0 commit comments

Comments
 (0)