convert_qualifiers.carbon 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/pointer/convert_qualifiers.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/pointer/convert_qualifiers.carbon
  12. // --- same_or_added_qualifiers.carbon
  13. library "[[@TEST_NAME]]";
  14. class X {}
  15. fn TakeConst(p: const X*);
  16. fn TakeNonConst(p: X*);
  17. fn NonConst(p: X*) {
  18. //@dump-sem-ir-begin
  19. TakeNonConst(p);
  20. TakeConst(p);
  21. //@dump-sem-ir-end
  22. }
  23. fn Const(p: const X*) {
  24. //@dump-sem-ir-begin
  25. TakeConst(p);
  26. //@dump-sem-ir-end
  27. }
  28. // Same thing, but the pointee type happens to be a pointer.
  29. fn TakeConstConst(p: const (const X*)*);
  30. fn NonConstConst(p: const X**) {
  31. //@dump-sem-ir-begin
  32. TakeConstConst(p);
  33. //@dump-sem-ir-end
  34. }
  35. // --- fail_removed_qualifiers.carbon
  36. library "[[@TEST_NAME]]";
  37. class X {}
  38. fn TakeNonConst(p: X*);
  39. // CHECK:STDERR: fail_removed_qualifiers.carbon:[[@LINE+10]]:38: error: cannot implicitly convert expression of type `const X*` to `X*` [ConversionFailure]
  40. // CHECK:STDERR: fn Const(p: const X*) { TakeNonConst(p); }
  41. // CHECK:STDERR: ^
  42. // CHECK:STDERR: fail_removed_qualifiers.carbon:[[@LINE+7]]:38: note: type `const X*` does not implement interface `Core.ImplicitAs(X*)` [MissingImplInMemberAccessInContext]
  43. // CHECK:STDERR: fn Const(p: const X*) { TakeNonConst(p); }
  44. // CHECK:STDERR: ^
  45. // CHECK:STDERR: fail_removed_qualifiers.carbon:[[@LINE-8]]:17: note: initializing function parameter [InCallToFunctionParam]
  46. // CHECK:STDERR: fn TakeNonConst(p: X*);
  47. // CHECK:STDERR: ^~~~~
  48. // CHECK:STDERR:
  49. fn Const(p: const X*) { TakeNonConst(p); }
  50. // --- fail_todo_nested_qualifiers.carbon
  51. library "[[@TEST_NAME]]";
  52. class X {}
  53. fn TakeConstConst(p: const (const X*)*);
  54. // TODO: We should allow these conversions.
  55. fn NonConstNonConst(p: X**) {
  56. //@dump-sem-ir-begin
  57. // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+10]]:18: error: cannot implicitly convert expression of type `X**` to `const (const X*)*` [ConversionFailure]
  58. // CHECK:STDERR: TakeConstConst(p);
  59. // CHECK:STDERR: ^
  60. // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+7]]:18: note: type `X**` does not implement interface `Core.ImplicitAs(const (const X*)*)` [MissingImplInMemberAccessInContext]
  61. // CHECK:STDERR: TakeConstConst(p);
  62. // CHECK:STDERR: ^
  63. // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE-12]]:19: note: initializing function parameter [InCallToFunctionParam]
  64. // CHECK:STDERR: fn TakeConstConst(p: const (const X*)*);
  65. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  66. // CHECK:STDERR:
  67. TakeConstConst(p);
  68. //@dump-sem-ir-end
  69. }
  70. fn ConstNonConst(p: const (X*)*) {
  71. //@dump-sem-ir-begin
  72. // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+10]]:18: error: cannot implicitly convert expression of type `const (X*)*` to `const (const X*)*` [ConversionFailure]
  73. // CHECK:STDERR: TakeConstConst(p);
  74. // CHECK:STDERR: ^
  75. // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE+7]]:18: note: type `const (X*)*` does not implement interface `Core.ImplicitAs(const (const X*)*)` [MissingImplInMemberAccessInContext]
  76. // CHECK:STDERR: TakeConstConst(p);
  77. // CHECK:STDERR: ^
  78. // CHECK:STDERR: fail_todo_nested_qualifiers.carbon:[[@LINE-28]]:19: note: initializing function parameter [InCallToFunctionParam]
  79. // CHECK:STDERR: fn TakeConstConst(p: const (const X*)*);
  80. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  81. // CHECK:STDERR:
  82. TakeConstConst(p);
  83. //@dump-sem-ir-end
  84. }
  85. // --- fail_nested_added_qualifiers.carbon
  86. library "[[@TEST_NAME]]";
  87. class X {}
  88. fn TakeNonConstConst(p: (const X*)*);
  89. // CHECK:STDERR: fail_nested_added_qualifiers.carbon:[[@LINE+10]]:49: error: cannot implicitly convert expression of type `X**` to `const X**` [ConversionFailure]
  90. // CHECK:STDERR: fn NonConstNonConst(p: X**) { TakeNonConstConst(p); }
  91. // CHECK:STDERR: ^
  92. // CHECK:STDERR: fail_nested_added_qualifiers.carbon:[[@LINE+7]]:49: note: type `X**` does not implement interface `Core.ImplicitAs(const X**)` [MissingImplInMemberAccessInContext]
  93. // CHECK:STDERR: fn NonConstNonConst(p: X**) { TakeNonConstConst(p); }
  94. // CHECK:STDERR: ^
  95. // CHECK:STDERR: fail_nested_added_qualifiers.carbon:[[@LINE-8]]:22: note: initializing function parameter [InCallToFunctionParam]
  96. // CHECK:STDERR: fn TakeNonConstConst(p: (const X*)*);
  97. // CHECK:STDERR: ^~~~~~~~~~~~~~
  98. // CHECK:STDERR:
  99. fn NonConstNonConst(p: X**) { TakeNonConstConst(p); }
  100. // CHECK:STDOUT: --- same_or_added_qualifiers.carbon
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: constants {
  103. // CHECK:STDOUT: %X: type = class_type @X [concrete]
  104. // CHECK:STDOUT: %const.9ff: type = const_type %X [concrete]
  105. // CHECK:STDOUT: %ptr.d4c: type = ptr_type %const.9ff [concrete]
  106. // CHECK:STDOUT: %TakeConst.type: type = fn_type @TakeConst [concrete]
  107. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  108. // CHECK:STDOUT: %TakeConst: %TakeConst.type = struct_value () [concrete]
  109. // CHECK:STDOUT: %ptr.2a9: type = ptr_type %X [concrete]
  110. // CHECK:STDOUT: %TakeNonConst.type: type = fn_type @TakeNonConst [concrete]
  111. // CHECK:STDOUT: %TakeNonConst: %TakeNonConst.type = struct_value () [concrete]
  112. // CHECK:STDOUT: %const.1ce: type = const_type %ptr.d4c [concrete]
  113. // CHECK:STDOUT: %ptr.a2c: type = ptr_type %const.1ce [concrete]
  114. // CHECK:STDOUT: %TakeConstConst.type: type = fn_type @TakeConstConst [concrete]
  115. // CHECK:STDOUT: %TakeConstConst: %TakeConstConst.type = struct_value () [concrete]
  116. // CHECK:STDOUT: %ptr.0ab: type = ptr_type %ptr.d4c [concrete]
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @NonConst(%p.param: %ptr.2a9) {
  120. // CHECK:STDOUT: !entry:
  121. // CHECK:STDOUT: %TakeNonConst.ref: %TakeNonConst.type = name_ref TakeNonConst, file.%TakeNonConst.decl [concrete = constants.%TakeNonConst]
  122. // CHECK:STDOUT: %p.ref.loc11: %ptr.2a9 = name_ref p, %p
  123. // CHECK:STDOUT: %TakeNonConst.call: init %empty_tuple.type = call %TakeNonConst.ref(%p.ref.loc11)
  124. // CHECK:STDOUT: %TakeConst.ref: %TakeConst.type = name_ref TakeConst, file.%TakeConst.decl [concrete = constants.%TakeConst]
  125. // CHECK:STDOUT: %p.ref.loc12: %ptr.2a9 = name_ref p, %p
  126. // CHECK:STDOUT: %.loc12_13.1: %ptr.d4c = as_compatible %p.ref.loc12
  127. // CHECK:STDOUT: %.loc12_13.2: %ptr.d4c = converted %p.ref.loc12, %.loc12_13.1
  128. // CHECK:STDOUT: %TakeConst.call: init %empty_tuple.type = call %TakeConst.ref(%.loc12_13.2)
  129. // CHECK:STDOUT: <elided>
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: fn @Const(%p.param: %ptr.d4c) {
  133. // CHECK:STDOUT: !entry:
  134. // CHECK:STDOUT: %TakeConst.ref: %TakeConst.type = name_ref TakeConst, file.%TakeConst.decl [concrete = constants.%TakeConst]
  135. // CHECK:STDOUT: %p.ref: %ptr.d4c = name_ref p, %p
  136. // CHECK:STDOUT: %TakeConst.call: init %empty_tuple.type = call %TakeConst.ref(%p.ref)
  137. // CHECK:STDOUT: <elided>
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: fn @NonConstConst(%p.param: %ptr.0ab) {
  141. // CHECK:STDOUT: !entry:
  142. // CHECK:STDOUT: %TakeConstConst.ref: %TakeConstConst.type = name_ref TakeConstConst, file.%TakeConstConst.decl [concrete = constants.%TakeConstConst]
  143. // CHECK:STDOUT: %p.ref: %ptr.0ab = name_ref p, %p
  144. // CHECK:STDOUT: %.loc25_18.1: %ptr.a2c = as_compatible %p.ref
  145. // CHECK:STDOUT: %.loc25_18.2: %ptr.a2c = converted %p.ref, %.loc25_18.1
  146. // CHECK:STDOUT: %TakeConstConst.call: init %empty_tuple.type = call %TakeConstConst.ref(%.loc25_18.2)
  147. // CHECK:STDOUT: <elided>
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: --- fail_todo_nested_qualifiers.carbon
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: constants {
  153. // CHECK:STDOUT: %X: type = class_type @X [concrete]
  154. // CHECK:STDOUT: %const.9ff: type = const_type %X [concrete]
  155. // CHECK:STDOUT: %ptr.d4c: type = ptr_type %const.9ff [concrete]
  156. // CHECK:STDOUT: %const.1ce: type = const_type %ptr.d4c [concrete]
  157. // CHECK:STDOUT: %ptr.a2c: type = ptr_type %const.1ce [concrete]
  158. // CHECK:STDOUT: %TakeConstConst.type: type = fn_type @TakeConstConst [concrete]
  159. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  160. // CHECK:STDOUT: %TakeConstConst: %TakeConstConst.type = struct_value () [concrete]
  161. // CHECK:STDOUT: %ptr.2a9: type = ptr_type %X [concrete]
  162. // CHECK:STDOUT: %ptr.8a5: type = ptr_type %ptr.2a9 [concrete]
  163. // CHECK:STDOUT: %const.cbb: type = const_type %ptr.2a9 [concrete]
  164. // CHECK:STDOUT: %ptr.e9d: type = ptr_type %const.cbb [concrete]
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: fn @NonConstNonConst(%p.param: %ptr.8a5) {
  168. // CHECK:STDOUT: !entry:
  169. // CHECK:STDOUT: %TakeConstConst.ref: %TakeConstConst.type = name_ref TakeConstConst, file.%TakeConstConst.decl [concrete = constants.%TakeConstConst]
  170. // CHECK:STDOUT: %p.ref: %ptr.8a5 = name_ref p, %p
  171. // CHECK:STDOUT: %.loc22: %ptr.a2c = converted %p.ref, <error> [concrete = <error>]
  172. // CHECK:STDOUT: %TakeConstConst.call: init %empty_tuple.type = call %TakeConstConst.ref(<error>)
  173. // CHECK:STDOUT: <elided>
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn @ConstNonConst(%p.param: %ptr.e9d) {
  177. // CHECK:STDOUT: !entry:
  178. // CHECK:STDOUT: %TakeConstConst.ref: %TakeConstConst.type = name_ref TakeConstConst, file.%TakeConstConst.decl [concrete = constants.%TakeConstConst]
  179. // CHECK:STDOUT: %p.ref: %ptr.e9d = name_ref p, %p
  180. // CHECK:STDOUT: %.loc38: %ptr.a2c = converted %p.ref, <error> [concrete = <error>]
  181. // CHECK:STDOUT: %TakeConstConst.call: init %empty_tuple.type = call %TakeConstConst.ref(<error>)
  182. // CHECK:STDOUT: <elided>
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT: