Skip to content

Commit 72c0d4c

Browse files
committed
Diag: Handle CustomAttr in formatDiagnosticArgument
1 parent fffa8c2 commit 72c0d4c

File tree

8 files changed

+69
-77
lines changed

8 files changed

+69
-77
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 8 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{"
@@ -6002,14 +6002,15 @@ NOTE(move_global_actor_attr_to_storage_decl,none,
60026002
"move global actor attribute to %kind0", (const ValueDecl *))
60036003

60046004
ERROR(actor_isolation_multiple_attr_2,none,
6005-
"%kind0 has multiple actor-isolation attributes ('%1' and '%2')",
6006-
(const Decl *, StringRef, StringRef))
6005+
"%kind0 has multiple actor-isolation attributes (%1 and %2)",
6006+
(const Decl *, DeclAttribute, DeclAttribute))
60076007
ERROR(actor_isolation_multiple_attr_3,none,
6008-
"%0 %1 has multiple actor-isolation attributes ('%2', '%3' and '%4')",
6009-
(const Decl *, StringRef, StringRef, StringRef))
6008+
"%0 %1 has multiple actor-isolation attributes (%2, %3 and %4)",
6009+
(const Decl *, DeclAttribute, DeclAttribute, DeclAttribute))
60106010
ERROR(actor_isolation_multiple_attr_4,none,
6011-
"%0 %1 has multiple actor-isolation attributes ('%2', '%3', '%4', and '%5')",
6012-
(const Decl *, StringRef, StringRef, StringRef, StringRef))
6011+
"%0 %1 has multiple actor-isolation attributes (%2, %3, %4, and %5)",
6012+
(const Decl *, DeclAttribute, DeclAttribute, DeclAttribute,
6013+
DeclAttribute))
60136014
ERROR(actor_isolation_override_mismatch,none,
60146015
"%0 %kind1 has different actor isolation from %2 overridden declaration",
60156016
(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/Sema/TypeCheckAttr.cpp

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,69 +4309,57 @@ static void checkGlobalActorAttr(
43094309
auto isolatedAttr = decl->getAttrs().getAttribute<IsolatedAttr>();
43104310
auto nonisolatedAttr = decl->getAttrs().getAttribute<NonisolatedAttr>();
43114311
auto executionAttr = decl->getAttrs().getAttribute<ExecutionAttr>();
4312-
struct NameAndRange {
4313-
StringRef name;
4314-
SourceRange range;
43154312

4316-
NameAndRange(StringRef _name, SourceRange _range)
4317-
: name(_name), range(_range) {}
4318-
};
4319-
4320-
llvm::SmallVector<NameAndRange, 4> attributes;
4313+
llvm::SmallVector<const DeclAttribute *, 2> attributes;
43214314

4322-
attributes.push_back(NameAndRange(globalActorAttr.second->getName().str(),
4323-
globalActorAttr.first->getRangeWithAt()));
4315+
attributes.push_back(globalActorAttr.first);
43244316

43254317
if (isolatedAttr) {
4326-
attributes.push_back(NameAndRange(isolatedAttr->getAttrName(),
4327-
isolatedAttr->getRangeWithAt()));
4318+
attributes.push_back(isolatedAttr);
43284319
}
43294320
if (nonisolatedAttr) {
4330-
attributes.push_back(NameAndRange(nonisolatedAttr->getAttrName(),
4331-
nonisolatedAttr->getRangeWithAt()));
4321+
attributes.push_back(nonisolatedAttr);
43324322
}
43334323
if (executionAttr) {
4334-
attributes.push_back(NameAndRange(executionAttr->getAttrName(),
4335-
executionAttr->getRangeWithAt()));
4324+
attributes.push_back(executionAttr);
43364325
}
43374326

43384327
if (attributes.size() == 1)
43394328
return;
43404329

43414330
if (attributes.size() == 2) {
4342-
decl->diagnose(diag::actor_isolation_multiple_attr_2, decl,
4343-
attributes[0].name, attributes[1].name)
4344-
.highlight(attributes[0].range)
4345-
.highlight(attributes[1].range)
4331+
decl->diagnose(diag::actor_isolation_multiple_attr_2, decl, attributes[0],
4332+
attributes[1])
4333+
.highlight(attributes[0]->getRangeWithAt())
4334+
.highlight(attributes[1]->getRangeWithAt())
43464335
.warnUntilSwiftVersion(6)
4347-
.fixItRemove(attributes[1].range);
4336+
.fixItRemove(attributes[1]->getRangeWithAt());
43484337
return;
43494338
}
43504339

43514340
if (attributes.size() == 3) {
4352-
decl->diagnose(diag::actor_isolation_multiple_attr_3, decl,
4353-
attributes[0].name, attributes[1].name, attributes[2].name)
4354-
.highlight(attributes[0].range)
4355-
.highlight(attributes[1].range)
4356-
.highlight(attributes[2].range)
4341+
decl->diagnose(diag::actor_isolation_multiple_attr_3, decl, attributes[0],
4342+
attributes[1], attributes[2])
4343+
.highlight(attributes[0]->getRangeWithAt())
4344+
.highlight(attributes[1]->getRangeWithAt())
4345+
.highlight(attributes[2]->getRangeWithAt())
43574346
.warnUntilSwiftVersion(6)
4358-
.fixItRemove(attributes[1].range)
4359-
.fixItRemove(attributes[2].range);
4347+
.fixItRemove(attributes[1]->getRangeWithAt())
4348+
.fixItRemove(attributes[2]->getRangeWithAt());
43604349
return;
43614350
}
43624351

43634352
assert(attributes.size() == 4);
4364-
decl->diagnose(diag::actor_isolation_multiple_attr_4, decl,
4365-
attributes[0].name, attributes[1].name, attributes[2].name,
4366-
attributes[3].name)
4367-
.highlight(attributes[0].range)
4368-
.highlight(attributes[1].range)
4369-
.highlight(attributes[2].range)
4370-
.highlight(attributes[3].range)
4353+
decl->diagnose(diag::actor_isolation_multiple_attr_4, decl, attributes[0],
4354+
attributes[1], attributes[2], attributes[3])
4355+
.highlight(attributes[0]->getRangeWithAt())
4356+
.highlight(attributes[1]->getRangeWithAt())
4357+
.highlight(attributes[2]->getRangeWithAt())
4358+
.highlight(attributes[3]->getRangeWithAt())
43714359
.warnUntilSwiftVersion(6)
4372-
.fixItRemove(attributes[1].range)
4373-
.fixItRemove(attributes[2].range)
4374-
.fixItRemove(attributes[3].range);
4360+
.fixItRemove(attributes[1]->getRangeWithAt())
4361+
.fixItRemove(attributes[2]->getRangeWithAt())
4362+
.fixItRemove(attributes[3]->getRangeWithAt());
43754363
}
43764364

43774365
void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
@@ -8137,10 +8125,10 @@ class ClosureAttributeChecker
81378125
: ctx(closure->getASTContext()), closure(closure) { }
81388126

81398127
void visitDeclAttribute(DeclAttribute *attr) {
8140-
ctx.Diags.diagnose(
8141-
attr->getLocation(), diag::unsupported_closure_attr,
8142-
attr->isDeclModifier(), attr->getAttrName())
8143-
.fixItRemove(attr->getRangeWithAt());
8128+
ctx.Diags
8129+
.diagnose(attr->getLocation(), diag::unsupported_closure_attr,
8130+
attr->isDeclModifier(), attr)
8131+
.fixItRemove(attr->getRangeWithAt());
81448132
attr->setInvalid();
81458133
}
81468134

@@ -8236,18 +8224,10 @@ class ClosureAttributeChecker
82368224
}
82378225

82388226
// Otherwise, it's an error.
8239-
std::string typeName;
8240-
if (auto typeRepr = attr->getTypeRepr()) {
8241-
llvm::raw_string_ostream out(typeName);
8242-
typeRepr->print(out);
8243-
} else {
8244-
typeName = attr->getType().getString();
8245-
}
8246-
8247-
ctx.Diags.diagnose(
8248-
attr->getLocation(), diag::unsupported_closure_attr,
8249-
attr->isDeclModifier(), typeName)
8250-
.fixItRemove(attr->getRangeWithAt());
8227+
ctx.Diags
8228+
.diagnose(attr->getLocation(), diag::unsupported_closure_attr,
8229+
attr->isDeclModifier(), attr)
8230+
.fixItRemove(attr->getRangeWithAt());
82518231
attr->setInvalid();
82528232
}
82538233
};

test/Concurrency/isolated_parameters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func isolatedClosures() {
341341
}
342342
}
343343

344-
// expected-warning@+3 {{global function 'allOfEm' has multiple actor-isolation attributes ('MainActor' and 'nonisolated')}}
344+
// expected-warning@+3 {{global function 'allOfEm' has multiple actor-isolation attributes (@MainActor and 'nonisolated')}}
345345
// expected-warning@+2 {{global function with 'isolated' parameter cannot be 'nonisolated'; this is an error in the Swift 6 language mode}}{{12-24=}}
346346
// expected-warning@+1 {{global function with 'isolated' parameter cannot have a global actor; this is an error in the Swift 6 language mode}}{{1-12=}}
347347
@MainActor nonisolated func allOfEm(_ a: isolated A) {

test/Concurrency/nonisolated_rules.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class KlassA {
154154

155155
@MainActor
156156
nonisolated struct Conflict {}
157-
// expected-error@-1 {{struct 'Conflict' has multiple actor-isolation attributes ('MainActor' and 'nonisolated')}}
157+
// expected-error@-1 {{struct 'Conflict' has multiple actor-isolation attributes (@MainActor and 'nonisolated')}}
158158

159159
struct B: Sendable {
160160
// expected-error@+1 {{'nonisolated' can not be applied to variable with non-'Sendable' type 'NonSendable}}

test/attr/attr_execution.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ struct TestAttributeCollisions {
9696
}
9797

9898
@MainActor @execution(concurrent) func testGlobalActor() async {}
99-
// expected-warning @-1 {{instance method 'testGlobalActor()' has multiple actor-isolation attributes ('MainActor' and 'execution(concurrent)')}}
99+
// expected-warning @-1 {{instance method 'testGlobalActor()' has multiple actor-isolation attributes (@MainActor and @execution(concurrent))}}
100100

101101
@execution(caller) nonisolated func testNonIsolatedCaller() async {} // Ok
102102
@MainActor @execution(caller) func testGlobalActorCaller() async {}
103-
// expected-warning@-1 {{instance method 'testGlobalActorCaller()' has multiple actor-isolation attributes ('MainActor' and 'execution(caller)')}}
103+
// expected-warning@-1 {{instance method 'testGlobalActorCaller()' has multiple actor-isolation attributes (@MainActor and @execution(caller))}}
104104
@execution(caller) func testCaller(arg: isolated MainActor) async {}
105105
// expected-error@-1 {{cannot use '@execution' on instance method 'testCaller(arg:)' because it has an isolated parameter: 'arg'}}
106106

test/attr/closures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// RUN: %target-typecheck-verify-swift -swift-version 5
22

33
func testNonacceptedClosures() {
4-
let fn = { @usableFromInline in // expected-error{{'usableFromInline' is not supported on a closure}}
4+
let fn = { @usableFromInline in // expected-error{{attribute @usableFromInline is not supported on a closure}}
55
"hello"
66
}
77

8-
let fn2: (Int) -> Int = { @usableFromInline x in // expected-error{{'usableFromInline' is not supported on a closure}}
8+
let fn2: (Int) -> Int = { @usableFromInline x in // expected-error{{attribute @usableFromInline is not supported on a closure}}
99
print("hello")
1010
return x
1111
}

test/attr/global_actor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct Container {
9898
// Redundant attributes
9999
// -----------------------------------------------------------------------
100100
extension SomeActor {
101-
@GA1 nonisolated func conflict1() { } // expected-warning {{instance method 'conflict1()' has multiple actor-isolation attributes ('GA1' and 'nonisolated')}}
101+
@GA1 nonisolated func conflict1() { } // expected-warning {{instance method 'conflict1()' has multiple actor-isolation attributes (@GA1 and 'nonisolated')}}
102102
}
103103

104104

0 commit comments

Comments
 (0)