Skip to content

Commit 7741409

Browse files
authored
Merge pull request #67075 from beccadax/kinder-diagnostics
Improve DiagnosticEngine's handling of ValueDecl arguments
2 parents c3c84d3 + fe67534 commit 7741409

File tree

57 files changed

+1123
-1419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1123
-1419
lines changed

docs/Diagnostics.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,26 @@ If you run into any issues or have questions while following the steps above, fe
128128

129129
- `%0`, `%1`, etc - Formats the specified diagnostic argument based on its type.
130130

131-
- `%select{a|b|c}0` - Chooses from a list of alternatives, separated by vertical bars, based on the value of the given argument. In this example, a value of 2 in diagnostic argument 0 would result in "c" being output. The argument to the %select may be an integer, enum, or StringRef. If it's a StringRef, the specifier acts as an emptiness check.
131+
- `%select{a|b|c}0` - Chooses from a list of alternatives, separated by vertical bars, based on the value of the given argument. In this example, a value of 2 in diagnostic argument 0 would result in "c" being output. The argument to the %select may be an integer, enum, StringRef, Identifier, or Decl. If it's a StringRef or Identifier, the specifier acts as an emptiness check; if it's a Decl, it acts as a null check.
132132

133133
- `%s0` - Produces an "s" if the given argument is anything other than 1, as meant for an English plural. This isn't particularly localizable without a more general `%plural` form, but most diagnostics try to avoid cases where a plural/singular distinction would be necessary in the first place.
134134

135135
- `%error` - Represents a branch in a `%select` that should never be taken. In debug builds of the compiler this produces an assertion failure.
136136

137137
- `%%` - Emits a literal percent sign.
138138

139+
There are several format specifiers that are specific to `Decl` parameters:
140+
141+
- `%kind0` - Prefixes the declaration's name with its descriptive decl kind (e.g. `instance method 'foo(x:)'`).
142+
143+
- `%kindonly0` - Inserts only the descriptive decl kind (e.g. `instance method`).
144+
145+
- `%base0` - Inserts only the base name, removing any argument labels (e.g. `'foo'`).
146+
147+
- `%kindbase0` - Combines `kind` and `base` (e.g. `instance method 'foo'`).
148+
149+
Note: If your diagnostic could apply to accessors, be careful how you format the declaration's name; accessors have an empty name, so you need to display their accessor kind and the name of their storage decl instead. Inserting the name with a `Decl *` parameter will handle these complications automatically; if you want to use `DeclName` or `Identifier` instead, you'll probably need a separate version of the diagnostic for accessors.
150+
139151
### Diagnostic Verifier ###
140152

141153
(This section is specific to the Swift compiler's diagnostic engine.)

include/swift/AST/DiagnosticEngine.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ class NamedDecl;
3838
}
3939

4040
namespace swift {
41+
class ConstructorDecl;
4142
class Decl;
4243
class DeclAttribute;
4344
class DiagnosticEngine;
45+
class FuncDecl;
4446
class GeneratedSourceInfo;
4547
class SourceManager;
48+
class TypeAliasDecl;
4649
class ValueDecl;
4750
class SourceFile;
4851

@@ -109,7 +112,7 @@ namespace swift {
109112
Unsigned,
110113
Identifier,
111114
ObjCSelector,
112-
ValueDecl,
115+
Decl,
113116
Type,
114117
TypeRepr,
115118
FullyQualifiedType,
@@ -143,7 +146,7 @@ namespace swift {
143146
StringRef StringVal;
144147
DeclNameRef IdentifierVal;
145148
ObjCSelector ObjCSelectorVal;
146-
const ValueDecl *TheValueDecl;
149+
const Decl *TheDecl;
147150
Type TypeVal;
148151
TypeRepr *TyR;
149152
FullyQualified<Type> FullyQualifiedTypeVal;
@@ -194,8 +197,8 @@ namespace swift {
194197
: Kind(DiagnosticArgumentKind::ObjCSelector), ObjCSelectorVal(S) {
195198
}
196199

197-
DiagnosticArgument(const ValueDecl *VD)
198-
: Kind(DiagnosticArgumentKind::ValueDecl), TheValueDecl(VD) {
200+
DiagnosticArgument(const Decl *VD)
201+
: Kind(DiagnosticArgumentKind::Decl), TheDecl(VD) {
199202
}
200203

201204
DiagnosticArgument(Type T)
@@ -305,9 +308,9 @@ namespace swift {
305308
return ObjCSelectorVal;
306309
}
307310

308-
const ValueDecl *getAsValueDecl() const {
309-
assert(Kind == DiagnosticArgumentKind::ValueDecl);
310-
return TheValueDecl;
311+
const Decl *getAsDecl() const {
312+
assert(Kind == DiagnosticArgumentKind::Decl);
313+
return TheDecl;
311314
}
312315

313316
Type getAsType() const {
@@ -1497,13 +1500,6 @@ namespace swift {
14971500
const StringRef Message;
14981501
};
14991502

1500-
/// Returns a value that can be used to select between accessor kinds in
1501-
/// diagnostics.
1502-
///
1503-
/// This is correlated with diag::availability_deprecated and others.
1504-
std::pair<unsigned, DeclName>
1505-
getAccessorKindAndNameForDiagnostics(const ValueDecl *D);
1506-
15071503
/// Retrieve the macro name for a generated source info that represents
15081504
/// a macro expansion.
15091505
DeclName getGeneratedSourceInfoMacroName(const GeneratedSourceInfo &info);

include/swift/AST/DiagnosticsIDE.def

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ NOTE(ide_redundant_import_indirect, none,
3838
"module %0 is already imported via another module import", (StringRef))
3939

4040
WARNING(ide_availability_softdeprecated, Deprecation,
41-
"%select{getter for |setter for |}0%1 will be deprecated"
42-
" in %select{a future version|%select{a future version of %3|%3 %5}4}2"
43-
"%select{|: %6}6",
44-
(unsigned, DeclName, bool, StringRef, bool, llvm::VersionTuple,
41+
"%0 will be deprecated"
42+
" in %select{a future version|%select{a future version of %2|%2 %4}3}1"
43+
"%select{|: %5}5",
44+
(const ValueDecl *, bool, StringRef, bool, llvm::VersionTuple,
4545
StringRef))
4646

4747
WARNING(ide_availability_softdeprecated_rename, Deprecation,
48-
"%select{getter for |setter for |}0%1 will be deprecated"
49-
" in %select{a future version|%select{a future version of %3|%3 %5}4}2"
50-
": renamed to '%6'",
51-
(unsigned, DeclName, bool, StringRef, bool, llvm::VersionTuple, StringRef))
48+
"%0 will be deprecated"
49+
" in %select{a future version|%select{a future version of %2|%2 %4}3}1"
50+
": renamed to '%5'",
51+
(const ValueDecl *, bool, StringRef, bool, llvm::VersionTuple, StringRef))
5252

5353
WARNING(ide_recursive_accessor_reference,none,
5454
"attempting to %select{access|modify}1 %0 within its own "

0 commit comments

Comments
 (0)