fail_addr_self.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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: note: 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: note: 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+9]]: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+6]]:3: note: type `Class*` does not implement interface `ImplicitAs`
  37. // CHECK:STDERR: (*p).G();
  38. // CHECK:STDERR: ^~~~~~~
  39. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE-31]]:13: note: initializing `addr self` parameter of method declared here
  40. // CHECK:STDERR: fn G[addr self: Class]();
  41. // CHECK:STDERR: ^~~~
  42. (*p).G();
  43. }
  44. // CHECK:STDOUT: --- fail_addr_self.carbon
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: constants {
  47. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  48. // CHECK:STDOUT: %.1: type = ptr_type %Class [template]
  49. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  50. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  51. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  52. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  53. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  54. // CHECK:STDOUT: %.3: type = struct_type {} [template]
  55. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  56. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  57. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  58. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  59. // CHECK:STDOUT: %ImplicitAs.type: type = generic_interface_type @ImplicitAs [template]
  60. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type = struct_value () [template]
  61. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic]
  62. // CHECK:STDOUT: %.6: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  63. // CHECK:STDOUT: %Self.1: @ImplicitAs.%.1 (%.6) = bind_symbolic_name Self 1 [symbolic]
  64. // CHECK:STDOUT: %Self.2: %.6 = bind_symbolic_name Self 1 [symbolic]
  65. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
  66. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  67. // CHECK:STDOUT: %.7: type = assoc_entity_type %.6, %Convert.type.1 [symbolic]
  68. // CHECK:STDOUT: %.8: %.7 = assoc_entity element0, imports.%import_ref.5 [symbolic]
  69. // CHECK:STDOUT: %.9: type = interface_type @ImplicitAs, @ImplicitAs(%Class) [template]
  70. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(%Class) [template]
  71. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  72. // CHECK:STDOUT: %.10: type = assoc_entity_type %.9, %Convert.type.2 [template]
  73. // CHECK:STDOUT: %.11: %.10 = assoc_entity element0, imports.%import_ref.5 [template]
  74. // CHECK:STDOUT: %.12: %.7 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: imports {
  78. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  79. // CHECK:STDOUT: .ImplicitAs = %import_ref.1
  80. // CHECK:STDOUT: import Core//prelude
  81. // CHECK:STDOUT: import Core//prelude/operators
  82. // CHECK:STDOUT: import Core//prelude/types
  83. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  84. // CHECK:STDOUT: import Core//prelude/operators/as
  85. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  86. // CHECK:STDOUT: import Core//prelude/operators/comparison
  87. // CHECK:STDOUT: import Core//prelude/types/bool
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %import_ref.1: %ImplicitAs.type = import_ref Core//prelude/operators/as, inst+40, loaded [template = constants.%ImplicitAs]
  90. // CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/as, inst+45, unloaded
  91. // CHECK:STDOUT: %import_ref.3: @ImplicitAs.%.2 (%.7) = import_ref Core//prelude/operators/as, inst+63, loaded [symbolic = @ImplicitAs.%.3 (constants.%.12)]
  92. // CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/as, inst+56, unloaded
  93. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+56, unloaded
  94. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+56, unloaded
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: file {
  98. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  99. // CHECK:STDOUT: .Core = imports.%Core
  100. // CHECK:STDOUT: .Class = %Class.decl
  101. // CHECK:STDOUT: .F = %F.decl
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %Core.import = import Core
  104. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  105. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  106. // CHECK:STDOUT: %c.patt: %Class = binding_pattern c
  107. // CHECK:STDOUT: %p.patt: %.1 = binding_pattern p
  108. // CHECK:STDOUT: } {
  109. // CHECK:STDOUT: %Class.ref.loc16_9: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  110. // CHECK:STDOUT: %c.param: %Class = param c, runtime_param0
  111. // CHECK:STDOUT: %c: %Class = bind_name c, %c.param
  112. // CHECK:STDOUT: %Class.ref.loc16_19: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  113. // CHECK:STDOUT: %.loc16: type = ptr_type %Class [template = constants.%.1]
  114. // CHECK:STDOUT: %p.param: %.1 = param p, runtime_param1
  115. // CHECK:STDOUT: %p: %.1 = bind_name p, %p.param
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  120. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: !definition:
  123. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.6)]
  124. // CHECK:STDOUT: %Self: %.6 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  125. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  126. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  127. // CHECK:STDOUT: %.2: type = assoc_entity_type @ImplicitAs.%.1 (%.6), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.2 (constants.%.7)]
  128. // CHECK:STDOUT: %.3: @ImplicitAs.%.2 (%.7) = assoc_entity element0, imports.%import_ref.5 [symbolic = %.3 (constants.%.8)]
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: interface {
  131. // CHECK:STDOUT: !members:
  132. // CHECK:STDOUT: .Self = imports.%import_ref.2
  133. // CHECK:STDOUT: .Convert = imports.%import_ref.3
  134. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: class @Class {
  139. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  140. // CHECK:STDOUT: %self.patt: %.1 = binding_pattern self
  141. // CHECK:STDOUT: } {
  142. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  143. // CHECK:STDOUT: %.loc12_24: type = ptr_type %Class [template = constants.%.1]
  144. // CHECK:STDOUT: %self.param: %.1 = param self, runtime_param0
  145. // CHECK:STDOUT: %self: %.1 = bind_name self, %self.param
  146. // CHECK:STDOUT: %.loc12_8: %.1 = addr_pattern %self
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  149. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  150. // CHECK:STDOUT: } {
  151. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  152. // CHECK:STDOUT: %self.param: %Class = param self, runtime_param0
  153. // CHECK:STDOUT: %self: %Class = bind_name self, %self.param
  154. // CHECK:STDOUT: %.loc13: %Class = addr_pattern %self
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.3 [template = constants.%.4]
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: !members:
  159. // CHECK:STDOUT: .Self = constants.%Class
  160. // CHECK:STDOUT: .F = %F.decl
  161. // CHECK:STDOUT: .G = %G.decl
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: fn @F.1[addr %self: %.1]();
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @G[addr %self: %Class]();
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: fn @F.2(%c: %Class, %p: %.1) {
  169. // CHECK:STDOUT: !entry:
  170. // CHECK:STDOUT: %c.ref.loc24: %Class = name_ref c, %c
  171. // CHECK:STDOUT: %F.ref.loc24: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
  172. // CHECK:STDOUT: %.loc24: <bound method> = bound_method %c.ref.loc24, %F.ref.loc24
  173. // CHECK:STDOUT: %F.call.loc24: init %.2 = call %.loc24(<invalid>) [template = <error>]
  174. // CHECK:STDOUT: %c.ref.loc33: %Class = name_ref c, %c
  175. // CHECK:STDOUT: %G.ref.loc33: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  176. // CHECK:STDOUT: %.loc33: <bound method> = bound_method %c.ref.loc33, %G.ref.loc33
  177. // CHECK:STDOUT: %G.call.loc33: init %.2 = call %.loc33(<invalid>) [template = <error>]
  178. // CHECK:STDOUT: %p.ref.loc36: %.1 = name_ref p, %p
  179. // CHECK:STDOUT: %.loc36_4.1: ref %Class = deref %p.ref.loc36
  180. // CHECK:STDOUT: %F.ref.loc36: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
  181. // CHECK:STDOUT: %.loc36_7: <bound method> = bound_method %.loc36_4.1, %F.ref.loc36
  182. // CHECK:STDOUT: %.loc36_4.2: %.1 = addr_of %.loc36_4.1
  183. // CHECK:STDOUT: %F.call.loc36: init %.2 = call %.loc36_7(%.loc36_4.2)
  184. // CHECK:STDOUT: %p.ref.loc47: %.1 = name_ref p, %p
  185. // CHECK:STDOUT: %.loc47_4.1: ref %Class = deref %p.ref.loc47
  186. // CHECK:STDOUT: %G.ref.loc47: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  187. // CHECK:STDOUT: %.loc47_7: <bound method> = bound_method %.loc47_4.1, %G.ref.loc47
  188. // CHECK:STDOUT: %.loc47_4.2: %.1 = addr_of %.loc47_4.1
  189. // CHECK:STDOUT: %.loc47_9.1: type = interface_type @ImplicitAs, @ImplicitAs(constants.%Class) [template = constants.%.9]
  190. // CHECK:STDOUT: %.loc47_9.2: %.10 = specific_constant imports.%import_ref.3, @ImplicitAs(constants.%Class) [template = constants.%.11]
  191. // CHECK:STDOUT: %Convert.ref: %.10 = name_ref Convert, %.loc47_9.2 [template = constants.%.11]
  192. // CHECK:STDOUT: %.loc47_9.3: %Class = converted %.loc47_4.2, <error> [template = <error>]
  193. // CHECK:STDOUT: %G.call.loc47: init %.2 = call %.loc47_7(<invalid>) [template = <error>]
  194. // CHECK:STDOUT: return
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%.1 (%.6)) {
  198. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  199. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.6)]
  200. // CHECK:STDOUT: %Self: %.6 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: fn[%self: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  206. // CHECK:STDOUT: %Dest => constants.%Dest
  207. // CHECK:STDOUT: }
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  210. // CHECK:STDOUT: %Dest => constants.%Dest
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  214. // CHECK:STDOUT: %Dest => constants.%Dest
  215. // CHECK:STDOUT: }
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  218. // CHECK:STDOUT: %Dest => constants.%Dest
  219. // CHECK:STDOUT: %.1 => constants.%.6
  220. // CHECK:STDOUT: %Self => constants.%Self.1
  221. // CHECK:STDOUT: }
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: specific @ImplicitAs(constants.%Class) {
  224. // CHECK:STDOUT: %Dest => constants.%Class
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: !definition:
  227. // CHECK:STDOUT: %.1 => constants.%.9
  228. // CHECK:STDOUT: %Self => constants.%Self.2
  229. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  230. // CHECK:STDOUT: %Convert => constants.%Convert.2
  231. // CHECK:STDOUT: %.2 => constants.%.10
  232. // CHECK:STDOUT: %.3 => constants.%.11
  233. // CHECK:STDOUT: }
  234. // CHECK:STDOUT: