fail_abstract.carbon 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_abstract.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_abstract.carbon
  10. abstract class Abstract {
  11. var a: i32;
  12. }
  13. class Derived {
  14. extend base: Abstract;
  15. var d: i32;
  16. }
  17. fn Make() -> Derived {
  18. // TODO: This should be valid, and should construct an instance of `partial Abstract` as the base.
  19. // CHECK:STDERR: fail_abstract.carbon:[[@LINE+3]]:19: ERROR: Cannot construct instance of abstract class. Consider using `partial Abstract` instead.
  20. // CHECK:STDERR: return {.base = {.a = 1}, .d = 7};
  21. // CHECK:STDERR: ^~~~~~~~
  22. return {.base = {.a = 1}, .d = 7};
  23. }
  24. fn Access(d: Derived) -> (i32, i32) {
  25. return (d.d, d.base.a);
  26. }
  27. // CHECK:STDOUT: --- fail_abstract.carbon
  28. // CHECK:STDOUT:
  29. // CHECK:STDOUT: constants {
  30. // CHECK:STDOUT: %Abstract: type = class_type @Abstract [template]
  31. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  32. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  33. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  34. // CHECK:STDOUT: %.2: type = unbound_element_type %Abstract, i32 [template]
  35. // CHECK:STDOUT: %.3: type = struct_type {.a: i32} [template]
  36. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  37. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  38. // CHECK:STDOUT: %.5: type = unbound_element_type %Derived, %Abstract [template]
  39. // CHECK:STDOUT: %.6: type = unbound_element_type %Derived, i32 [template]
  40. // CHECK:STDOUT: %.7: type = struct_type {.base: %Abstract, .d: i32} [template]
  41. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  42. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  43. // CHECK:STDOUT: %.8: type = struct_type {.base: %.4, .d: i32} [template]
  44. // CHECK:STDOUT: %.9: type = ptr_type %.8 [template]
  45. // CHECK:STDOUT: %.10: type = ptr_type %.7 [template]
  46. // CHECK:STDOUT: %.11: i32 = int_literal 1 [template]
  47. // CHECK:STDOUT: %.12: i32 = int_literal 7 [template]
  48. // CHECK:STDOUT: %.13: type = struct_type {.base: %.3, .d: i32} [template]
  49. // CHECK:STDOUT: %.14: type = tuple_type (type, type) [template]
  50. // CHECK:STDOUT: %.15: type = tuple_type (i32, i32) [template]
  51. // CHECK:STDOUT: %Access.type: type = fn_type @Access [template]
  52. // CHECK:STDOUT: %Access: %Access.type = struct_value () [template]
  53. // CHECK:STDOUT: %.16: type = ptr_type %.15 [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: file {
  57. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  58. // CHECK:STDOUT: .Core = %Core
  59. // CHECK:STDOUT: .Abstract = %Abstract.decl
  60. // CHECK:STDOUT: .Derived = %Derived.decl
  61. // CHECK:STDOUT: .Make = %Make.decl
  62. // CHECK:STDOUT: .Access = %Access.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  65. // CHECK:STDOUT: %Abstract.decl: type = class_decl @Abstract [template = constants.%Abstract] {}
  66. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  67. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {}
  68. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  69. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  70. // CHECK:STDOUT: %Derived.ref.loc21: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  71. // CHECK:STDOUT: @Make.%return: ref %Derived = var <return slot>
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  74. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  75. // CHECK:STDOUT: %Access.decl: %Access.type = fn_decl @Access [template = constants.%Access] {
  76. // CHECK:STDOUT: %Derived.ref.loc29: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  77. // CHECK:STDOUT: %d.loc29_11.1: %Derived = param d
  78. // CHECK:STDOUT: @Access.%d: %Derived = bind_name d, %d.loc29_11.1
  79. // CHECK:STDOUT: %int.make_type_32.loc29_27: init type = call constants.%Int32() [template = i32]
  80. // CHECK:STDOUT: %int.make_type_32.loc29_32: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc29_35.1: %.14 = tuple_literal (%int.make_type_32.loc29_27, %int.make_type_32.loc29_32)
  82. // CHECK:STDOUT: %.loc29_35.2: type = value_of_initializer %int.make_type_32.loc29_27 [template = i32]
  83. // CHECK:STDOUT: %.loc29_35.3: type = converted %int.make_type_32.loc29_27, %.loc29_35.2 [template = i32]
  84. // CHECK:STDOUT: %.loc29_35.4: type = value_of_initializer %int.make_type_32.loc29_32 [template = i32]
  85. // CHECK:STDOUT: %.loc29_35.5: type = converted %int.make_type_32.loc29_32, %.loc29_35.4 [template = i32]
  86. // CHECK:STDOUT: %.loc29_35.6: type = converted %.loc29_35.1, constants.%.15 [template = constants.%.15]
  87. // CHECK:STDOUT: @Access.%return: ref %.15 = var <return slot>
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: class @Abstract {
  92. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  93. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  94. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  95. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: !members:
  98. // CHECK:STDOUT: .Self = constants.%Abstract
  99. // CHECK:STDOUT: .a = %.loc12_8
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: class @Derived {
  103. // CHECK:STDOUT: %Abstract.ref: type = name_ref Abstract, file.%Abstract.decl [template = constants.%Abstract]
  104. // CHECK:STDOUT: %.loc16: %.5 = base_decl %Abstract, element0 [template]
  105. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  106. // CHECK:STDOUT: %.loc18_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  107. // CHECK:STDOUT: %.loc18_10.2: type = converted %int.make_type_32, %.loc18_10.1 [template = i32]
  108. // CHECK:STDOUT: %.loc18_8: %.6 = field_decl d, element1 [template]
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: !members:
  111. // CHECK:STDOUT: .Self = constants.%Derived
  112. // CHECK:STDOUT: .base = %.loc16
  113. // CHECK:STDOUT: .d = %.loc18_8
  114. // CHECK:STDOUT: extend name_scope2
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @Make() -> %return: %Derived {
  120. // CHECK:STDOUT: !entry:
  121. // CHECK:STDOUT: %.loc26_25: i32 = int_literal 1 [template = constants.%.11]
  122. // CHECK:STDOUT: %.loc26_26: %.3 = struct_literal (%.loc26_25)
  123. // CHECK:STDOUT: %.loc26_34: i32 = int_literal 7 [template = constants.%.12]
  124. // CHECK:STDOUT: %.loc26_35: %.13 = struct_literal (%.loc26_26, %.loc26_34)
  125. // CHECK:STDOUT: return <error> to %return
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: fn @Access(%d: %Derived) -> %return: %.15 {
  129. // CHECK:STDOUT: !entry:
  130. // CHECK:STDOUT: %d.ref.loc30_11: %Derived = name_ref d, %d
  131. // CHECK:STDOUT: %d.ref.loc30_12: %.6 = name_ref d, @Derived.%.loc18_8 [template = @Derived.%.loc18_8]
  132. // CHECK:STDOUT: %.loc30_12.1: ref i32 = class_element_access %d.ref.loc30_11, element1
  133. // CHECK:STDOUT: %.loc30_12.2: i32 = bind_value %.loc30_12.1
  134. // CHECK:STDOUT: %d.ref.loc30_16: %Derived = name_ref d, %d
  135. // CHECK:STDOUT: %base.ref: %.5 = name_ref base, @Derived.%.loc16 [template = @Derived.%.loc16]
  136. // CHECK:STDOUT: %.loc30_17.1: ref %Abstract = class_element_access %d.ref.loc30_16, element0
  137. // CHECK:STDOUT: %.loc30_17.2: %Abstract = bind_value %.loc30_17.1
  138. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Abstract.%.loc12_8 [template = @Abstract.%.loc12_8]
  139. // CHECK:STDOUT: %.loc30_22.1: ref i32 = class_element_access %.loc30_17.2, element0
  140. // CHECK:STDOUT: %.loc30_22.2: i32 = bind_value %.loc30_22.1
  141. // CHECK:STDOUT: %.loc30_24.1: %.15 = tuple_literal (%.loc30_12.2, %.loc30_22.2)
  142. // CHECK:STDOUT: %.loc30_24.2: ref i32 = tuple_access %return, element0
  143. // CHECK:STDOUT: %.loc30_24.3: init i32 = initialize_from %.loc30_12.2 to %.loc30_24.2
  144. // CHECK:STDOUT: %.loc30_24.4: ref i32 = tuple_access %return, element1
  145. // CHECK:STDOUT: %.loc30_24.5: init i32 = initialize_from %.loc30_22.2 to %.loc30_24.4
  146. // CHECK:STDOUT: %.loc30_24.6: init %.15 = tuple_init (%.loc30_24.3, %.loc30_24.5) to %return
  147. // CHECK:STDOUT: %.loc30_25: init %.15 = converted %.loc30_24.1, %.loc30_24.6
  148. // CHECK:STDOUT: return %.loc30_25 to %return
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: