@@ -1115,6 +1115,47 @@ swift_getTypeByMangledNameImpl(const char *typeNameStart, size_t typeNameLength,
1115
1115
});
1116
1116
}
1117
1117
1118
+ struct swift_closure {
1119
+ void *fptr;
1120
+ HeapObject *context;
1121
+ };
1122
+ SWIFT_RUNTIME_STDLIB_API SWIFT_CC (swift) swift_closure
1123
+ MANGLE_SYM(s20_playgroundPrintHookySScSgvg)();
1124
+
1125
+ static bool _shouldReportMissingReflectionMetadataWarnings () {
1126
+ // Missing metadata warnings noise up playground sessions and aren't really
1127
+ // actionable in playground contexts. If we're running in a playground,
1128
+ // suppress warnings.
1129
+ //
1130
+ // Guesstimate whether we're in a playground by looking at the
1131
+ // _playgroundPrintHook variable in the standard library, which is set during
1132
+ // playground execution.
1133
+ auto hook = MANGLE_SYM (s20_playgroundPrintHookySScSgvg)();
1134
+ if (hook.fptr ) {
1135
+ swift_release (hook.context );
1136
+ return false ;
1137
+ } else {
1138
+ return true ;
1139
+ }
1140
+ }
1141
+
1142
+ // / Raise a warning about reflection metadata that could not be found
1143
+ // / at runtime. This is usually mostly harmless, but it's good to alert
1144
+ // / users that it happens.
1145
+ static void
1146
+ missing_reflection_metadata_warning (const char *fmt, ...) {
1147
+ bool shouldWarn =
1148
+ SWIFT_LAZY_CONSTANT (_shouldReportMissingReflectionMetadataWarnings ());
1149
+
1150
+ if (!shouldWarn)
1151
+ return ;
1152
+
1153
+ va_list args;
1154
+ va_start (args, fmt);
1155
+
1156
+ warningv (0 , fmt, args);
1157
+ }
1158
+
1118
1159
void swift::swift_getFieldAt (
1119
1160
const Metadata *base, unsigned index,
1120
1161
void (*callback)(const char *name, const Metadata *type, void *ctx), void *callbackCtx) {
@@ -1182,10 +1223,12 @@ void swift::_swift_getFieldAt(
1182
1223
// a log message.
1183
1224
if (typeInfo == nullptr ) {
1184
1225
typeInfo = TypeInfo (&METADATA_SYM (EMPTY_TUPLE_MANGLING), {});
1185
- warning (0 , " SWIFT RUNTIME BUG: unable to demangle type of field '%*s'. "
1186
- " mangled type name is '%*s'\n " ,
1187
- (int )name.size (), name.data (),
1188
- (int )typeName.size (), typeName.data ());
1226
+ missing_reflection_metadata_warning (
1227
+ " warning: the Swift runtime was unable to demangle the type "
1228
+ " of field '%*s'. the mangled type name is '%*s'. this field will "
1229
+ " show up as an empty tuple in Mirrors\n " ,
1230
+ (int )name.size (), name.data (),
1231
+ (int )typeName.size (), typeName.data ());
1189
1232
}
1190
1233
1191
1234
callback (name, FieldType ()
@@ -1235,8 +1278,11 @@ void swift::_swift_getFieldAt(
1235
1278
// If we failed to find the field descriptor metadata for the type, fall
1236
1279
// back to returning an empty tuple as a standin.
1237
1280
auto typeName = swift_getTypeName (base, /* qualified*/ true );
1238
- warning (0 , " SWIFT RUNTIME BUG: unable to find field metadata for type '%*s'\n " ,
1239
- (int )typeName.length , typeName.data );
1281
+ missing_reflection_metadata_warning (
1282
+ " warning: the Swift runtime found no field metadata for "
1283
+ " type '%*s' that claims to be reflectable. Its fields will show up as "
1284
+ " 'unknown' in Mirrors\n " ,
1285
+ (int )typeName.length , typeName.data );
1240
1286
callback (" unknown" ,
1241
1287
FieldType ()
1242
1288
.withType (TypeInfo (&METADATA_SYM (EMPTY_TUPLE_MANGLING), {}))
0 commit comments