fail_derived_to_base.carbon 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_derived_to_base.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_derived_to_base.carbon
  10. base class A1 {
  11. var a: i32;
  12. }
  13. base class A2 {
  14. var a: i32;
  15. }
  16. class B2 {
  17. extend base: A2;
  18. var b: i32;
  19. }
  20. // CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+7]]:38: error: cannot implicitly convert from `B2*` to `A1*` [ImplicitAsConversionFailure]
  21. // CHECK:STDERR: fn ConvertUnrelated(p: B2*) -> A1* { return p; }
  22. // CHECK:STDERR: ^~~~~~~~~
  23. // CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+4]]:38: note: type `B2*` does not implement interface `ImplicitAs(A1*)` [MissingImplInMemberAccessNote]
  24. // CHECK:STDERR: fn ConvertUnrelated(p: B2*) -> A1* { return p; }
  25. // CHECK:STDERR: ^~~~~~~~~
  26. // CHECK:STDERR:
  27. fn ConvertUnrelated(p: B2*) -> A1* { return p; }
  28. class Incomplete;
  29. // CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+6]]:47: error: cannot implicitly convert from `Incomplete*` to `A2*` [ImplicitAsConversionFailure]
  30. // CHECK:STDERR: fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
  31. // CHECK:STDERR: ^~~~~~~~~
  32. // CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+3]]:47: note: type `Incomplete*` does not implement interface `ImplicitAs(A2*)` [MissingImplInMemberAccessNote]
  33. // CHECK:STDERR: fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
  34. // CHECK:STDERR: ^~~~~~~~~
  35. fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
  36. // CHECK:STDOUT: --- fail_derived_to_base.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %A1: type = class_type @A1 [template]
  40. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  41. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  42. // CHECK:STDOUT: %.1: type = unbound_element_type %A1, i32 [template]
  43. // CHECK:STDOUT: %.2: type = struct_type {.a: i32} [template]
  44. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  45. // CHECK:STDOUT: %A2: type = class_type @A2 [template]
  46. // CHECK:STDOUT: %.4: type = unbound_element_type %A2, i32 [template]
  47. // CHECK:STDOUT: %B2: type = class_type @B2 [template]
  48. // CHECK:STDOUT: %.6: type = unbound_element_type %B2, %A2 [template]
  49. // CHECK:STDOUT: %.7: type = unbound_element_type %B2, i32 [template]
  50. // CHECK:STDOUT: %.8: type = struct_type {.base: %A2, .b: i32} [template]
  51. // CHECK:STDOUT: %.9: <witness> = complete_type_witness %.8 [template]
  52. // CHECK:STDOUT: %.10: type = ptr_type %B2 [template]
  53. // CHECK:STDOUT: %.11: type = ptr_type %A1 [template]
  54. // CHECK:STDOUT: %ConvertUnrelated.type: type = fn_type @ConvertUnrelated [template]
  55. // CHECK:STDOUT: %ConvertUnrelated: %ConvertUnrelated.type = struct_value () [template]
  56. // CHECK:STDOUT: %Incomplete: type = class_type @Incomplete [template]
  57. // CHECK:STDOUT: %.40: type = ptr_type %Incomplete [template]
  58. // CHECK:STDOUT: %.41: type = ptr_type %A2 [template]
  59. // CHECK:STDOUT: %ConvertIncomplete.type: type = fn_type @ConvertIncomplete [template]
  60. // CHECK:STDOUT: %ConvertIncomplete: %ConvertIncomplete.type = struct_value () [template]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: imports {
  64. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  65. // CHECK:STDOUT: .Int32 = %import_ref.1
  66. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  67. // CHECK:STDOUT: import Core//prelude
  68. // CHECK:STDOUT: import Core//prelude/...
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: file {
  73. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  74. // CHECK:STDOUT: .Core = imports.%Core
  75. // CHECK:STDOUT: .A1 = %A1.decl
  76. // CHECK:STDOUT: .A2 = %A2.decl
  77. // CHECK:STDOUT: .B2 = %B2.decl
  78. // CHECK:STDOUT: .ConvertUnrelated = %ConvertUnrelated.decl
  79. // CHECK:STDOUT: .Incomplete = %Incomplete.decl
  80. // CHECK:STDOUT: .ConvertIncomplete = %ConvertIncomplete.decl
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Core.import = import Core
  83. // CHECK:STDOUT: %A1.decl: type = class_decl @A1 [template = constants.%A1] {} {}
  84. // CHECK:STDOUT: %A2.decl: type = class_decl @A2 [template = constants.%A2] {} {}
  85. // CHECK:STDOUT: %B2.decl: type = class_decl @B2 [template = constants.%B2] {} {}
  86. // CHECK:STDOUT: %ConvertUnrelated.decl: %ConvertUnrelated.type = fn_decl @ConvertUnrelated [template = constants.%ConvertUnrelated] {
  87. // CHECK:STDOUT: %p.patt: %.10 = binding_pattern p
  88. // CHECK:STDOUT: %p.param_patt: %.10 = value_param_pattern %p.patt, runtime_param0
  89. // CHECK:STDOUT: %return.patt: %.11 = return_slot_pattern
  90. // CHECK:STDOUT: %return.param_patt: %.11 = out_param_pattern %return.patt, runtime_param1
  91. // CHECK:STDOUT: } {
  92. // CHECK:STDOUT: %B2.ref: type = name_ref B2, file.%B2.decl [template = constants.%B2]
  93. // CHECK:STDOUT: %.loc31_26: type = ptr_type %B2 [template = constants.%.10]
  94. // CHECK:STDOUT: %A1.ref: type = name_ref A1, file.%A1.decl [template = constants.%A1]
  95. // CHECK:STDOUT: %.loc31_34: type = ptr_type %A1 [template = constants.%.11]
  96. // CHECK:STDOUT: %p.param: %.10 = value_param runtime_param0
  97. // CHECK:STDOUT: %p: %.10 = bind_name p, %p.param
  98. // CHECK:STDOUT: %return.param: ref %.11 = out_param runtime_param1
  99. // CHECK:STDOUT: %return: ref %.11 = return_slot %return.param
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %Incomplete.decl: type = class_decl @Incomplete [template = constants.%Incomplete] {} {}
  102. // CHECK:STDOUT: %ConvertIncomplete.decl: %ConvertIncomplete.type = fn_decl @ConvertIncomplete [template = constants.%ConvertIncomplete] {
  103. // CHECK:STDOUT: %p.patt: %.40 = binding_pattern p
  104. // CHECK:STDOUT: %p.param_patt: %.40 = value_param_pattern %p.patt, runtime_param0
  105. // CHECK:STDOUT: %return.patt: %.41 = return_slot_pattern
  106. // CHECK:STDOUT: %return.param_patt: %.41 = out_param_pattern %return.patt, runtime_param1
  107. // CHECK:STDOUT: } {
  108. // CHECK:STDOUT: %Incomplete.ref: type = name_ref Incomplete, file.%Incomplete.decl [template = constants.%Incomplete]
  109. // CHECK:STDOUT: %.loc41_35: type = ptr_type %Incomplete [template = constants.%.40]
  110. // CHECK:STDOUT: %A2.ref: type = name_ref A2, file.%A2.decl [template = constants.%A2]
  111. // CHECK:STDOUT: %.loc41_43: type = ptr_type %A2 [template = constants.%.41]
  112. // CHECK:STDOUT: %p.param: %.40 = value_param runtime_param0
  113. // CHECK:STDOUT: %p: %.40 = bind_name p, %p.param
  114. // CHECK:STDOUT: %return.param: ref %.41 = out_param runtime_param1
  115. // CHECK:STDOUT: %return: ref %.41 = return_slot %return.param
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: class @A1 {
  120. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  121. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  122. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  123. // CHECK:STDOUT: %.loc12_8: %.1 = field_decl a, element0 [template]
  124. // CHECK:STDOUT: %.loc13: <witness> = complete_type_witness %.2 [template = constants.%.3]
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: !members:
  127. // CHECK:STDOUT: .Self = constants.%A1
  128. // CHECK:STDOUT: .a = %.loc12_8
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: class @A2 {
  132. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  133. // CHECK:STDOUT: %.loc16_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  134. // CHECK:STDOUT: %.loc16_10.2: type = converted %int.make_type_32, %.loc16_10.1 [template = i32]
  135. // CHECK:STDOUT: %.loc16_8: %.4 = field_decl a, element0 [template]
  136. // CHECK:STDOUT: %.loc17: <witness> = complete_type_witness %.2 [template = constants.%.3]
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: !members:
  139. // CHECK:STDOUT: .Self = constants.%A2
  140. // CHECK:STDOUT: .a = %.loc16_8
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: class @B2 {
  144. // CHECK:STDOUT: %A2.ref: type = name_ref A2, file.%A2.decl [template = constants.%A2]
  145. // CHECK:STDOUT: %.loc20: %.6 = base_decl %A2, element0 [template]
  146. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  147. // CHECK:STDOUT: %.loc21_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  148. // CHECK:STDOUT: %.loc21_10.2: type = converted %int.make_type_32, %.loc21_10.1 [template = i32]
  149. // CHECK:STDOUT: %.loc21_8: %.7 = field_decl b, element1 [template]
  150. // CHECK:STDOUT: %.loc22: <witness> = complete_type_witness %.8 [template = constants.%.9]
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: !members:
  153. // CHECK:STDOUT: .Self = constants.%B2
  154. // CHECK:STDOUT: .base = %.loc20
  155. // CHECK:STDOUT: .b = %.loc21_8
  156. // CHECK:STDOUT: extend %A2.ref
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: class @Incomplete;
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: fn @ConvertUnrelated(%p.param_patt: %.10) -> %.11 {
  162. // CHECK:STDOUT: !entry:
  163. // CHECK:STDOUT: %p.ref: %.10 = name_ref p, %p
  164. // CHECK:STDOUT: %.loc31_46: %.11 = converted %p.ref, <error> [template = <error>]
  165. // CHECK:STDOUT: return <error>
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: fn @ConvertIncomplete(%p.param_patt: %.40) -> %.41 {
  169. // CHECK:STDOUT: !entry:
  170. // CHECK:STDOUT: %p.ref: %.40 = name_ref p, %p
  171. // CHECK:STDOUT: %.loc41_55: %.41 = converted %p.ref, <error> [template = <error>]
  172. // CHECK:STDOUT: return <error>
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT: