Files
swift-mirror/test/SILGen/switch_isa.swift
Vedant Kumar 60ec3f1b90 Fix debug description for cases with multiple items (#32282)
* [SILGenFunction] Don't create redundant nested debug scopes

Instead of emitting:

```
sil_scope 4 { loc "main.swift":6:19 parent 3 }
sil_scope 5 { loc "main.swift":7:3 parent 4 }
sil_scope 6 { loc "main.swift":7:3 parent 5 }
sil_scope 7 { loc "main.swift":7:3 parent 5 }
sil_scope 8 { loc "main.swift":9:5 parent 4 }
```

Emit:

```
sil_scope 4 { loc "main.swift":6:19 parent 3 }
sil_scope 5 { loc "main.swift":7:3 parent 4 }
sil_scope 6 { loc "main.swift":9:5 parent 5 }
```

* [IRGenSIL] Diagnose conflicting shadow copies

If we attempt to store a value with the wrong type into a slot reserved
for a shadow copy, diagnose what went wrong.

* [SILGenPattern] Defer debug description of case variables

Create unique nested debug scopes for a switch, each of its case labels,
and each of its case bodies. This looks like:

```
  switch ... { // Enter scope 1.
    case ... : // Enter scope 2, nested within scope 1.
      <body-1> // Enter scope 3, nested within scope 2.

    case ... : // Enter scope 4, nested within scope 1.
      <body-2> // Enter scope 5, nested within scope 4.
  }
```

Use the new scope structure to defer emitting debug descriptions of case
bindings. Specifically, defer the work until we can nest the scope for a
case body under the scope for a pattern match.

This fixes SR-7973, a problem where it was impossible to inspect a case
binding in lldb when stopped at a case with multiple items.

Previously, we would emit the debug descriptions too early (in the
pattern match), leading to duplicate/conflicting descriptions. The only
reason that the ambiguous description was allowed to compile was because
the debug scopes were nested incorrectly.

rdar://41048339

* Update tests
2020-06-10 13:31:10 -07:00

102 lines
3.4 KiB
Swift

// RUN: %target-swift-emit-silgen -module-name switch_isa %s | %FileCheck %s
func markUsed<T>(_ t: T) {}
// rdar://17772217
func testSwitchOnExistential(_ value: Any) {
switch value {
case true as Bool: markUsed("true")
case false as Bool: markUsed("false")
default: markUsed("default")
}
}
// CHECK-LABEL: sil hidden [ossa] @$s10switch_isa23testSwitchOnExistentialyyypF :
// CHECK: [[ANY:%.*]] = alloc_stack $Any
// CHECK: copy_addr %0 to [initialization] [[ANY]]
// CHECK: [[BOOL:%.*]] = alloc_stack $Bool
// CHECK: checked_cast_addr_br copy_on_success Any in [[ANY]] : $*Any to Bool in [[BOOL]] : $*Bool, [[IS_BOOL:bb[0-9]+]], [[IS_NOT_BOOL:bb[0-9]+]]
// CHECK: [[IS_BOOL]]:
// CHECK: [[T0:%.*]] = load [trivial] [[BOOL]]
enum Foo {
case A
}
enum Bar<T> {
case B(T)
}
func testSwitchEnumOnExistential(_ value: Any) {
switch value {
case Foo.A:
()
case Bar<Int>.B(let i):
()
case Bar<Foo>.B(let f):
()
default:
()
}
}
// CHECK-LABEL: sil hidden [ossa] @$s10switch_isa27testSwitchEnumOnExistentialyyypF : $@convention(thin) (@in_guaranteed Any) -> ()
// CHECK: checked_cast_addr_br copy_on_success Any in {{%.*}} : $*Any to Foo
// CHECK: checked_cast_addr_br copy_on_success Any in {{%.*}} : $*Any to Bar<Int>
// CHECK: checked_cast_addr_br copy_on_success Any in {{%.*}} : $*Any to Bar<Foo>
class B {}
class D: B {}
func guardFn(_ l: D, _ r: D) -> Bool { return true }
// rdar://problem/21087371
// CHECK-LABEL: sil hidden [ossa] @$s10switch_isa32testSwitchTwoIsPatternsWithGuard_1ryAA1BC_AEtF
// CHECK: bb0([[ARG0:%.*]] : @guaranteed $B, [[ARG1:%.*]] : @guaranteed $B):
// CHECK: [[ARG0_COPY:%.*]] = copy_value [[ARG0]]
// CHECK: [[ARG1_COPY:%.*]] = copy_value [[ARG1]]
// CHECK: [[TUP:%.*]] = tuple ([[ARG0_COPY:%.*]] : $B, [[ARG1_COPY:%.*]] : $B)
// CHECK: [[BORROWED_TUP:%.*]] = begin_borrow [[TUP]]
// CHECK: ([[TUP_1:%.*]], [[TUP_2:%.*]]) = destructure_tuple [[BORROWED_TUP]]
// CHECK: checked_cast_br [[TUP_1]] : $B to D, [[R_CAST_YES:bb[0-9]+]], [[R_CAST_NO:bb[0-9]+]]
//
// CHECK: [[R_CAST_YES]]([[R:%.*]] : @guaranteed $D):
// CHECK: [[R2:%.*]] = copy_value [[R]]
// CHECK: checked_cast_br [[TUP_2]] : $B to D, [[L_CAST_YES:bb[0-9]+]], [[L_CAST_NO:bb[0-9]+]]
//
// CHECK: [[L_CAST_YES]]([[L:%.*]] : @guaranteed $D):
// CHECK: [[L2:%.*]] = copy_value [[L]]
// CHECK: function_ref @$s10switch_isa7guardFnySbAA1DC_ADtF
// CHECK: cond_br {{%.*}}, [[GUARD_YES:bb[0-9]+]], [[GUARD_NO:bb[0-9]+]]
//
// CHECK: [[GUARD_YES]]:
// CHECK-NEXT: debug_value [[R2]]
// CHECK-NEXT: debug_value [[L2]]
// CHECK-NEXT: destroy_value [[L2]]
// CHECK-NEXT: destroy_value [[R2]]
// CHECK-NEXT: end_borrow [[BORROWED_TUP]]
// CHECK-NEXT: destroy_value [[TUP]]
// CHECK-NEXT: br [[EXIT:bb[0-9]+]]
//
// CHECK: [[GUARD_NO]]:
// CHECK-NEXT: destroy_value [[L2]]
// CHECK-NEXT: destroy_value [[R2]]
// CHECK-NEXT: end_borrow [[BORROWED_TUP]]
// CHECK-NEXT: br [[CONT:bb[0-9]+]]
//
// CHECK: [[L_CAST_NO]]([[LFAIL:%.*]] : @guaranteed $B):
// CHECK-NEXT: destroy_value [[R2]]
// CHECK-NEXT: end_borrow [[BORROWED_TUP]]
// CHECK-NEXT: br [[CONT]]
//
// CHECK: [[CONT]]:
// CHECK-NEXT: destroy_value [[TUP]]
// CHECK-NEXT: br [[EXIT]]
func testSwitchTwoIsPatternsWithGuard(_ l: B, r: B) {
switch (l, r) {
case (let l2 as D, let r2 as D) where guardFn(l2, r2):
break
default:
break
}
}