10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
+ #include " swift/Basic/Lazy.h"
13
14
#include " swift/Runtime/Reflection.h"
14
15
#include " swift/Runtime/Casting.h"
15
16
#include " swift/Runtime/Config.h"
@@ -253,6 +254,47 @@ AnyReturn subscript(intptr_t i, const char **outName,
253
254
return AnyReturn (result);
254
255
}
255
256
};
257
+
258
+ struct swift_closure {
259
+ void *fptr;
260
+ HeapObject *context;
261
+ };
262
+ SWIFT_RUNTIME_STDLIB_API SWIFT_CC (swift) swift_closure
263
+ MANGLE_SYM(s20_playgroundPrintHookySScSgvg)();
264
+
265
+ static bool _shouldReportMissingReflectionMetadataWarnings () {
266
+ // Missing metadata warnings noise up playground sessions and aren't really
267
+ // actionable in playground contexts. If we're running in a playground,
268
+ // suppress warnings.
269
+ //
270
+ // Guesstimate whether we're in a playground by looking at the
271
+ // _playgroundPrintHook variable in the standard library, which is set during
272
+ // playground execution.
273
+ auto hook = MANGLE_SYM (s20_playgroundPrintHookySScSgvg)();
274
+ if (hook.fptr ) {
275
+ swift_release (hook.context );
276
+ return false ;
277
+ } else {
278
+ return true ;
279
+ }
280
+ }
281
+
282
+ // / Raise a warning about reflection metadata that could not be found
283
+ // / at runtime. This is usually mostly harmless, but it's good to alert
284
+ // / users that it happens.
285
+ static void
286
+ missing_reflection_metadata_warning (const char *fmt, ...) {
287
+ bool shouldWarn =
288
+ SWIFT_LAZY_CONSTANT (_shouldReportMissingReflectionMetadataWarnings ());
289
+
290
+ if (!shouldWarn)
291
+ return ;
292
+
293
+ va_list args;
294
+ va_start (args, fmt);
295
+
296
+ warningv (0 , fmt, args);
297
+ }
256
298
257
299
static std::pair<StringRef /* name*/ , FieldType /* fieldInfo*/ >
258
300
getFieldAt (const Metadata *base, unsigned index) {
@@ -262,9 +304,11 @@ AnyReturn subscript(intptr_t i, const char **outName,
262
304
// back to returning an empty tuple as a standin.
263
305
auto failedToFindMetadata = [&]() -> std::pair<StringRef, FieldType> {
264
306
auto typeName = swift_getTypeName (base, /* qualified*/ true );
265
- warning (0 , " SWIFT RUNTIME BUG: no field metadata for type '%*s' that claims "
266
- " to be reflectable\n " ,
267
- (int )typeName.length , typeName.data );
307
+ missing_reflection_metadata_warning (
308
+ " warning: the Swift runtime found no field metadata for "
309
+ " type '%*s' that claims to be reflectable. Its fields will show up as "
310
+ " 'unknown' in Mirrors\n " ,
311
+ (int )typeName.length , typeName.data );
268
312
return {" unknown" ,
269
313
FieldType ()
270
314
.withType (TypeInfo (&METADATA_SYM (EMPTY_TUPLE_MANGLING), {}))
@@ -331,10 +375,12 @@ AnyReturn subscript(intptr_t i, const char **outName,
331
375
// a log message.
332
376
if (typeInfo == nullptr ) {
333
377
typeInfo = TypeInfo (&METADATA_SYM (EMPTY_TUPLE_MANGLING), {});
334
- warning (0 , " SWIFT RUNTIME BUG: unable to demangle type of field '%*s'. "
335
- " mangled type name is '%*s'\n " ,
336
- (int )name.size (), name.data (),
337
- (int )typeName.size (), typeName.data ());
378
+ missing_reflection_metadata_warning (
379
+ " warning: the Swift runtime was unable to demangle the type "
380
+ " of field '%*s'. the mangled type name is '%*s'. this field will "
381
+ " show up as an empty tuple in Mirrors\n " ,
382
+ (int )name.size (), name.data (),
383
+ (int )typeName.size (), typeName.data ());
338
384
}
339
385
340
386
return {name, FieldType ()
0 commit comments