Skip to content

Commit b647245

Browse files
authored
Merge pull request #33316 from HassanElDesouky/localization-ignoreIDs
[Localization] Ignore diagnostic IDs that are available in YAML and not in `.def`
2 parents 8362643 + 55c2e01 commit b647245

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/AST/LocalizationFormat.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ template <> struct ScalarEnumerationTraits<LocalDiagID> {
4646
#define DIAG(KIND, ID, Options, Text, Signature) \
4747
io.enumCase(value, #ID, LocalDiagID::ID);
4848
#include "swift/AST/DiagnosticsAll.def"
49+
// Ignore diagnostic IDs that are available in the YAML file and not
50+
// available in the `.def` file.
51+
if (io.matchEnumFallback())
52+
value = LocalDiagID::NumDiags;
4953
}
5054
};
5155

@@ -101,12 +105,19 @@ readYAML(llvm::yaml::IO &io, T &Seq, bool, Context &Ctx) {
101105
DiagnosticNode current;
102106
yamlize(io, current, true, Ctx);
103107
io.postflightElement(SaveInfo);
104-
// YAML file isn't guaranteed to have diagnostics in order of their
105-
// declaration in `.def` files, to accommodate that we need to leave
106-
// holes in diagnostic array for diagnostics which haven't yet been
107-
// localized and for the ones that have `DiagnosticNode::id`
108-
// indicates their position.
109-
Seq[static_cast<unsigned>(current.id)] = std::move(current.msg);
108+
109+
// A diagnostic ID might be present in YAML and not in `.def` file,
110+
// if that's the case ScalarEnumerationTraits will assign the diagnostic ID
111+
// to `LocalDiagID::NumDiags`. Since the diagnostic ID isn't available
112+
// in `.def` it shouldn't be stored in the diagnostics array.
113+
if (current.id != LocalDiagID::NumDiags) {
114+
// YAML file isn't guaranteed to have diagnostics in order of their
115+
// declaration in `.def` files, to accommodate that we need to leave
116+
// holes in diagnostic array for diagnostics which haven't yet been
117+
// localized and for the ones that have `DiagnosticNode::id`
118+
// indicates their position.
119+
Seq[static_cast<unsigned>(current.id)] = std::move(current.msg);
120+
}
110121
}
111122
}
112123
io.endSequence();

test/diagnostics/Localization/Inputs/en.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#
1818
#===----------------------------------------------------------------------===#
1919

20+
- id: "not_available_in_def"
21+
msg: "Shouldn't be produced"
22+
2023
- id: "lex_unterminated_string"
2124
msg: "unterminated string literal"
2225

0 commit comments

Comments
 (0)