Skip to content

Commit 7eff49c

Browse files
authored
[cxx-interop] [nfc] Remove swift namespace from SwiftShims in C++ mode. (#32715)
Most SwiftShims were put in the swift namespace in C++ mode which broke certain things when importing them in a swift file in C++ mode. This was OK when they were only imported as part of the swift runtime but, now they are used in C++ mode both in the swift runtime and when C++ interop is enabled. This broke when C++ interop was enabled because the `Swift` module contains references to symbols in the SwiftShims headers which are built without C++ interop enabled (no "swift" namespace). But, when C++ interop is enabled, the SwiftShims headers would put everything in the swift namespace meaning the symbols couldn't be found in the global namespace. Then, the compiler would error when trying to deserialize the Swift module.
1 parent f558eb3 commit 7eff49c

18 files changed

+87
-87
lines changed

stdlib/public/SwiftShims/AssertionReporting.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#endif
2222

2323
#ifdef __cplusplus
24-
namespace swift { extern "C" {
24+
extern "C" {
2525
#endif
2626

2727
/// Report a fatal error to system console, stderr, and crash logs.
@@ -69,7 +69,7 @@ void _swift_stdlib_reportUnimplementedInitializer(
6969
__swift_uint32_t flags);
7070

7171
#ifdef __cplusplus
72-
}} // extern "C", namespace swift
72+
} // extern "C"
7373
#endif
7474

7575
#if __has_feature(nullability)

stdlib/public/SwiftShims/CoreFoundationShims.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "Visibility.h"
2424

2525
#ifdef __cplusplus
26-
namespace swift { extern "C" {
26+
extern "C" {
2727
#endif
2828

2929
#ifdef __OBJC2__
@@ -78,7 +78,7 @@ _swift_stdlib_dyld_is_objc_constant_string(const void * _Nonnull addr);
7878
#endif // __OBJC2__
7979

8080
#ifdef __cplusplus
81-
}} // extern "C", namespace swift
81+
} // extern "C"
8282
#endif
8383

8484
#endif // SWIFT_STDLIB_SHIMS_COREFOUNDATIONSHIMS_H

stdlib/public/SwiftShims/DispatchOverlayShims.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#pragma clang assume_nonnull begin
3333

3434
#ifdef __cplusplus
35-
namespace swift { extern "C" {
35+
extern "C" {
3636
#endif
3737

3838
typedef void (^__swift_shims_dispatch_block_t)(void);
@@ -251,7 +251,7 @@ static inline void _swift_dispatch_release(dispatch_object_t object) {
251251
}
252252

253253
#ifdef __cplusplus
254-
}} // extern "C", namespace swift
254+
} // extern "C"
255255
#endif
256256

257257
#pragma clang assume_nonnull end

stdlib/public/SwiftShims/FoundationShims.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "SwiftStdint.h"
3131

3232
#ifdef __cplusplus
33-
namespace swift { extern "C" {
33+
extern "C" {
3434
#endif
3535

3636
typedef struct {
@@ -58,7 +58,7 @@ SWIFT_RUNTIME_STDLIB_API
5858
_SwiftNSOperatingSystemVersion _swift_stdlib_operatingSystemVersion() __attribute__((const));
5959

6060
#ifdef __cplusplus
61-
}} // extern "C", namespace swift
61+
} // extern "C"
6262
#endif
6363

6464
#endif // SWIFT_STDLIB_SHIMS_FOUNDATIONSHIMS_H

stdlib/public/SwiftShims/LibcShims.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#endif
3030

3131
#ifdef __cplusplus
32-
namespace swift { extern "C" {
32+
extern "C" {
3333
#endif
3434

3535
// This declaration might not be universally correct.
@@ -184,7 +184,7 @@ long double lgammal_r(long double x, int *psigngam);
184184
#endif // defined(__APPLE__)
185185

186186
#ifdef __cplusplus
187-
}} // extern "C", namespace swift
187+
} // extern "C"
188188
#endif
189189

190190
#if __has_feature(nullability)

stdlib/public/SwiftShims/Random.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
#endif
2626

2727
#ifdef __cplusplus
28-
namespace swift { extern "C" {
28+
extern "C" {
2929
#endif
3030

3131
SWIFT_RUNTIME_STDLIB_API
3232
void swift_stdlib_random(void *buf, __swift_size_t nbytes);
3333

3434
#ifdef __cplusplus
35-
}} // extern "C", namespace swift
35+
} // extern "C"
3636
#endif
3737

3838
#if __has_feature(nullability)

stdlib/public/SwiftShims/RuntimeShims.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "Visibility.h"
2424

2525
#ifdef __cplusplus
26-
namespace swift { extern "C" {
26+
extern "C" {
2727
#endif
2828

2929
/// Return an NSString to be used as the Mirror summary of the object
@@ -85,7 +85,7 @@ __swift_size_t _swift_stdlib_getHardwareConcurrency(void);
8585
#define _swift_MinAllocationAlignment (__swift_size_t) 16
8686

8787
#ifdef __cplusplus
88-
}} // extern "C", namespace swift
88+
} // extern "C"
8989
#endif
9090

9191
#endif // SWIFT_STDLIB_SHIMS_RUNTIMESHIMS_H

stdlib/public/SwiftShims/RuntimeStubs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "LibcShims.h"
2323

2424
#ifdef __cplusplus
25-
namespace swift { extern "C" {
25+
extern "C" {
2626
#endif
2727

2828
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
@@ -42,7 +42,7 @@ _swift_stdlib_overrideUnsafeArgvArgc(char * _Nullable * _Nonnull argv, int argc)
4242
SWIFT_END_NULLABILITY_ANNOTATIONS
4343

4444
#ifdef __cplusplus
45-
}} // extern "C", namespace swift
45+
} // extern "C"
4646
#endif
4747

4848
#endif // SWIFT_STDLIB_SHIMS_RUNTIMESTUBS_H_

stdlib/public/SwiftShims/UnicodeShims.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#endif
2727

2828
#ifdef __cplusplus
29-
namespace swift { extern "C" {
29+
extern "C" {
3030
#endif
3131

3232
SWIFT_RUNTIME_STDLIB_API
@@ -554,7 +554,7 @@ double __swift_stdlib_u_getNumericValue(__swift_stdlib_UChar32 c);
554554

555555

556556
#ifdef __cplusplus
557-
}} // extern "C", namespace swift
557+
} // extern "C"
558558
#endif
559559

560560
#if __has_feature(nullability)

stdlib/public/runtime/BackDeployment.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ _swift_classIsSwiftMask = computeIsSwiftMask();
3030
SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_END
3131
#endif // SWIFT_CLASS_IS_SWIFT_MASK_GLOBAL_VARIABLE
3232

33-
static swift::_SwiftNSOperatingSystemVersion swiftInOSVersion = {
33+
static _SwiftNSOperatingSystemVersion swiftInOSVersion = {
3434
#if __MAC_OS_X_VERSION_MIN_REQUIRED
3535
10, 14, 4
3636
// WatchOS also pretends to be iOS, so check it first.
@@ -43,8 +43,8 @@ static swift::_SwiftNSOperatingSystemVersion swiftInOSVersion = {
4343
#endif
4444
};
4545

46-
static bool versionLessThan(swift::_SwiftNSOperatingSystemVersion lhs,
47-
swift::_SwiftNSOperatingSystemVersion rhs) {
46+
static bool versionLessThan(_SwiftNSOperatingSystemVersion lhs,
47+
_SwiftNSOperatingSystemVersion rhs) {
4848
if (lhs.majorVersion < rhs.majorVersion) return true;
4949
if (lhs.majorVersion > rhs.majorVersion) return false;
5050

@@ -58,7 +58,7 @@ static bool versionLessThan(swift::_SwiftNSOperatingSystemVersion lhs,
5858

5959
SWIFT_RUNTIME_STDLIB_INTERNAL
6060
int _swift_isBackDeploying() {
61-
auto version = swift::_swift_stdlib_operatingSystemVersion();
61+
auto version = _swift_stdlib_operatingSystemVersion();
6262
return versionLessThan(version, swiftInOSVersion);
6363
}
6464
#endif

stdlib/public/stubs/Assert.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void logPrefixAndMessageToDebugger(
3939
free(debuggerMessage);
4040
}
4141

42-
void swift::_swift_stdlib_reportFatalErrorInFile(
42+
void _swift_stdlib_reportFatalErrorInFile(
4343
const unsigned char *prefix, int prefixLength,
4444
const unsigned char *message, int messageLength,
4545
const unsigned char *file, int fileLength,
@@ -61,7 +61,7 @@ void swift::_swift_stdlib_reportFatalErrorInFile(
6161
logPrefixAndMessageToDebugger(prefix, prefixLength, message, messageLength);
6262
}
6363

64-
void swift::_swift_stdlib_reportFatalError(
64+
void _swift_stdlib_reportFatalError(
6565
const unsigned char *prefix, int prefixLength,
6666
const unsigned char *message, int messageLength,
6767
uint32_t flags
@@ -78,7 +78,7 @@ void swift::_swift_stdlib_reportFatalError(
7878
logPrefixAndMessageToDebugger(prefix, prefixLength, message, messageLength);
7979
}
8080

81-
void swift::_swift_stdlib_reportUnimplementedInitializerInFile(
81+
void _swift_stdlib_reportUnimplementedInitializerInFile(
8282
const unsigned char *className, int classNameLength,
8383
const unsigned char *initName, int initNameLength,
8484
const unsigned char *file, int fileLength,
@@ -99,7 +99,7 @@ void swift::_swift_stdlib_reportUnimplementedInitializerInFile(
9999
free(log);
100100
}
101101

102-
void swift::_swift_stdlib_reportUnimplementedInitializer(
102+
void _swift_stdlib_reportUnimplementedInitializer(
103103
const unsigned char *className, int classNameLength,
104104
const unsigned char *initName, int initNameLength,
105105
uint32_t flags

stdlib/public/stubs/Availability.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static os_system_version_s getOSVersion() {
4747
/// This is ABI and cannot be removed. Even though _stdlib_isOSVersionAtLeast()
4848
/// is no longer inlinable, is previously was and so calls to this method
4949
/// have been inlined into shipped apps.
50-
_SwiftNSOperatingSystemVersion swift::_swift_stdlib_operatingSystemVersion() {
50+
_SwiftNSOperatingSystemVersion _swift_stdlib_operatingSystemVersion() {
5151
os_system_version_s version = SWIFT_LAZY_CONSTANT(getOSVersion());
5252

5353
return { (int)version.major, (int)version.minor, (int)version.patch };

stdlib/public/stubs/FoundationHelpers.mm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,26 @@ static inline void initializeBridgingFunctions() {
6666
}
6767

6868
__swift_uint8_t
69-
swift::_swift_stdlib_isNSString(id obj) {
69+
_swift_stdlib_isNSString(id obj) {
7070
initializeBridgingFunctions();
7171
return _CFGetTypeID((CFTypeRef)obj) == _CFStringTypeID ? 1 : 0;
7272
}
7373

7474
_swift_shims_CFHashCode
75-
swift::_swift_stdlib_CFStringHashNSString(id _Nonnull obj) {
75+
_swift_stdlib_CFStringHashNSString(id _Nonnull obj) {
7676
initializeBridgingFunctions();
7777
return _CFStringHashNSString(obj);
7878
}
7979

8080
_swift_shims_CFHashCode
81-
swift::_swift_stdlib_CFStringHashCString(const _swift_shims_UInt8 * _Nonnull bytes,
81+
_swift_stdlib_CFStringHashCString(const _swift_shims_UInt8 * _Nonnull bytes,
8282
_swift_shims_CFIndex length) {
8383
initializeBridgingFunctions();
8484
return _CFStringHashCString(bytes, length);
8585
}
8686

8787
const __swift_uint8_t *
88-
swift::_swift_stdlib_NSStringCStringUsingEncodingTrampoline(id _Nonnull obj,
88+
_swift_stdlib_NSStringCStringUsingEncodingTrampoline(id _Nonnull obj,
8989
unsigned long encoding) {
9090
typedef __swift_uint8_t * _Nullable (*cStrImplPtr)(id, SEL, unsigned long);
9191
cStrImplPtr imp = (cStrImplPtr)class_getMethodImplementation([obj superclass],
@@ -94,7 +94,7 @@ static inline void initializeBridgingFunctions() {
9494
}
9595

9696
__swift_uint8_t
97-
swift::_swift_stdlib_NSStringGetCStringTrampoline(id _Nonnull obj,
97+
_swift_stdlib_NSStringGetCStringTrampoline(id _Nonnull obj,
9898
_swift_shims_UInt8 *buffer,
9999
_swift_shims_CFIndex maxLength,
100100
unsigned long encoding) {
@@ -111,7 +111,7 @@ typedef __swift_uint8_t (*getCStringImplPtr)(id,
111111
}
112112

113113
__swift_uint8_t
114-
swift::_swift_stdlib_dyld_is_objc_constant_string(const void *addr) {
114+
_swift_stdlib_dyld_is_objc_constant_string(const void *addr) {
115115
initializeBridgingFunctions();
116116
if (!dyld_is_objc_constant) return false;
117117
return dyld_is_objc_constant(dyld_objc_string_kind, addr) ? 1 : 0;

stdlib/public/stubs/GlobalObjects.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ static swift::_SwiftHashingParameters initializeHashingParameters() {
118118
return { 0, 0, true };
119119
}
120120
__swift_uint64_t seed0 = 0, seed1 = 0;
121-
swift::swift_stdlib_random(&seed0, sizeof(seed0));
122-
swift::swift_stdlib_random(&seed1, sizeof(seed1));
121+
swift_stdlib_random(&seed0, sizeof(seed0));
122+
swift_stdlib_random(&seed1, sizeof(seed1));
123123
return { seed0, seed1, false };
124124
}
125125

stdlib/public/stubs/LibcShims.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@
3131

3232
#include "../SwiftShims/LibcShims.h"
3333

34-
using namespace swift;
35-
3634
#if !defined(_WIN32) || defined(__CYGWIN__)
37-
static_assert(std::is_same<mode_t, swift::__swift_mode_t>::value,
35+
static_assert(std::is_same<mode_t, __swift_mode_t>::value,
3836
"__swift_mode_t must be defined as equivalent to mode_t in LibcShims.h");
3937
#endif
4038

4139
SWIFT_RUNTIME_STDLIB_INTERNAL
42-
int swift::_swift_stdlib_putchar_unlocked(int c) {
40+
int _swift_stdlib_putchar_unlocked(int c) {
4341
#if defined(_WIN32)
4442
return _putc_nolock(c, stdout);
4543
#else
@@ -48,15 +46,15 @@ int swift::_swift_stdlib_putchar_unlocked(int c) {
4846
}
4947

5048
SWIFT_RUNTIME_STDLIB_INTERNAL
51-
__swift_size_t swift::_swift_stdlib_fwrite_stdout(const void *ptr,
49+
__swift_size_t _swift_stdlib_fwrite_stdout(const void *ptr,
5250
__swift_size_t size,
5351
__swift_size_t nitems) {
5452
return fwrite(ptr, size, nitems, stdout);
5553
}
5654

5755
SWIFT_RUNTIME_STDLIB_SPI
5856
__swift_ssize_t
59-
swift::_swift_stdlib_read(int fd, void *buf, __swift_size_t nbyte) {
57+
_swift_stdlib_read(int fd, void *buf, __swift_size_t nbyte) {
6058
#if defined(_WIN32)
6159
return _read(fd, buf, nbyte);
6260
#else
@@ -66,7 +64,7 @@ swift::_swift_stdlib_read(int fd, void *buf, __swift_size_t nbyte) {
6664

6765
SWIFT_RUNTIME_STDLIB_SPI
6866
__swift_ssize_t
69-
swift::_swift_stdlib_write(int fd, const void *buf, __swift_size_t nbyte) {
67+
_swift_stdlib_write(int fd, const void *buf, __swift_size_t nbyte) {
7068
#if defined(_WIN32)
7169
return _write(fd, buf, nbyte);
7270
#else
@@ -75,7 +73,7 @@ swift::_swift_stdlib_write(int fd, const void *buf, __swift_size_t nbyte) {
7573
}
7674

7775
SWIFT_RUNTIME_STDLIB_SPI
78-
int swift::_swift_stdlib_close(int fd) {
76+
int _swift_stdlib_close(int fd) {
7977
#if defined(_WIN32)
8078
return _close(fd);
8179
#else

stdlib/public/stubs/Random.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@
4444

4545
#include <algorithm> // required for std::min
4646

47+
using namespace swift;
48+
4749
#if defined(__APPLE__)
4850

4951
SWIFT_RUNTIME_STDLIB_API
50-
void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
52+
void swift_stdlib_random(void *buf, __swift_size_t nbytes) {
5153
arc4random_buf(buf, nbytes);
5254
}
5355

5456
#elif defined(_WIN32) && !defined(__CYGWIN__)
5557
#warning TODO: Test swift_stdlib_random on Windows
5658

5759
SWIFT_RUNTIME_STDLIB_API
58-
void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
60+
void swift_stdlib_random(void *buf, __swift_size_t nbytes) {
5961
if (nbytes > ULONG_MAX) {
6062
fatalError(0, "Fatal error: %zd exceeds ULONG_MAX\n", nbytes);
6163
}
@@ -79,7 +81,7 @@ void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
7981
}())
8082

8183
SWIFT_RUNTIME_STDLIB_API
82-
void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
84+
void swift_stdlib_random(void *buf, __swift_size_t nbytes) {
8385
while (nbytes > 0) {
8486
__swift_ssize_t actual_nbytes = -1;
8587

0 commit comments

Comments
 (0)