fail_memaccess_category.carbon 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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]]:8: 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]]:13: 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]]:8: 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]]:13: 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: %B: type = class_type @B [template]
  46. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  47. // CHECK:STDOUT: %.5: type = unbound_element_type %B, %A [template]
  48. // CHECK:STDOUT: %.6: type = struct_type {.a: %A} [template]
  49. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  50. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  51. // CHECK:STDOUT: %.7: type = struct_type {.a: %.4} [template]
  52. // CHECK:STDOUT: %.8: type = ptr_type %.6 [template]
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Core = %Core
  58. // CHECK:STDOUT: .A = %A.decl
  59. // CHECK:STDOUT: .B = %B.decl
  60. // CHECK:STDOUT: .F = %F.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  63. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  64. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  65. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  66. // CHECK:STDOUT: %A.ref: type = name_ref A, %A.decl [template = constants.%A]
  67. // CHECK:STDOUT: %.loc19: type = struct_type {.a: %A} [template = constants.%.6]
  68. // CHECK:STDOUT: %s.loc19_6.1: %.6 = param s
  69. // CHECK:STDOUT: @F.2.%s: %.6 = bind_name s, %s.loc19_6.1
  70. // CHECK:STDOUT: %B.ref: type = name_ref B, %B.decl [template = constants.%B]
  71. // CHECK:STDOUT: %b.loc19_18.1: %B = param b
  72. // CHECK:STDOUT: @F.2.%b: %B = bind_name b, %b.loc19_18.1
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: class @A {
  77. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  78. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  79. // CHECK:STDOUT: %.loc12_20: type = ptr_type %A [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:
  85. // CHECK:STDOUT: !members:
  86. // CHECK:STDOUT: .Self = constants.%A
  87. // CHECK:STDOUT: .F = %F.decl
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: class @B {
  91. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  92. // CHECK:STDOUT: %.loc16: %.5 = field_decl a, element0 [template]
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: !members:
  95. // CHECK:STDOUT: .Self = constants.%B
  96. // CHECK:STDOUT: .a = %.loc16
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: fn @F.1[addr @A.%self.loc12_13.3: %.1]();
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: fn @F.2(%s: %.6, %b: %B) {
  102. // CHECK:STDOUT: !entry:
  103. // CHECK:STDOUT: %s.ref: %.6 = name_ref s, %s
  104. // CHECK:STDOUT: %.loc28_4: %A = struct_access %s.ref, element0
  105. // CHECK:STDOUT: %F.ref.loc28: %F.type.1 = name_ref F, @A.%F.decl [template = constants.%F.1]
  106. // CHECK:STDOUT: %.loc28_6: <bound method> = bound_method %.loc28_4, %F.ref.loc28
  107. // CHECK:STDOUT: %F.call.loc28: init %.2 = call %.loc28_6(<invalid>) [template = <error>]
  108. // CHECK:STDOUT: %b.ref: %B = name_ref b, %b
  109. // CHECK:STDOUT: %a.ref: %.5 = name_ref a, @B.%.loc16 [template = @B.%.loc16]
  110. // CHECK:STDOUT: %.loc38_4.1: ref %A = class_element_access %b.ref, element0
  111. // CHECK:STDOUT: %.loc38_4.2: %A = bind_value %.loc38_4.1
  112. // CHECK:STDOUT: %F.ref.loc38: %F.type.1 = name_ref F, @A.%F.decl [template = constants.%F.1]
  113. // CHECK:STDOUT: %.loc38_6: <bound method> = bound_method %.loc38_4.2, %F.ref.loc38
  114. // CHECK:STDOUT: %F.call.loc38: init %.2 = call %.loc38_6(<invalid>) [template = <error>]
  115. // CHECK:STDOUT: return
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: