@@ -276,6 +276,26 @@ internal func _getEnumCaseName<T>(_ value: T) -> UnsafePointer<CChar>?
276
276
@_silgen_name ( " swift_OpaqueSummary " )
277
277
internal func _opaqueSummary( _ metadata: Any . Type ) -> UnsafePointer < CChar > ?
278
278
279
+ /// Obtain a fallback raw value for an enum type without metadata; this
280
+ /// should be OK for enums from C/C++ until they have metadata (see
281
+ /// <rdar://22036374>). Note that if this turns out to be a Swift Enum
282
+ /// with missing metadata, the raw value may be misleading.
283
+ @_semantics ( " optimize.sil.specialize.generic.never " )
284
+ internal func _fallbackEnumRawValue< T > ( _ value: T ) -> Int64 ? {
285
+ switch MemoryLayout . size ( ofValue: value) {
286
+ case 8 :
287
+ return unsafeBitCast ( value, to: Int64 . self)
288
+ case 4 :
289
+ return Int64 ( unsafeBitCast ( value, to: Int32 . self) )
290
+ case 2 :
291
+ return Int64 ( unsafeBitCast ( value, to: Int16 . self) )
292
+ case 1 :
293
+ return Int64 ( unsafeBitCast ( value, to: Int8 . self) )
294
+ default :
295
+ return nil
296
+ }
297
+ }
298
+
279
299
/// Do our best to print a value that cannot be printed directly.
280
300
@_semantics ( " optimize.sil.specialize.generic.never " )
281
301
internal func _adHocPrint_unlocked< T, TargetStream: TextOutputStream > (
@@ -342,8 +362,14 @@ internal func _adHocPrint_unlocked<T, TargetStream: TextOutputStream>(
342
362
}
343
363
target. write ( caseName)
344
364
} else {
345
- // If the case name is garbage, just print the type name.
346
365
printTypeName ( mirror. subjectType)
366
+ // The case name is garbage; this might be a C/C++ enum without
367
+ // metadata, so see if we can get a raw value
368
+ if let rawValue = _fallbackEnumRawValue ( value) {
369
+ target. write ( " (rawValue: " )
370
+ _debugPrint_unlocked ( rawValue, & target) ;
371
+ target. write ( " ) " )
372
+ }
347
373
}
348
374
if let ( _, value) = mirror. children. first {
349
375
if Mirror ( reflecting: value) . displayStyle == . tuple {
0 commit comments