fail_memaccess_category.carbon 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_memaccess_category.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_memaccess_category.carbon
  10. class A {
  11. fn F[addr self: A*]();
  12. }
  13. class B {
  14. var a: A;
  15. }
  16. fn F(s: {.a: A}, b: B) {
  17. // `s` has only a value representation, so this must be invalid.
  18. // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE+7]]:4: error: `addr self` method cannot be invoked on a value
  19. // CHECK:STDERR: s.a.F();
  20. // CHECK:STDERR: ^
  21. // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE-12]]:8: note: initializing `addr self` parameter of method declared here
  22. // CHECK:STDERR: fn F[addr self: A*]();
  23. // CHECK:STDERR: ^~~~~~~~~~~~~
  24. // CHECK:STDERR:
  25. s.a.F();
  26. // `b` has an object representation for `A`, but this is still invalid for
  27. // consistency.
  28. // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE+6]]:4: error: `addr self` method cannot be invoked on a value
  29. // CHECK:STDERR: b.a.F();
  30. // CHECK:STDERR: ^
  31. // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE-23]]:8: note: initializing `addr self` parameter of method declared here
  32. // CHECK:STDERR: fn F[addr self: A*]();
  33. // CHECK:STDERR: ^~~~~~~~~~~~~
  34. b.a.F();
  35. }
  36. // CHECK:STDOUT: --- fail_memaccess_category.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %A: type = class_type @A [template]
  40. // CHECK:STDOUT: %.1: type = ptr_type %A [template]
  41. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  42. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  43. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  44. // CHECK:STDOUT: %.3: type = struct_type {} [template]
  45. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  46. // CHECK:STDOUT: %B: type = class_type @B [template]
  47. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  48. // CHECK:STDOUT: %.6: type = unbound_element_type %B, %A [template]
  49. // CHECK:STDOUT: %.7: type = struct_type {.a: %A} [template]
  50. // CHECK:STDOUT: %.8: <witness> = complete_type_witness %.7 [template]
  51. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  52. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  53. // CHECK:STDOUT: %.9: type = struct_type {.a: %.5} [template]
  54. // CHECK:STDOUT: %.10: type = ptr_type %.7 [template]
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: imports {
  58. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  59. // CHECK:STDOUT: import Core//prelude
  60. // CHECK:STDOUT: import Core//prelude/operators
  61. // CHECK:STDOUT: import Core//prelude/types
  62. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  63. // CHECK:STDOUT: import Core//prelude/operators/as
  64. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  65. // CHECK:STDOUT: import Core//prelude/operators/comparison
  66. // CHECK:STDOUT: import Core//prelude/types/bool
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: file {
  71. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  72. // CHECK:STDOUT: .Core = imports.%Core
  73. // CHECK:STDOUT: .A = %A.decl
  74. // CHECK:STDOUT: .B = %B.decl
  75. // CHECK:STDOUT: .F = %F.decl
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %Core.import = import Core
  78. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  79. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {} {}
  80. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  81. // CHECK:STDOUT: %s.patt: %.7 = binding_pattern s
  82. // CHECK:STDOUT: %s.param_patt: %.7 = param_pattern %s.patt, runtime_param0
  83. // CHECK:STDOUT: %b.patt: %B = binding_pattern b
  84. // CHECK:STDOUT: %b.param_patt: %B = param_pattern %b.patt, runtime_param1
  85. // CHECK:STDOUT: } {
  86. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  87. // CHECK:STDOUT: %.loc19: type = struct_type {.a: %A} [template = constants.%.7]
  88. // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [template = constants.%B]
  89. // CHECK:STDOUT: %param.loc19_6: %.7 = param runtime_param0
  90. // CHECK:STDOUT: %s: %.7 = bind_name s, %param.loc19_6
  91. // CHECK:STDOUT: %param.loc19_18: %B = param runtime_param1
  92. // CHECK:STDOUT: %b: %B = bind_name b, %param.loc19_18
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: class @A {
  97. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  98. // CHECK:STDOUT: %self.patt: %.1 = binding_pattern self
  99. // CHECK:STDOUT: %self.param_patt: %.1 = param_pattern %self.patt, runtime_param0
  100. // CHECK:STDOUT: %.loc12_8: auto = addr_pattern %self.param_patt
  101. // CHECK:STDOUT: } {
  102. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  103. // CHECK:STDOUT: %.loc12_20: type = ptr_type %A [template = constants.%.1]
  104. // CHECK:STDOUT: %param: %.1 = param runtime_param0
  105. // CHECK:STDOUT: %self: %.1 = bind_name self, %param
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %.loc13: <witness> = complete_type_witness %.3 [template = constants.%.4]
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: !members:
  110. // CHECK:STDOUT: .Self = constants.%A
  111. // CHECK:STDOUT: .F = %F.decl
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: class @B {
  115. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  116. // CHECK:STDOUT: %.loc16: %.6 = field_decl a, element0 [template]
  117. // CHECK:STDOUT: %.loc17: <witness> = complete_type_witness %.7 [template = constants.%.8]
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: !members:
  120. // CHECK:STDOUT: .Self = constants.%B
  121. // CHECK:STDOUT: .a = %.loc16
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: fn @F.1[addr %self.param_patt: %.1]();
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @F.2(%s.param_patt: %.7, %b.param_patt: %B) {
  127. // CHECK:STDOUT: !entry:
  128. // CHECK:STDOUT: %s.ref: %.7 = name_ref s, %s
  129. // CHECK:STDOUT: %.loc28_4: %A = struct_access %s.ref, element0
  130. // CHECK:STDOUT: %F.ref.loc28: %F.type.1 = name_ref F, @A.%F.decl [template = constants.%F.1]
  131. // CHECK:STDOUT: %.loc28_6: <bound method> = bound_method %.loc28_4, %F.ref.loc28
  132. // CHECK:STDOUT: %F.call.loc28: init %.2 = call %.loc28_6(<invalid>) [template = <error>]
  133. // CHECK:STDOUT: %b.ref: %B = name_ref b, %b
  134. // CHECK:STDOUT: %a.ref: %.6 = name_ref a, @B.%.loc16 [template = @B.%.loc16]
  135. // CHECK:STDOUT: %.loc38_4.1: ref %A = class_element_access %b.ref, element0
  136. // CHECK:STDOUT: %.loc38_4.2: %A = bind_value %.loc38_4.1
  137. // CHECK:STDOUT: %F.ref.loc38: %F.type.1 = name_ref F, @A.%F.decl [template = constants.%F.1]
  138. // CHECK:STDOUT: %.loc38_6: <bound method> = bound_method %.loc38_4.2, %F.ref.loc38
  139. // CHECK:STDOUT: %F.call.loc38: init %.2 = call %.loc38_6(<invalid>) [template = <error>]
  140. // CHECK:STDOUT: return
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: