fail_addr_self.carbon 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_addr_self.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_addr_self.carbon
  10. class Class {
  11. fn F[addr self: Class*]();
  12. fn G[addr self: Class]();
  13. }
  14. fn F(c: Class, p: Class*) {
  15. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE+7]]:6: ERROR: `addr self` method cannot be invoked on a value.
  16. // CHECK:STDERR: c.F();
  17. // CHECK:STDERR: ^
  18. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE-8]]:13: Initializing `addr self` parameter of method declared here.
  19. // CHECK:STDERR: fn F[addr self: Class*]();
  20. // CHECK:STDERR: ^~~~
  21. // CHECK:STDERR:
  22. c.F();
  23. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE+7]]:6: ERROR: `addr self` method cannot be invoked on a value.
  24. // CHECK:STDERR: c.G();
  25. // CHECK:STDERR: ^
  26. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE-16]]:13: Initializing `addr self` parameter of method declared here.
  27. // CHECK:STDERR: fn G[addr self: Class]();
  28. // CHECK:STDERR: ^~~~
  29. // CHECK:STDERR:
  30. c.G();
  31. // This call is OK.
  32. (*p).F();
  33. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE+6]]:3: ERROR: Cannot implicitly convert from `Class*` to `Class`.
  34. // CHECK:STDERR: (*p).G();
  35. // CHECK:STDERR: ^~~~~~~
  36. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE-28]]:13: Initializing `addr self` parameter of method declared here.
  37. // CHECK:STDERR: fn G[addr self: Class]();
  38. // CHECK:STDERR: ^~~~
  39. (*p).G();
  40. }
  41. // CHECK:STDOUT: --- fail_addr_self.carbon
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: constants {
  44. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  45. // CHECK:STDOUT: %.1: type = ptr_type %Class [template]
  46. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  47. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  48. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  49. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  50. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  51. // CHECK:STDOUT: %.3: type = struct_type {} [template]
  52. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  53. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  54. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: file {
  58. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  59. // CHECK:STDOUT: .Core = %Core
  60. // CHECK:STDOUT: .Class = %Class.decl
  61. // CHECK:STDOUT: .F = %F.decl
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  64. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  65. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  66. // CHECK:STDOUT: %Class.ref.loc16_9: type = name_ref Class, %Class.decl [template = constants.%Class]
  67. // CHECK:STDOUT: %c.loc16_6.1: %Class = param c
  68. // CHECK:STDOUT: @F.2.%c: %Class = bind_name c, %c.loc16_6.1
  69. // CHECK:STDOUT: %Class.ref.loc16_19: type = name_ref Class, %Class.decl [template = constants.%Class]
  70. // CHECK:STDOUT: %.loc16: type = ptr_type %Class [template = constants.%.1]
  71. // CHECK:STDOUT: %p.loc16_16.1: %.1 = param p
  72. // CHECK:STDOUT: @F.2.%p: %.1 = bind_name p, %p.loc16_16.1
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: class @Class {
  77. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  78. // CHECK:STDOUT: %Class.ref.loc12: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  79. // CHECK:STDOUT: %.loc12_24: type = ptr_type %Class [template = constants.%.1]
  80. // CHECK:STDOUT: %self.loc12_13.1: %.1 = param self
  81. // CHECK:STDOUT: %self.loc12_13.3: %.1 = bind_name self, %self.loc12_13.1
  82. // CHECK:STDOUT: %.loc12_8: %.1 = addr_pattern %self.loc12_13.3
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  85. // CHECK:STDOUT: %Class.ref.loc13: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  86. // CHECK:STDOUT: %self.loc13_13.1: %Class = param self
  87. // CHECK:STDOUT: %self.loc13_13.3: %Class = bind_name self, %self.loc13_13.1
  88. // CHECK:STDOUT: %.loc13: %Class = addr_pattern %self.loc13_13.3
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: !members:
  92. // CHECK:STDOUT: .Self = constants.%Class
  93. // CHECK:STDOUT: .F = %F.decl
  94. // CHECK:STDOUT: .G = %G.decl
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: fn @F.1[addr @Class.%self.loc12_13.3: %.1]();
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: fn @G[addr @Class.%self.loc13_13.3: %Class]();
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: fn @F.2(%c: %Class, %p: %.1) {
  102. // CHECK:STDOUT: !entry:
  103. // CHECK:STDOUT: %c.ref.loc24: %Class = name_ref c, %c
  104. // CHECK:STDOUT: %F.ref.loc24: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
  105. // CHECK:STDOUT: %.loc24: <bound method> = bound_method %c.ref.loc24, %F.ref.loc24
  106. // CHECK:STDOUT: %F.call.loc24: init %.2 = call %.loc24(<invalid>) [template = <error>]
  107. // CHECK:STDOUT: %c.ref.loc33: %Class = name_ref c, %c
  108. // CHECK:STDOUT: %G.ref.loc33: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  109. // CHECK:STDOUT: %.loc33: <bound method> = bound_method %c.ref.loc33, %G.ref.loc33
  110. // CHECK:STDOUT: %G.call.loc33: init %.2 = call %.loc33(<invalid>) [template = <error>]
  111. // CHECK:STDOUT: %p.ref.loc36: %.1 = name_ref p, %p
  112. // CHECK:STDOUT: %.loc36_4.1: ref %Class = deref %p.ref.loc36
  113. // CHECK:STDOUT: %F.ref.loc36: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
  114. // CHECK:STDOUT: %.loc36_7: <bound method> = bound_method %.loc36_4.1, %F.ref.loc36
  115. // CHECK:STDOUT: %.loc36_4.2: %.1 = addr_of %.loc36_4.1
  116. // CHECK:STDOUT: %F.call.loc36: init %.2 = call %.loc36_7(%.loc36_4.2)
  117. // CHECK:STDOUT: %p.ref.loc44: %.1 = name_ref p, %p
  118. // CHECK:STDOUT: %.loc44_4.1: ref %Class = deref %p.ref.loc44
  119. // CHECK:STDOUT: %G.ref.loc44: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  120. // CHECK:STDOUT: %.loc44_7: <bound method> = bound_method %.loc44_4.1, %G.ref.loc44
  121. // CHECK:STDOUT: %.loc44_4.2: %.1 = addr_of %.loc44_4.1
  122. // CHECK:STDOUT: %G.call.loc44: init %.2 = call %.loc44_7(<invalid>) [template = <error>]
  123. // CHECK:STDOUT: return
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: