Skip to content

Commit b5ace61

Browse files
authored
Merge pull request #63097 from gottesmm/pr-2ede5f5d83cffb7871481e39ed42760c0592bd0a
[borrow-expr] Add tests for globals, self in methods, generic self in methods
2 parents 29c1a42 + c97d338 commit b5ace61

File tree

1 file changed

+203
-2
lines changed

1 file changed

+203
-2
lines changed

test/SILGen/borrow_expr.swift

+203-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ protocol P {
189189
var q: Q { get /*_read*/ }
190190
func doSomething()
191191
func doSomething(_ k: Klass)
192+
193+
mutating func mutatingExtensionPointCallMethodOnSelf()
194+
mutating func mutatingExtensionPointPassSelfAsArg()
192195
}
193196

194197
func usePExistential(_ p: P) {}
@@ -238,7 +241,7 @@ func simpleTestGenericArgField<T : P>(_ pArg: T) {
238241

239242
// Without borrow.
240243
useQGeneric(p.q)
241-
244+
242245
// With borrow.
243246
//
244247
// TODO: This doesn't work now. We should support this potentially for
@@ -287,10 +290,208 @@ func simpleTestExistentialArgField(_ pArg: P) {
287290

288291
// Without borrow.
289292
useQGeneric(p.q)
290-
293+
291294
// With borrow.
292295
//
293296
// TODO: This doesn't work now. We should support this potentially for
294297
// _read. But protocols seem to not support _read at this time.
295298
// useQGeneric(_borrow p.q)
296299
}
300+
301+
/////////////
302+
// Globals //
303+
/////////////
304+
305+
var globalKlass = Klass()
306+
307+
// CHECK-LABEL: sil hidden [ossa] @$s11borrow_expr0A6GlobalyyF : $@convention(thin) () -> () {
308+
// CHECK: [[ADDR:%.*]] = global_addr @$s11borrow_expr11globalKlassAA0D0Cvp
309+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[ADDR]]
310+
// CHECK: [[VAL:%.*]] = load_borrow [[ACCESS]]
311+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr5KlassC11doSomethingyyF : $@convention(method) (@guaranteed Klass) -> ()
312+
// CHECK: apply [[FUNC]]([[VAL]]
313+
// CHECK: end_borrow [[VAL]]
314+
// CHECK: end_access [[ACCESS]]
315+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[ADDR]]
316+
// CHECK: [[VAL:%.*]] = load_borrow [[ACCESS]]
317+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr8useKlassyyAA0D0CF : $@convention(thin) (@guaranteed Klass) -> ()
318+
// CHECK: apply [[FUNC]]([[VAL]])
319+
// CHECK: end_borrow [[VAL]]
320+
// CHECK: end_access [[ACCESS]]
321+
// CHECK: } // end sil function '$s11borrow_expr0A6GlobalyyF'
322+
func borrowGlobal() {
323+
(_borrow globalKlass).doSomething()
324+
useKlass(_borrow globalKlass)
325+
}
326+
327+
/////////////
328+
// Methods //
329+
/////////////
330+
331+
struct MethodTestingStruct {
332+
var k = Klass()
333+
334+
func doSomething() {}
335+
336+
// CHECK-LABEL: sil hidden [ossa] @$s11borrow_expr19MethodTestingStructV012mutatingCallC6OnSelfyyF : $@convention(method) (@inout MethodTestingStruct) -> () {
337+
// CHECK: bb0([[SELF:%.*]] :
338+
//
339+
// Without borrow
340+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[SELF]]
341+
// CHECK: [[VAL:%.*]] = load [copy] [[ACCESS]]
342+
// CHECK: end_access [[ACCESS]]
343+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr19MethodTestingStructV11doSomethingyyF : $@convention(method) (@guaranteed MethodTestingStruct) -> ()
344+
// CHECK: apply [[FUNC]]([[VAL]])
345+
// CHECK: destroy_value [[VAL]]
346+
//
347+
// With borrow
348+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[SELF]]
349+
// CHECK: [[VAL:%.*]] = load_borrow [[ACCESS]]
350+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr19MethodTestingStructV11doSomethingyyF : $@convention(method) (@guaranteed MethodTestingStruct) -> ()
351+
// CHECK: apply [[FUNC]]([[VAL]])
352+
// CHECK: end_borrow [[VAL]]
353+
// CHECK: end_access [[ACCESS]]
354+
// CHECK: } // end sil function '$s11borrow_expr19MethodTestingStructV012mutatingCallC6OnSelfyyF'
355+
mutating func mutatingCallMethodOnSelf() {
356+
// Without borrow
357+
doSomething()
358+
// With borrow
359+
(_borrow self).doSomething()
360+
}
361+
362+
// CHECK-LABEL: sil hidden [ossa] @$s11borrow_expr19MethodTestingStructV26mutatingPassSelfAsArgumentyyF : $@convention(method) (@inout MethodTestingStruct) -> () {
363+
// CHECK: bb0([[SELF:%.*]] :
364+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[SELF]]
365+
// CHECK: [[VAL:%.*]] = load [copy] [[ACCESS]]
366+
// CHECK: end_access [[ACCESS]]
367+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr22useMethodTestingStructyyAA0deF0VF : $@convention(thin) (@guaranteed MethodTestingStruct) -> ()
368+
// CHECK: apply [[FUNC]]([[VAL]])
369+
// CHECK: destroy_value [[VAL]]
370+
//
371+
// With borrow
372+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[SELF]]
373+
// CHECK: [[VAL:%.*]] = load_borrow [[ACCESS]]
374+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr22useMethodTestingStructyyAA0deF0VF : $@convention(thin) (@guaranteed MethodTestingStruct) -> ()
375+
// CHECK: apply [[FUNC]]([[VAL]])
376+
// CHECK: end_borrow [[VAL]]
377+
// CHECK: end_access [[ACCESS]]
378+
// CHECK: } // end sil function '$s11borrow_expr19MethodTestingStructV26mutatingPassSelfAsArgumentyyF'
379+
mutating func mutatingPassSelfAsArgument() {
380+
// Without borrow
381+
useMethodTestingStruct(self)
382+
// With borrow
383+
useMethodTestingStruct(_borrow self)
384+
}
385+
}
386+
387+
func useMethodTestingStruct(_ x: MethodTestingStruct) {}
388+
389+
extension P {
390+
// CHECK-LABEL: sil hidden [ossa] @$s11borrow_expr1PPAAE27mutatingCallingMethodOnSelfyyF : $@convention(method) <Self where Self : P> (@inout Self) -> () {
391+
// CHECK: bb0([[ADDR:%.*]] :
392+
//
393+
// First without borrow.
394+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
395+
// CHECK: [[STACK:%.*]] = alloc_stack $Self
396+
// CHECK: copy_addr [[ACCESS]] to [init] [[STACK]]
397+
// CHECK: end_access [[ACCESS]]
398+
// CHECK: [[FUNC:%.*]] = witness_method $Self, #P.doSomething : <Self where Self : P> (Self) -> () -> () : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
399+
// CHECK: apply [[FUNC]]<Self>([[STACK]])
400+
// CHECK: destroy_addr [[STACK]]
401+
// CHECK: dealloc_stack [[STACK]]
402+
//
403+
// Now with borrow
404+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
405+
// CHECK: [[FUNC:%.*]] = witness_method $Self, #P.doSomething : <Self where Self : P> (Self) -> () -> () : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
406+
// CHECK: apply [[FUNC]]<(Self)>([[ACCESS]])
407+
// CHECK: end_access [[ACCESS]]
408+
// CHECK: } // end sil function '$s11borrow_expr1PPAAE27mutatingCallingMethodOnSelfyyF'
409+
mutating func mutatingCallingMethodOnSelf() {
410+
// Without borrow
411+
doSomething()
412+
413+
// With borrow
414+
(_borrow self).doSomething()
415+
}
416+
417+
// CHECK-LABEL: sil hidden [ossa] @$s11borrow_expr1PPAAE26mutatingPassSelfAsArgumentyyF : $@convention(method) <Self where Self : P> (@inout Self) -> () {
418+
// CHECK: bb0([[ADDR:%.*]] :
419+
//
420+
// First without borrow.
421+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
422+
// CHECK: [[STACK:%.*]] = alloc_stack $Self
423+
// CHECK: copy_addr [[ACCESS]] to [init] [[STACK]]
424+
// CHECK: end_access [[ACCESS]]
425+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr11usePGenericyyxAA1PRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
426+
// CHECK: apply [[FUNC]]<Self>([[STACK]])
427+
// CHECK: destroy_addr [[STACK]]
428+
// CHECK: dealloc_stack [[STACK]]
429+
//
430+
// Now with borrow
431+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
432+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr11usePGenericyyxAA1PRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
433+
// CHECK: apply [[FUNC]]<Self>([[ACCESS]])
434+
// CHECK: end_access [[ACCESS]]
435+
// CHECK: } // end sil function '$s11borrow_expr1PPAAE26mutatingPassSelfAsArgumentyyF'
436+
mutating func mutatingPassSelfAsArgument() {
437+
// Without borrow
438+
usePGeneric(self)
439+
440+
// With borrow
441+
usePGeneric(_borrow self)
442+
}
443+
444+
// CHECK-LABEL: sil hidden [ossa] @$s11borrow_expr1PPAAE38mutatingExtensionPointCallMethodOnSelfyyF : $@convention(method) <Self where Self : P> (@inout Self) -> () {
445+
// CHECK: bb0([[ADDR:%.*]] :
446+
//
447+
// First without borrow.
448+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
449+
// CHECK: [[STACK:%.*]] = alloc_stack $Self
450+
// CHECK: copy_addr [[ACCESS]] to [init] [[STACK]]
451+
// CHECK: end_access [[ACCESS]]
452+
// CHECK: [[FUNC:%.*]] = witness_method $Self, #P.doSomething : <Self where Self : P> (Self) -> () -> () : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
453+
// CHECK: apply [[FUNC]]<Self>([[STACK]])
454+
// CHECK: destroy_addr [[STACK]]
455+
// CHECK: dealloc_stack [[STACK]]
456+
//
457+
// Now with borrow
458+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
459+
// CHECK: [[FUNC:%.*]] = witness_method $Self, #P.doSomething : <Self where Self : P> (Self) -> () -> () : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
460+
// CHECK: apply [[FUNC]]<(Self)>([[ACCESS]])
461+
// CHECK: end_access [[ACCESS]]
462+
// CHECK: } // end sil function '$s11borrow_expr1PPAAE38mutatingExtensionPointCallMethodOnSelfyyF'
463+
mutating func mutatingExtensionPointCallMethodOnSelf() {
464+
// Without borrow
465+
doSomething()
466+
467+
// With borrow
468+
(_borrow self).doSomething()
469+
}
470+
471+
// CHECK-LABEL: sil hidden [ossa] @$s11borrow_expr1PPAAE35mutatingExtensionPointPassSelfAsArgyyF : $@convention(method) <Self where Self : P> (@inout Self) -> () {
472+
// CHECK: bb0([[ADDR:%.*]] :
473+
//
474+
// First without borrow.
475+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
476+
// CHECK: [[STACK:%.*]] = alloc_stack $Self
477+
// CHECK: copy_addr [[ACCESS]] to [init] [[STACK]]
478+
// CHECK: end_access [[ACCESS]]
479+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr11usePGenericyyxAA1PRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
480+
// CHECK: apply [[FUNC]]<Self>([[STACK]])
481+
// CHECK: destroy_addr [[STACK]]
482+
// CHECK: dealloc_stack [[STACK]]
483+
//
484+
// Now with borrow
485+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unknown] [[ADDR]]
486+
// CHECK: [[FUNC:%.*]] = function_ref @$s11borrow_expr11usePGenericyyxAA1PRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
487+
// CHECK: apply [[FUNC]]<Self>([[ACCESS]])
488+
// CHECK: end_access [[ACCESS]]
489+
// CHECK: } // end sil function '$s11borrow_expr1PPAAE35mutatingExtensionPointPassSelfAsArgyyF'
490+
mutating func mutatingExtensionPointPassSelfAsArg() {
491+
// Without borrow
492+
usePGeneric(self)
493+
494+
// With borrow
495+
usePGeneric(_borrow self)
496+
}
497+
}

0 commit comments

Comments
 (0)