Skip to content

Commit e13a85c

Browse files
committed
NFC: make headers compatible with C++20
In a downstream project we are using the Swift code as a library and compiling with C++20. This mostly works except for two backward incompatibilities introduced by C++20: * [P1008] disallows aggregate initialization when defaulted or deleted constructors are specified. This lead to a compilation error for aggregate initialization of `swift::Compilation::Result`. The backward and forward compatible fix for that is to provide a constructor turning aggregate initializations into a normal constructor call. * [P1185] introduces more candidates for overload resolution of comparison operator calls. As explained in [P1630], this leads in some cases to ambiguity compilation errors in C++20, which is exactly the case with `swift::ValueOwnershipKind`. The fix in this case is to remove some redundant operator calls conditionally on the `__cpp_impl_three_way_comparison` feature test macro, which [includes the papers mentioned above][feature_test]. [P1008]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1008r1.pdf [P1185]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1185r2.html [P1630]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1630r1.html [feature_test]: https://en.cppreference.com/w/cpp/feature_test
1 parent 53da675 commit e13a85c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

include/swift/Driver/Compilation.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class Compilation {
8989
/// This data is used for cross-module module dependencies.
9090
fine_grained_dependencies::ModuleDepGraph depGraph;
9191

92+
// this constructor is provided instead of relying on aggregate
93+
// initialization to be compatible with C++20, as P1185 disallowed that for
94+
// types with defaulted or deleted constructors
95+
Result(bool hadAbnormalExit = false, int exitCode = 0,
96+
fine_grained_dependencies::ModuleDepGraph&& depGraph = {})
97+
: hadAbnormalExit{hadAbnormalExit}, exitCode{exitCode},
98+
depGraph{std::move(depGraph)} {}
99+
92100
Result(const Result &) = delete;
93101
Result &operator=(const Result &) = delete;
94102

@@ -97,8 +105,7 @@ class Compilation {
97105

98106
/// Construct a \c Compilation::Result from just an exit code.
99107
static Result code(int code) {
100-
return Compilation::Result{false, code,
101-
fine_grained_dependencies::ModuleDepGraph()};
108+
return Compilation::Result{false, code};
102109
}
103110
};
104111

include/swift/SIL/SILValue.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ struct ValueOwnershipKind {
271271

272272
explicit operator bool() const { return value != OwnershipKind::Any; }
273273

274+
#ifndef __cpp_impl_three_way_comparison
275+
// C++20 (more precisely P1185) introduced more overload candidates for
276+
// comparison operator calls. With that in place the following definitions are
277+
// redundant and actually cause compilation errors because of ambiguity.
278+
// P1630 explains the rationale behind introducing this backward
279+
// incompatibility.
280+
//
281+
// References:
282+
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1185r2.html
283+
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1630r1.html
284+
274285
bool operator==(ValueOwnershipKind other) const {
275286
return value == other.value;
276287
}
@@ -280,6 +291,7 @@ struct ValueOwnershipKind {
280291

281292
bool operator==(innerty other) const { return value == other; }
282293
bool operator!=(innerty other) const { return !(value == other); }
294+
#endif
283295

284296
/// We merge by moving down the lattice.
285297
ValueOwnershipKind merge(ValueOwnershipKind rhs) const {

0 commit comments

Comments
 (0)