fail_addr_self.carbon 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE+4]]:8: error: `addr` can only be applied to a binding with a pointer type
  13. // CHECK:STDERR: fn G[addr self: Class]();
  14. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  15. // CHECK:STDERR:
  16. fn G[addr self: Class]();
  17. }
  18. fn F(c: Class, p: Class*) {
  19. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE+6]]:3: error: `addr self` method cannot be invoked on a value
  20. // CHECK:STDERR: c.F();
  21. // CHECK:STDERR: ^
  22. // CHECK:STDERR: fail_addr_self.carbon:[[@LINE-12]]:8: note: initializing `addr self` parameter of method declared here
  23. // CHECK:STDERR: fn F[addr self: Class*]();
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  25. c.F();
  26. c.G();
  27. // This call is OK.
  28. (*p).F();
  29. (*p).G();
  30. }
  31. // CHECK:STDOUT: --- fail_addr_self.carbon
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: constants {
  34. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  35. // CHECK:STDOUT: %.1: type = ptr_type %Class [template]
  36. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  37. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  38. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  39. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  40. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  41. // CHECK:STDOUT: %.3: type = struct_type {} [template]
  42. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  43. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  44. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  45. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: imports {
  49. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  50. // CHECK:STDOUT: import Core//prelude
  51. // CHECK:STDOUT: import Core//prelude/operators
  52. // CHECK:STDOUT: import Core//prelude/types
  53. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  54. // CHECK:STDOUT: import Core//prelude/operators/as
  55. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  56. // CHECK:STDOUT: import Core//prelude/operators/comparison
  57. // CHECK:STDOUT: import Core//prelude/types/bool
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: file {
  62. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  63. // CHECK:STDOUT: .Core = imports.%Core
  64. // CHECK:STDOUT: .Class = %Class.decl
  65. // CHECK:STDOUT: .F = %F.decl
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Core.import = import Core
  68. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  69. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  70. // CHECK:STDOUT: %c.patt: %Class = binding_pattern c
  71. // CHECK:STDOUT: %c.param_patt: %Class = param_pattern %c.patt, runtime_param0
  72. // CHECK:STDOUT: %p.patt: %.1 = binding_pattern p
  73. // CHECK:STDOUT: %p.param_patt: %.1 = param_pattern %p.patt, runtime_param1
  74. // CHECK:STDOUT: } {
  75. // CHECK:STDOUT: %Class.ref.loc20_9: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  76. // CHECK:STDOUT: %Class.ref.loc20_19: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  77. // CHECK:STDOUT: %.loc20: type = ptr_type %Class [template = constants.%.1]
  78. // CHECK:STDOUT: %param.loc20_6: %Class = param runtime_param0
  79. // CHECK:STDOUT: %c: %Class = bind_name c, %param.loc20_6
  80. // CHECK:STDOUT: %param.loc20_16: %.1 = param runtime_param1
  81. // CHECK:STDOUT: %p: %.1 = bind_name p, %param.loc20_16
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: class @Class {
  86. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  87. // CHECK:STDOUT: %self.patt: %.1 = binding_pattern self
  88. // CHECK:STDOUT: %self.param_patt: %.1 = param_pattern %self.patt, runtime_param0
  89. // CHECK:STDOUT: %.loc12_8: auto = addr_pattern %self.param_patt
  90. // CHECK:STDOUT: } {
  91. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  92. // CHECK:STDOUT: %.loc12_24: type = ptr_type %Class [template = constants.%.1]
  93. // CHECK:STDOUT: %param: %.1 = param runtime_param0
  94. // CHECK:STDOUT: %self: %.1 = bind_name self, %param
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  97. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  98. // CHECK:STDOUT: %self.param_patt: %Class = param_pattern %self.patt, runtime_param0
  99. // CHECK:STDOUT: } {
  100. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  101. // CHECK:STDOUT: %param: %Class = param runtime_param0
  102. // CHECK:STDOUT: %self: %Class = bind_name self, %param
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %.loc18: <witness> = complete_type_witness %.3 [template = constants.%.4]
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: !members:
  107. // CHECK:STDOUT: .Self = constants.%Class
  108. // CHECK:STDOUT: .F = %F.decl
  109. // CHECK:STDOUT: .G = %G.decl
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: fn @F.1[addr %self.param_patt: %.1]();
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @G[%self.param_patt: %Class]();
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @F.2(%c.param_patt: %Class, %p.param_patt: %.1) {
  117. // CHECK:STDOUT: !entry:
  118. // CHECK:STDOUT: %c.ref.loc27: %Class = name_ref c, %c
  119. // CHECK:STDOUT: %F.ref.loc27: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
  120. // CHECK:STDOUT: %.loc27: <bound method> = bound_method %c.ref.loc27, %F.ref.loc27
  121. // CHECK:STDOUT: %F.call.loc27: init %.2 = call %.loc27(<invalid>) [template = <error>]
  122. // CHECK:STDOUT: %c.ref.loc29: %Class = name_ref c, %c
  123. // CHECK:STDOUT: %G.ref.loc29: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  124. // CHECK:STDOUT: %.loc29: <bound method> = bound_method %c.ref.loc29, %G.ref.loc29
  125. // CHECK:STDOUT: %G.call.loc29: init %.2 = call %.loc29(%c.ref.loc29)
  126. // CHECK:STDOUT: %p.ref.loc32: %.1 = name_ref p, %p
  127. // CHECK:STDOUT: %.loc32_4.1: ref %Class = deref %p.ref.loc32
  128. // CHECK:STDOUT: %F.ref.loc32: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
  129. // CHECK:STDOUT: %.loc32_7: <bound method> = bound_method %.loc32_4.1, %F.ref.loc32
  130. // CHECK:STDOUT: %.loc32_4.2: %.1 = addr_of %.loc32_4.1
  131. // CHECK:STDOUT: %F.call.loc32: init %.2 = call %.loc32_7(%.loc32_4.2)
  132. // CHECK:STDOUT: %p.ref.loc34: %.1 = name_ref p, %p
  133. // CHECK:STDOUT: %.loc34_4.1: ref %Class = deref %p.ref.loc34
  134. // CHECK:STDOUT: %G.ref.loc34: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
  135. // CHECK:STDOUT: %.loc34_7: <bound method> = bound_method %.loc34_4.1, %G.ref.loc34
  136. // CHECK:STDOUT: %.loc34_4.2: %Class = bind_value %.loc34_4.1
  137. // CHECK:STDOUT: %G.call.loc34: init %.2 = call %.loc34_7(%.loc34_4.2)
  138. // CHECK:STDOUT: return
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: