fail_derived_to_base.carbon 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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+4]]:38: ERROR: Cannot implicitly convert from `B2*` to `A1*`.
  21. // CHECK:STDERR: fn ConvertUnrelated(p: B2*) -> A1* { return p; }
  22. // CHECK:STDERR: ^~~~~~~~~
  23. // CHECK:STDERR:
  24. fn ConvertUnrelated(p: B2*) -> A1* { return p; }
  25. class Incomplete;
  26. // CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+3]]:47: ERROR: Cannot implicitly convert from `Incomplete*` to `A2*`.
  27. // CHECK:STDERR: fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
  28. // CHECK:STDERR: ^~~~~~~~~
  29. fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
  30. // CHECK:STDOUT: --- fail_derived_to_base.carbon
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: constants {
  33. // CHECK:STDOUT: %A1: type = class_type @A1 [template]
  34. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  35. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  36. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  37. // CHECK:STDOUT: %.2: type = unbound_element_type %A1, i32 [template]
  38. // CHECK:STDOUT: %.3: type = struct_type {.a: i32} [template]
  39. // CHECK:STDOUT: %A2: type = class_type @A2 [template]
  40. // CHECK:STDOUT: %.4: type = unbound_element_type %A2, i32 [template]
  41. // CHECK:STDOUT: %B2: type = class_type @B2 [template]
  42. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  43. // CHECK:STDOUT: %.6: type = unbound_element_type %B2, %A2 [template]
  44. // CHECK:STDOUT: %.7: type = unbound_element_type %B2, i32 [template]
  45. // CHECK:STDOUT: %.8: type = struct_type {.base: %A2, .b: i32} [template]
  46. // CHECK:STDOUT: %.9: type = ptr_type %B2 [template]
  47. // CHECK:STDOUT: %.10: type = ptr_type %A1 [template]
  48. // CHECK:STDOUT: %ConvertUnrelated.type: type = fn_type @ConvertUnrelated [template]
  49. // CHECK:STDOUT: %ConvertUnrelated: %ConvertUnrelated.type = struct_value () [template]
  50. // CHECK:STDOUT: %.11: type = struct_type {.base: %.5, .b: i32} [template]
  51. // CHECK:STDOUT: %.12: type = ptr_type %.11 [template]
  52. // CHECK:STDOUT: %.13: type = ptr_type %.8 [template]
  53. // CHECK:STDOUT: %Incomplete: type = class_type @Incomplete [template]
  54. // CHECK:STDOUT: %.14: type = ptr_type %Incomplete [template]
  55. // CHECK:STDOUT: %.15: type = ptr_type %A2 [template]
  56. // CHECK:STDOUT: %ConvertIncomplete.type: type = fn_type @ConvertIncomplete [template]
  57. // CHECK:STDOUT: %ConvertIncomplete: %ConvertIncomplete.type = struct_value () [template]
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: imports {
  61. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  62. // CHECK:STDOUT: .Int32 = %import_ref
  63. // CHECK:STDOUT: import Core//prelude
  64. // CHECK:STDOUT: import Core//prelude/operators
  65. // CHECK:STDOUT: import Core//prelude/types
  66. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  67. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  68. // CHECK:STDOUT: import Core//prelude/operators/comparison
  69. // CHECK:STDOUT: import Core//prelude/types/bool
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: file {
  75. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  76. // CHECK:STDOUT: .Core = imports.%Core
  77. // CHECK:STDOUT: .A1 = %A1.decl
  78. // CHECK:STDOUT: .A2 = %A2.decl
  79. // CHECK:STDOUT: .B2 = %B2.decl
  80. // CHECK:STDOUT: .ConvertUnrelated = %ConvertUnrelated.decl
  81. // CHECK:STDOUT: .Incomplete = %Incomplete.decl
  82. // CHECK:STDOUT: .ConvertIncomplete = %ConvertIncomplete.decl
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %Core.import = import Core
  85. // CHECK:STDOUT: %A1.decl: type = class_decl @A1 [template = constants.%A1] {}
  86. // CHECK:STDOUT: %A2.decl: type = class_decl @A2 [template = constants.%A2] {}
  87. // CHECK:STDOUT: %B2.decl: type = class_decl @B2 [template = constants.%B2] {}
  88. // CHECK:STDOUT: %ConvertUnrelated.decl: %ConvertUnrelated.type = fn_decl @ConvertUnrelated [template = constants.%ConvertUnrelated] {
  89. // CHECK:STDOUT: %B2.ref: type = name_ref B2, %B2.decl [template = constants.%B2]
  90. // CHECK:STDOUT: %.loc28_26: type = ptr_type %B2 [template = constants.%.9]
  91. // CHECK:STDOUT: %p.loc28_21.1: %.9 = param p
  92. // CHECK:STDOUT: @ConvertUnrelated.%p: %.9 = bind_name p, %p.loc28_21.1
  93. // CHECK:STDOUT: %A1.ref: type = name_ref A1, %A1.decl [template = constants.%A1]
  94. // CHECK:STDOUT: %.loc28_34: type = ptr_type %A1 [template = constants.%.10]
  95. // CHECK:STDOUT: @ConvertUnrelated.%return: ref %.10 = var <return slot>
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %Incomplete.decl: type = class_decl @Incomplete [template = constants.%Incomplete] {}
  98. // CHECK:STDOUT: %ConvertIncomplete.decl: %ConvertIncomplete.type = fn_decl @ConvertIncomplete [template = constants.%ConvertIncomplete] {
  99. // CHECK:STDOUT: %Incomplete.ref: type = name_ref Incomplete, %Incomplete.decl [template = constants.%Incomplete]
  100. // CHECK:STDOUT: %.loc35_35: type = ptr_type %Incomplete [template = constants.%.14]
  101. // CHECK:STDOUT: %p.loc35_22.1: %.14 = param p
  102. // CHECK:STDOUT: @ConvertIncomplete.%p: %.14 = bind_name p, %p.loc35_22.1
  103. // CHECK:STDOUT: %A2.ref: type = name_ref A2, %A2.decl [template = constants.%A2]
  104. // CHECK:STDOUT: %.loc35_43: type = ptr_type %A2 [template = constants.%.15]
  105. // CHECK:STDOUT: @ConvertIncomplete.%return: ref %.15 = var <return slot>
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: class @A1 {
  110. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  111. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  112. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  113. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: !members:
  116. // CHECK:STDOUT: .Self = constants.%A1
  117. // CHECK:STDOUT: .a = %.loc12_8
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: class @A2 {
  121. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  122. // CHECK:STDOUT: %.loc16_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  123. // CHECK:STDOUT: %.loc16_10.2: type = converted %int.make_type_32, %.loc16_10.1 [template = i32]
  124. // CHECK:STDOUT: %.loc16_8: %.4 = field_decl a, element0 [template]
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: !members:
  127. // CHECK:STDOUT: .Self = constants.%A2
  128. // CHECK:STDOUT: .a = %.loc16_8
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: class @B2 {
  132. // CHECK:STDOUT: %A2.ref: type = name_ref A2, file.%A2.decl [template = constants.%A2]
  133. // CHECK:STDOUT: %.loc20: %.6 = base_decl %A2, element0 [template]
  134. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  135. // CHECK:STDOUT: %.loc21_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  136. // CHECK:STDOUT: %.loc21_10.2: type = converted %int.make_type_32, %.loc21_10.1 [template = i32]
  137. // CHECK:STDOUT: %.loc21_8: %.7 = field_decl b, element1 [template]
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: !members:
  140. // CHECK:STDOUT: .Self = constants.%B2
  141. // CHECK:STDOUT: .base = %.loc20
  142. // CHECK:STDOUT: .b = %.loc21_8
  143. // CHECK:STDOUT: extend name_scope3
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: class @Incomplete;
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: fn @ConvertUnrelated(%p: %.9) -> %.10 {
  151. // CHECK:STDOUT: !entry:
  152. // CHECK:STDOUT: %p.ref: %.9 = name_ref p, %p
  153. // CHECK:STDOUT: return <error>
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn @ConvertIncomplete(%p: %.14) -> %.15 {
  157. // CHECK:STDOUT: !entry:
  158. // CHECK:STDOUT: %p.ref: %.14 = name_ref p, %p
  159. // CHECK:STDOUT: return <error>
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: