Repository: apple/swift
File: test/SILGen/foreach_borrowing.swift (Swift)

Code before:
    }
  }
}


// CHECK-LABEL: sil hidden [ossa] @$s17foreach_borrowing20testForEachLocations3seq3valys4SpanVySiG_SitF : $@convention(thin) (@guaranteed Span<Int>, Int) -> () {
func testForEachLocations(seq: borrowing Span<Int>, val: Int) {
  // Test that synthesized code has correct source locations for the borrowing foreach loop.
  // The borrowing foreach desugars to:
  //   let $x$generator = seq._makeBorrowingIterator()
  //   while true {
  //     let $span = $x$generator._nextSpan(maximumCount: Int.max)
  //     if !$span.isEmpty {
  //       let $i = 0
  //       let $count = $span.count
  //       while $i < $count, case let x = $span[$i] {
  //         $i = $i + 1
  //         if x == val {
  //           // body
  //         }
  //       }
  //     }
  //   }

  // _makeBorrowingIterator() function_ref should be at "for" keyword location (170:3)
  // CHECK: [[MAKE_BORROWING_IT:%.*]] = function_ref @$ss4SpanVsRi_zrlE22_makeBorrowingIteratorAByxGyF {{.*}}, loc "{{.*}}":170:3
  // CHECK: apply [[MAKE_BORROWING_IT]]{{.*}}, loc "{{.*}}":170:12

  // _nextSpan() function_ref should be at "for" keyword location (170:3)
  // CHECK: [[NEXT_SPAN:%.*]] = function_ref @$ss4SpanVsRi_zrlE05_nextA012maximumCountAByxGSi_tF {{.*}}, loc "{{.*}}":170:3
  // CHECK: apply [[NEXT_SPAN]]{{.*}}, loc "{{.*}}":170:3

  // $span debug_value should be at "for" keyword location (170:3)
  // CHECK: debug_value {{.*}}, let, name "$span", loc "{{.*}}":170:3

  // Outer loop condition (!$span.isEmpty)
  // CHECK: [[NOT_IS_EMPTY:%.*]] = function_ref @$sSb1nopyS2bFZ {{.*}}, loc "{{.*}}":170:3
  // CHECK: apply [[NOT_IS_EMPTY]]{{.*}}, loc "{{.*}}":170:3

  // Inner loop condition ($i < $count)
  // CHECK: function_ref @$sSi1loiySbSi_SitFZ  {{.*}}, loc "{{.*}}":170:3

  // Pattern variable "x" should be at its declaration location (170:7)
  // CHECK: debug_value {{.*}}, let, name "x", loc "{{.*}}":170:7

  // Where clause "==" operator function_ref
  // CHECK: [[EQUALS:%.*]] = function_ref @$sSi2eeoiySbSi_SitFZ {{.*}}, loc "{{.*}}":170:24
  // Where clause "==" operator apply
  // CHECK: apply [[EQUALS]]{{.*}}, loc "{{.*}}":170:24
  // Where clause condition extraction
  // CHECK: struct_extract {{.*}} : $Bool, #Bool._value, loc "{{.*}}":170:22

Review comment (suggestion):
Rather than checking for an absolute line number it would be better to use a relative line number such as `[[@LINE+29]]`, that way adding extra lines above and below this test doesn't affect it

Code after:
    }
  }
}


// CHECK-LABEL: sil hidden [ossa] @$s17foreach_borrowing20testForEachLocations3seq3valys4SpanVySiG_SitF : $@convention(thin) (@guaranteed Span<Int>, Int) -> () {
func testForEachLocations(seq: borrowing Span<Int>, val: Int) {
  // Test that synthesized code has correct source locations for the borrowing foreach loop.
  // The borrowing foreach desugars to:
  //   let $x$generator = seq._makeBorrowingIterator()
  //   while case let $span = $x$generator._nextSpan(maximumCount: Int.max),
  //      !$span.isEmpty {
  //       let $i = 0
  //       let $count = $span.count
  //       while $i < $count, case let x = $span[$i] {
  //         $i = $i + 1
  //         if x == val {
  //           // body
  //         }
  //       }
  //     }
  //   }

  // _makeBorrowingIterator() function_ref should be at "for" keyword location (172:3)
  // CHECK: [[MAKE_BORROWING_IT:%.*]] = function_ref @$ss4SpanVsRi_zrlE22_makeBorrowingIteratorAByxGyF {{.*}}, loc "{{.*}}":[[@LINE+31]]:3
  // CHECK: apply [[MAKE_BORROWING_IT]]{{.*}}, loc "{{.*}}":[[@LINE+30]]:18

  // _nextSpan() function_ref should be at "for" keyword location
  // CHECK: [[NEXT_SPAN:%.*]] = function_ref @$ss4SpanVsRi_zrlE05_nextA012maximumCountAByxGSi_tF {{.*}}, loc "{{.*}}":[[@LINE+27]]:3
  // CHECK: apply [[NEXT_SPAN]]{{.*}}, loc "{{.*}}":[[@LINE+26]]:3

  // $span debug_value should be at "for" keyword location
  // CHECK: debug_value {{.*}}, let, name "$span", loc "{{.*}}":[[@LINE+23]]:3

  // Outer loop condition (!$span.isEmpty)
  // CHECK: [[NOT_IS_EMPTY:%.*]] = function_ref @$sSb1nopyS2bFZ {{.*}}, loc "{{.*}}":[[@LINE+20]]:3
  // CHECK: apply [[NOT_IS_EMPTY]]{{.*}}, loc "{{.*}}":[[@LINE+19]]:3
  // CHECK: cond_br {{.*}}, loc "{{.*}}":[[@LINE+18]]:3

  // Inner loop condition ($i < $count)
  // CHECK: function_ref @$sSi1loiySbSi_SitFZ  {{.*}}, loc "{{.*}}":[[@LINE+15]]:3
  // CHECK: cond_br {{.*}}, loc "{{.*}}":[[@LINE+14]]:3

  // Pattern variable "x" should be at its declaration location
  // CHECK: debug_value {{.*}}, let, name "element", loc "{{.*}}":[[@LINE+11]]:7

  // Where clause "==" operator function_ref
  // CHECK: [[EQUALS:%.*]] = function_ref @$sSi2eeoiySbSi_SitFZ {{.*}}, loc "{{.*}}":[[@LINE+8]]:36
  // Where clause "==" operator apply
  // CHECK: apply [[EQUALS]]{{.*}}, loc "{{.*}}":[[@LINE+6]]:36
  // Where clause condition extraction