@@ -370,7 +370,28 @@ extension ValueDefUseWalker {
370370 // We need to ignore this because otherwise the path wouldn't contain the right `existential` field kind.
371371 return leafUse ( value: operand, path: path)
372372 }
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+ }
374395 case let beginDealloc as BeginDeallocRefInst :
375396 if operand. index == 0 {
376397 return walkDownUses ( ofValue: beginDealloc, path: path)
@@ -699,7 +720,29 @@ extension ValueUseDefWalker {
699720 // We need to ignore this because otherwise the path wouldn't contain the right `existential` field kind.
700721 return rootDef ( value: urc, path: path)
701722 }
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+ }
703746 case let arg as Argument :
704747 if let phi = Phi ( arg) {
705748 for incoming in phi. incomingValues {
0 commit comments