Skip to content

Commit 880d7e5

Browse files
Merge pull request #80342 from AnthonyLatsis/andrias-japonicus
Diag: Handle `CustomAttr` in `formatDiagnosticArgument`
2 parents 4efe08e + 72c0d4c commit 880d7e5

20 files changed

+169
-181
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ ERROR(operator_decl_no_fixity,none,
463463
ERROR(operator_decl_expected_precedencegroup, none,
464464
"expected precedence group name after ':' in operator declaration", ())
465465
ERROR(operator_decl_should_not_contain_other_attributes, none,
466-
"unexpected attribute %0 in operator declaration", (StringRef))
466+
"unexpected attribute %0 in operator declaration", (DeclAttribute))
467467
WARNING(operator_decl_remove_designated_types,none,
468468
"designated types are no longer used by the compiler; please remove "
469469
"the designated type list from this operator declaration", ())
@@ -1485,7 +1485,11 @@ ERROR(multiple_access_level_modifiers,none,
14851485
NOTE(previous_access_level_modifier,none,
14861486
"previous modifier specified here", ())
14871487
ERROR(mutually_exclusive_attrs,none,
1488-
"'%0' contradicts previous %select{attribute|modifier}2 '%1'", (StringRef, StringRef, bool))
1488+
"%0 contradicts previous %select{attribute|modifier}2 %1",
1489+
(DeclAttribute, DeclAttribute, bool))
1490+
ERROR(mutually_exclusive_attr_names,none,
1491+
"'%0' contradicts previous %select{attribute|modifier}2 '%1'",
1492+
(StringRef, StringRef, bool))
14891493

14901494
ERROR(invalid_infix_on_func,none,
14911495
"'infix' modifier is not required or allowed on func declarations", ())
@@ -1560,6 +1564,8 @@ WARNING(attr_renamed_to_modifier_warning, none,
15601564
ERROR(attr_name_close_match, none,
15611565
"no attribute named '@%0'; did you mean '@%1'?", (StringRef, StringRef))
15621566
ERROR(attr_unsupported_on_target, none,
1567+
"attribute %0 is unsupported on target '%1'", (DeclAttribute, StringRef))
1568+
ERROR(attr_name_unsupported_on_target, none,
15631569
"attribute '%0' is unsupported on target '%1'", (StringRef, StringRef))
15641570

15651571
// availability

include/swift/AST/DiagnosticsSema.def

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ ERROR(class_cannot_be_addressable_for_dependencies,none,
319319
"a class cannot be @_addressableForDependencies", ())
320320

321321
ERROR(unsupported_closure_attr,none,
322-
"%select{attribute |}0'%1' is not supported on a closure",
323-
(bool, StringRef))
322+
"%select{attribute |}0%1 is not supported on a closure",
323+
(bool, DeclAttribute))
324324

325325
NOTE(suggest_partial_overloads,none,
326326
"overloads for '%1' exist with these %select{"
@@ -1730,11 +1730,11 @@ ERROR(missing_initializer_def,PointsToFirstBadToken,
17301730
ERROR(operator_not_func,none,
17311731
"operators must be declared with 'func'", ())
17321732
ERROR(redefining_builtin_operator,none,
1733-
"cannot declare a custom %0 '%1' operator", (StringRef, StringRef))
1733+
"cannot declare a custom %0 '%1' operator", (DeclAttribute, StringRef))
17341734
ERROR(attribute_requires_operator_identifier,none,
1735-
"'%0' requires a function with an operator identifier", (StringRef))
1735+
"%0 requires a function with an operator identifier", (DeclAttribute))
17361736
ERROR(attribute_requires_single_argument,none,
1737-
"'%0' requires a function with one argument", (StringRef))
1737+
"%0 requires a function with one argument", (DeclAttribute))
17381738

17391739
ERROR(nominal_type_not_attribute,none,
17401740
"%kind0 cannot be used as an attribute", (const ValueDecl *))
@@ -1783,23 +1783,24 @@ ERROR(invalid_ibdesignable_extension,none,
17831783
"@IBDesignable can only be applied to classes and extensions "
17841784
"of classes", ())
17851785
ERROR(attr_must_be_used_on_class_instance,none,
1786-
"only class instance properties can be declared @%0", (StringRef))
1786+
"only class instance properties can be declared %0", (DeclAttribute))
17871787
ERROR(invalid_ibaction_decl,none,
1788-
"only instance methods can be declared @%0", (StringRef))
1788+
"only instance methods can be declared %0", (DeclAttribute))
17891789
ERROR(invalid_ibaction_result,none,
1790-
"methods declared @%0 must %select{|not }1return a value", (StringRef, bool))
1790+
"methods declared %0 must %select{|not }1return a value",
1791+
(DeclAttribute, bool))
17911792
ERROR(invalid_ibaction_argument_count,none,
1792-
"@%0 methods must have %1 to %2 arguments",
1793-
(StringRef, int, int))
1793+
"%0 methods must have %1 to %2 arguments",
1794+
(DeclAttribute, int, int))
17941795
ERROR(invalid_ibaction_argument_count_exact,none,
1795-
"@%0 methods must have %2 argument%s2",
1796-
(StringRef, int, int))
1796+
"%0 methods must have %2 argument%s2",
1797+
(DeclAttribute, int, int))
17971798
ERROR(invalid_ibaction_argument_count_max,none,
1798-
"@%0 methods must have at most %2 argument%s2",
1799-
(StringRef, int, int))
1799+
"%0 methods must have at most %2 argument%s2",
1800+
(DeclAttribute, int, int))
18001801
ERROR(ibsegueaction_objc_method_family,none,
1801-
"@%0 method cannot have selector %1 because it has special memory "
1802-
"management behavior", (StringRef, ObjCSelector))
1802+
"%0 method cannot have selector %1 because it has special memory "
1803+
"management behavior", (DeclAttribute, ObjCSelector))
18031804
NOTE(fixit_rename_in_swift,none,
18041805
"change Swift name to %0", (DeclName))
18051806
NOTE(fixit_rename_in_objc,none,
@@ -2051,7 +2052,8 @@ ERROR(extern_not_at_top_level_func,none,
20512052
ERROR(extern_empty_c_name,none,
20522053
"expected non-empty C name in @_extern attribute", ())
20532054
ERROR(extern_only_non_other_attr,none,
2054-
"@_extern attribute cannot be applied to an '@%0' declaration", (StringRef))
2055+
"@_extern attribute cannot be applied to an %0 declaration",
2056+
(DeclAttribute))
20552057
WARNING(extern_c_maybe_invalid_name, none,
20562058
"C name '%0' may be invalid; explicitly specify the name in @_extern(c) to suppress this warning",
20572059
(StringRef))
@@ -2142,14 +2144,16 @@ ERROR(expose_zero_size_to_cxx,none,
21422144
ERROR(attr_methods_only,none,
21432145
"only methods can be declared %0", (DeclAttribute))
21442146
ERROR(attr_decl_async,none,
2145-
"@%0 %1 cannot be asynchronous", (StringRef, DescriptiveDeclKind))
2147+
"%0 %kindonly1 cannot be asynchronous", (DeclAttribute, const FuncDecl *))
21462148

21472149
ERROR(attr_only_at_non_local_scope, none,
2150+
"attribute %0 can only be used in a non-local scope", (DeclAttribute))
2151+
ERROR(attr_name_only_at_non_local_scope, none,
21482152
"attribute '%0' can only be used in a non-local scope", (StringRef))
21492153
ERROR(attr_only_at_non_generic_scope, none,
2150-
"attribute '%0' cannot be used in a generic context", (StringRef))
2154+
"attribute %0 cannot be used in a generic context", (DeclAttribute))
21512155
ERROR(attr_only_on_static_properties, none,
2152-
"properties with attribute '%0' must be static", (StringRef))
2156+
"properties with attribute %0 must be static", (DeclAttribute))
21532157

21542158
ERROR(weak_unowned_in_embedded_swift, none,
21552159
"attribute %0 cannot be used in embedded Swift", (ReferenceOwnership))
@@ -4018,7 +4022,7 @@ ERROR(attr_has_no_effect_on_unavailable_decl,none,
40184022
(DeclAttribute, const ValueDecl *, AvailabilityDomain))
40194023

40204024
ERROR(attr_ambiguous_reference_to_decl,none,
4021-
"ambiguous reference to %0 in '@%1' attribute", (DeclNameRef, StringRef))
4025+
"ambiguous reference to %0 in %1 attribute", (DeclNameRef, DeclAttribute))
40224026

40234027
ERROR(attr_contains_multiple_versions_for_platform,none,
40244028
"'%0' contains multiple versions for %1", (DeclAttribute, StringRef))
@@ -6001,14 +6005,15 @@ NOTE(move_global_actor_attr_to_storage_decl,none,
60016005
"move global actor attribute to %kind0", (const ValueDecl *))
60026006

60036007
ERROR(actor_isolation_multiple_attr_2,none,
6004-
"%kind0 has multiple actor-isolation attributes ('%1' and '%2')",
6005-
(const Decl *, StringRef, StringRef))
6008+
"%kind0 has multiple actor-isolation attributes (%1 and %2)",
6009+
(const Decl *, DeclAttribute, DeclAttribute))
60066010
ERROR(actor_isolation_multiple_attr_3,none,
6007-
"%0 %1 has multiple actor-isolation attributes ('%2', '%3' and '%4')",
6008-
(const Decl *, StringRef, StringRef, StringRef))
6011+
"%0 %1 has multiple actor-isolation attributes (%2, %3 and %4)",
6012+
(const Decl *, DeclAttribute, DeclAttribute, DeclAttribute))
60096013
ERROR(actor_isolation_multiple_attr_4,none,
6010-
"%0 %1 has multiple actor-isolation attributes ('%2', '%3', '%4', and '%5')",
6011-
(const Decl *, StringRef, StringRef, StringRef, StringRef))
6014+
"%0 %1 has multiple actor-isolation attributes (%2, %3, %4, and %5)",
6015+
(const Decl *, DeclAttribute, DeclAttribute, DeclAttribute,
6016+
DeclAttribute))
60126017
ERROR(actor_isolation_override_mismatch,none,
60136018
"%0 %kind1 has different actor isolation from %2 overridden declaration",
60146019
(ActorIsolation, const ValueDecl *, ActorIsolation))

lib/AST/DiagnosticEngine.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,17 +1083,28 @@ static void formatDiagnosticArgument(StringRef Modifier,
10831083
Out << Stmt::getDescriptiveKindName(Arg.getAsDescriptiveStmtKind());
10841084
break;
10851085

1086-
case DiagnosticArgumentKind::DeclAttribute:
1086+
case DiagnosticArgumentKind::DeclAttribute: {
1087+
auto *const attr = Arg.getAsDeclAttribute();
1088+
const auto printAttrName = [&] {
1089+
if (auto *custom = dyn_cast<CustomAttr>(attr)) {
1090+
custom->getTypeRepr()->print(Out);
1091+
} else {
1092+
Out << attr->getAttrName();
1093+
}
1094+
};
1095+
10871096
assert(Modifier.empty() &&
10881097
"Improper modifier for DeclAttribute argument");
1089-
if (Arg.getAsDeclAttribute()->isDeclModifier())
1090-
Out << FormatOpts.OpeningQuotationMark
1091-
<< Arg.getAsDeclAttribute()->getAttrName()
1092-
<< FormatOpts.ClosingQuotationMark;
1093-
else
1094-
Out << '@' << Arg.getAsDeclAttribute()->getAttrName();
1098+
if (Arg.getAsDeclAttribute()->isDeclModifier()) {
1099+
Out << FormatOpts.OpeningQuotationMark;
1100+
printAttrName();
1101+
Out << FormatOpts.ClosingQuotationMark;
1102+
} else {
1103+
Out << '@';
1104+
printAttrName();
1105+
}
10951106
break;
1096-
1107+
}
10971108
case DiagnosticArgumentKind::AvailabilityDomain:
10981109
assert(Modifier.empty() &&
10991110
"Improper modifier for AvailabilityDomain argument");

lib/Parse/ParseDecl.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,7 +2858,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
28582858

28592859
// Diagnose using access control in a local scope, which isn't meaningful.
28602860
if (CurDeclContext->isLocalContext()) {
2861-
diagnose(Loc, diag::attr_only_at_non_local_scope, AttrName);
2861+
diagnose(Loc, diag::attr_name_only_at_non_local_scope, AttrName);
28622862
}
28632863

28642864
AccessLevel access = llvm::StringSwitch<AccessLevel>(AttrName)
@@ -3067,7 +3067,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
30673067
if (CurDeclContext->isLocalContext()) {
30683068
// Emit an error, but do not discard the attribute. This enables
30693069
// better recovery in the parser.
3070-
diagnose(Loc, diag::attr_only_at_non_local_scope, AttrName);
3070+
diagnose(Loc, diag::attr_name_only_at_non_local_scope, AttrName);
30713071
}
30723072

30733073
if (!DiscardAttribute) {
@@ -3132,7 +3132,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
31323132

31333133
// @_section in a local scope is not allowed.
31343134
if (CurDeclContext->isLocalContext()) {
3135-
diagnose(Loc, diag::attr_only_at_non_local_scope, AttrName);
3135+
diagnose(Loc, diag::attr_name_only_at_non_local_scope, AttrName);
31363136
}
31373137

31383138
if (!DiscardAttribute)
@@ -3619,7 +3619,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
36193619
case DeclAttrKind::Derivative: {
36203620
// `@derivative` in a local scope is not allowed.
36213621
if (CurDeclContext->isLocalContext())
3622-
diagnose(Loc, diag::attr_only_at_non_local_scope, '@' + AttrName.str());
3622+
diagnose(Loc, diag::attr_name_only_at_non_local_scope,
3623+
'@' + AttrName.str());
36233624

36243625
auto Attr = parseDerivativeAttribute(AtLoc, Loc);
36253626
Status |= Attr;
@@ -3631,7 +3632,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
36313632
case DeclAttrKind::Transpose: {
36323633
// `@transpose` in a local scope is not allowed.
36333634
if (CurDeclContext->isLocalContext())
3634-
diagnose(Loc, diag::attr_only_at_non_local_scope, '@' + AttrName.str());
3635+
diagnose(Loc, diag::attr_name_only_at_non_local_scope,
3636+
'@' + AttrName.str());
36353637

36363638
auto Attr = parseTransposeAttribute(AtLoc, Loc);
36373639
Status |= Attr;
@@ -5455,10 +5457,9 @@ static void diagnoseOperatorFixityAttributes(Parser &P,
54555457
for (auto it = fixityAttrs.begin(); it != fixityAttrs.end(); ++it) {
54565458
if (it != fixityAttrs.begin()) {
54575459
auto *attr = *it;
5458-
P.diagnose(attr->getLocation(), diag::mutually_exclusive_attrs,
5459-
attr->getAttrName(), fixityAttrs.front()->getAttrName(),
5460-
attr->isDeclModifier())
5461-
.fixItRemove(attr->getRange());
5460+
P.diagnose(attr->getLocation(), diag::mutually_exclusive_attrs, attr,
5461+
fixityAttrs.front(), attr->isDeclModifier())
5462+
.fixItRemove(attr->getRange());
54625463
attr->setInvalid();
54635464
}
54645465
}
@@ -5475,8 +5476,7 @@ static void diagnoseOperatorFixityAttributes(Parser &P,
54755476
}
54765477
auto *attr = *it;
54775478
P.diagnose(attr->getLocation(),
5478-
diag::operator_decl_should_not_contain_other_attributes,
5479-
attr->getAttrName())
5479+
diag::operator_decl_should_not_contain_other_attributes, attr)
54805480
.fixItRemove(attr->getRange());
54815481
attr->setInvalid();
54825482
}

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ static bool parseDeclSILOptional(
744744
*specialPurpose = SILFunction::Purpose::GlobalInitOnceFunction;
745745
else if (isWeakImported && SP.P.Tok.getText() == "weak_imported") {
746746
if (M.getASTContext().LangOpts.Target.isOSBinFormatCOFF())
747-
SP.P.diagnose(SP.P.Tok, diag::attr_unsupported_on_target,
747+
SP.P.diagnose(SP.P.Tok, diag::attr_name_unsupported_on_target,
748748
SP.P.Tok.getText(),
749749
M.getASTContext().LangOpts.Target.str());
750750
else

lib/SIL/Parser/SILParser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ class SILParser {
394394
if (*existing.Value == value) {
395395
P.diagnose(loc, diag::duplicate_attribute, /*modifier*/ 1);
396396
} else {
397-
P.diagnose(loc, diag::mutually_exclusive_attrs, name, existing.Name,
397+
P.diagnose(loc, diag::mutually_exclusive_attr_names, name,
398+
existing.Name,
398399
/*modifier*/ 1);
399400
}
400401
P.diagnose(existing.Loc, diag::previous_attribute, /*modifier*/ 1);

0 commit comments

Comments
 (0)