@@ -370,7 +370,28 @@ extension ValueDefUseWalker {
370
370
// We need to ignore this because otherwise the path wouldn't contain the right `existential` field kind.
371
371
return leafUse ( value: operand, path: path)
372
372
}
373
- return walkDownUses ( ofValue: urc, path: path)
373
+ // The `unchecked_ref_cast` is designed to be able to cast between
374
+ // `Optional<ClassType>` and `ClassType`. We need to handle these
375
+ // cases by checking if the type is optional and adjust the path
376
+ // accordingly.
377
+ switch ( urc. type. isOptional, urc. fromInstance. type. isOptional) {
378
+ case ( true , false ) :
379
+ if walkDownUses ( ofValue: urc, path: path. push ( . enumCase, index: 0 ) ) == . abortWalk {
380
+ return . abortWalk
381
+ }
382
+ return walkDownUses ( ofValue: urc, path: path. push ( . enumCase, index: 1 ) )
383
+ case ( false , true ) :
384
+ if let path = path. popIfMatches ( . enumCase, index: 0 ) {
385
+ if walkDownUses ( ofValue: urc, path: path) == . abortWalk {
386
+ return . abortWalk
387
+ } else if let path = path. popIfMatches ( . enumCase, index: 1 ) {
388
+ return walkDownUses ( ofValue: urc, path: path)
389
+ }
390
+ }
391
+ return . abortWalk
392
+ default :
393
+ return walkDownUses ( ofValue: urc, path: path)
394
+ }
374
395
case let beginDealloc as BeginDeallocRefInst :
375
396
if operand. index == 0 {
376
397
return walkDownUses ( ofValue: beginDealloc, path: path)
@@ -699,7 +720,29 @@ extension ValueUseDefWalker {
699
720
// We need to ignore this because otherwise the path wouldn't contain the right `existential` field kind.
700
721
return rootDef ( value: urc, path: path)
701
722
}
702
- return walkUp ( value: urc. fromInstance, path: path)
723
+ // The `unchecked_ref_cast` is designed to be able to cast between
724
+ // `Optional<ClassType>` and `ClassType`. We need to handle these
725
+ // cases by checking if the type is optional and adjust the path
726
+ // accordingly.
727
+ switch ( urc. type. isOptional, urc. fromInstance. type. isOptional) {
728
+ case ( true , false ) :
729
+ if let path = path. popIfMatches ( . enumCase, index: 0 ) {
730
+ if walkUp ( value: urc. fromInstance, path: path) == . abortWalk {
731
+ return . abortWalk
732
+ } else if let path = path. popIfMatches ( . enumCase, index: 1 ) {
733
+ return walkUp ( value: urc. fromInstance, path: path)
734
+ }
735
+ }
736
+ return . abortWalk
737
+ case ( false , true ) :
738
+ if walkUp ( value: urc. fromInstance, path: path. push ( . enumCase, index: 0 ) ) == . abortWalk {
739
+ return . abortWalk
740
+ } else {
741
+ return walkUp ( value: urc. fromInstance, path: path. push ( . enumCase, index: 1 ) )
742
+ }
743
+ default :
744
+ return walkUp ( value: urc. fromInstance, path: path)
745
+ }
703
746
case let arg as Argument :
704
747
if let phi = Phi ( arg) {
705
748
for incoming in phi. incomingValues {
0 commit comments