fail_adapt_bad_decl.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  6. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/adapter/fail_adapt_bad_decl.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/adapter/fail_adapt_bad_decl.carbon
  13. // --- fail_not_type.carbon
  14. library "[[@TEST_NAME]]";
  15. class Bad {
  16. // CHECK:STDERR: fail_not_type.carbon:[[@LINE+7]]:3: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet]
  17. // CHECK:STDERR: adapt 100;
  18. // CHECK:STDERR: ^~~~~~~~~~
  19. // CHECK:STDERR: fail_not_type.carbon:[[@LINE+4]]:3: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
  20. // CHECK:STDERR: adapt 100;
  21. // CHECK:STDERR: ^~~~~~~~~~
  22. // CHECK:STDERR:
  23. adapt 100;
  24. }
  25. // CHECK:STDERR: fail_not_type.carbon:[[@LINE+4]]:18: error: member name `F` not found in `Bad` [MemberNameNotFoundInInstScope]
  26. // CHECK:STDERR: fn Use(b: Bad) { b.F(); }
  27. // CHECK:STDERR: ^~~
  28. // CHECK:STDERR:
  29. fn Use(b: Bad) { b.F(); }
  30. // --- fail_extend_not_type.carbon
  31. library "[[@TEST_NAME]]";
  32. class Bad {
  33. // CHECK:STDERR: fail_extend_not_type.carbon:[[@LINE+7]]:3: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet]
  34. // CHECK:STDERR: extend adapt 100;
  35. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  36. // CHECK:STDERR: fail_extend_not_type.carbon:[[@LINE+4]]:3: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
  37. // CHECK:STDERR: extend adapt 100;
  38. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  39. // CHECK:STDERR:
  40. extend adapt 100;
  41. }
  42. // No diagnostic here, we don't know what names Bad has.
  43. fn Use(b: Bad) { b.F(); }
  44. // --- fail_repeated.carbon
  45. library "[[@TEST_NAME]]";
  46. class MultipleAdapts {
  47. adapt ();
  48. // CHECK:STDERR: fail_repeated.carbon:[[@LINE+7]]:3: error: multiple `adapt` declarations in class [AdaptDeclRepeated]
  49. // CHECK:STDERR: adapt {};
  50. // CHECK:STDERR: ^~~~~~~~~
  51. // CHECK:STDERR: fail_repeated.carbon:[[@LINE-4]]:3: note: previous `adapt` declaration is here [ClassSpecificDeclPrevious]
  52. // CHECK:STDERR: adapt ();
  53. // CHECK:STDERR: ^~~~~~~~~
  54. // CHECK:STDERR:
  55. adapt {};
  56. }
  57. class MultipleAdaptsSameType {
  58. adapt ();
  59. // CHECK:STDERR: fail_repeated.carbon:[[@LINE+7]]:3: error: multiple `adapt` declarations in class [AdaptDeclRepeated]
  60. // CHECK:STDERR: adapt ();
  61. // CHECK:STDERR: ^~~~~~~~~
  62. // CHECK:STDERR: fail_repeated.carbon:[[@LINE-4]]:3: note: previous `adapt` declaration is here [ClassSpecificDeclPrevious]
  63. // CHECK:STDERR: adapt ();
  64. // CHECK:STDERR: ^~~~~~~~~
  65. // CHECK:STDERR:
  66. adapt ();
  67. }
  68. // --- fail_bad_scope.carbon
  69. library "[[@TEST_NAME]]";
  70. // CHECK:STDERR: fail_bad_scope.carbon:[[@LINE+4]]:1: error: `adapt` declaration outside class [ClassSpecificDeclOutsideClass]
  71. // CHECK:STDERR: adapt {};
  72. // CHECK:STDERR: ^~~~~~~~~
  73. // CHECK:STDERR:
  74. adapt {};
  75. interface I {
  76. // CHECK:STDERR: fail_bad_scope.carbon:[[@LINE+4]]:3: error: `adapt` declaration outside class [ClassSpecificDeclOutsideClass]
  77. // CHECK:STDERR: adapt {};
  78. // CHECK:STDERR: ^~~~~~~~~
  79. // CHECK:STDERR:
  80. adapt {};
  81. }
  82. class C {
  83. interface I {
  84. // CHECK:STDERR: fail_bad_scope.carbon:[[@LINE+4]]:5: error: `adapt` declaration outside class [ClassSpecificDeclOutsideClass]
  85. // CHECK:STDERR: adapt {};
  86. // CHECK:STDERR: ^~~~~~~~~
  87. // CHECK:STDERR:
  88. adapt {};
  89. }
  90. }
  91. // CHECK:STDOUT: --- fail_not_type.carbon
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: constants {
  94. // CHECK:STDOUT: %Bad: type = class_type @Bad [concrete]
  95. // CHECK:STDOUT: %int_100: Core.IntLiteral = int_value 100 [concrete]
  96. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  97. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  98. // CHECK:STDOUT: %pattern_type.fc4: type = pattern_type %Bad [concrete]
  99. // CHECK:STDOUT: %Use.type: type = fn_type @Use [concrete]
  100. // CHECK:STDOUT: %Use: %Use.type = struct_value () [concrete]
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: imports {
  104. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  105. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  106. // CHECK:STDOUT: import Core//prelude
  107. // CHECK:STDOUT: import Core//prelude/...
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/operators/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: file {
  113. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  114. // CHECK:STDOUT: .Core = imports.%Core
  115. // CHECK:STDOUT: .Bad = %Bad.decl
  116. // CHECK:STDOUT: .Use = %Use.decl
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %Core.import = import Core
  119. // CHECK:STDOUT: %Bad.decl: type = class_decl @Bad [concrete = constants.%Bad] {} {}
  120. // CHECK:STDOUT: %Use.decl: %Use.type = fn_decl @Use [concrete = constants.%Use] {
  121. // CHECK:STDOUT: %b.patt: %pattern_type.fc4 = binding_pattern b [concrete]
  122. // CHECK:STDOUT: %b.param_patt: %pattern_type.fc4 = value_param_pattern %b.patt, call_param0 [concrete]
  123. // CHECK:STDOUT: } {
  124. // CHECK:STDOUT: %b.param: %Bad = value_param call_param0
  125. // CHECK:STDOUT: %Bad.ref: type = name_ref Bad, file.%Bad.decl [concrete = constants.%Bad]
  126. // CHECK:STDOUT: %b: %Bad = bind_name b, %b.param
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: class @Bad {
  131. // CHECK:STDOUT: %int_100: Core.IntLiteral = int_value 100 [concrete = constants.%int_100]
  132. // CHECK:STDOUT: %.loc12: type = converted %int_100, <error> [concrete = <error>]
  133. // CHECK:STDOUT: adapt_decl <error> [concrete]
  134. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness <error> [concrete = <error>]
  135. // CHECK:STDOUT: complete_type_witness = %complete_type
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !members:
  138. // CHECK:STDOUT: .Self = constants.%Bad
  139. // CHECK:STDOUT: .F = <poisoned>
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: fn @Use(%b.param: %Bad) {
  143. // CHECK:STDOUT: !entry:
  144. // CHECK:STDOUT: %b.ref: %Bad = name_ref b, %b
  145. // CHECK:STDOUT: %F.ref: <error> = name_ref F, <error> [concrete = <error>]
  146. // CHECK:STDOUT: return
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: --- fail_extend_not_type.carbon
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: constants {
  152. // CHECK:STDOUT: %Bad: type = class_type @Bad [concrete]
  153. // CHECK:STDOUT: %int_100: Core.IntLiteral = int_value 100 [concrete]
  154. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  155. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  156. // CHECK:STDOUT: %pattern_type.fc4: type = pattern_type %Bad [concrete]
  157. // CHECK:STDOUT: %Use.type: type = fn_type @Use [concrete]
  158. // CHECK:STDOUT: %Use: %Use.type = struct_value () [concrete]
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: imports {
  162. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  163. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  164. // CHECK:STDOUT: import Core//prelude
  165. // CHECK:STDOUT: import Core//prelude/...
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/operators/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: file {
  171. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  172. // CHECK:STDOUT: .Core = imports.%Core
  173. // CHECK:STDOUT: .Bad = %Bad.decl
  174. // CHECK:STDOUT: .Use = %Use.decl
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT: %Core.import = import Core
  177. // CHECK:STDOUT: %Bad.decl: type = class_decl @Bad [concrete = constants.%Bad] {} {}
  178. // CHECK:STDOUT: %Use.decl: %Use.type = fn_decl @Use [concrete = constants.%Use] {
  179. // CHECK:STDOUT: %b.patt: %pattern_type.fc4 = binding_pattern b [concrete]
  180. // CHECK:STDOUT: %b.param_patt: %pattern_type.fc4 = value_param_pattern %b.patt, call_param0 [concrete]
  181. // CHECK:STDOUT: } {
  182. // CHECK:STDOUT: %b.param: %Bad = value_param call_param0
  183. // CHECK:STDOUT: %Bad.ref: type = name_ref Bad, file.%Bad.decl [concrete = constants.%Bad]
  184. // CHECK:STDOUT: %b: %Bad = bind_name b, %b.param
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: class @Bad {
  189. // CHECK:STDOUT: %int_100: Core.IntLiteral = int_value 100 [concrete = constants.%int_100]
  190. // CHECK:STDOUT: %.loc12: type = converted %int_100, <error> [concrete = <error>]
  191. // CHECK:STDOUT: adapt_decl <error> [concrete]
  192. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness <error> [concrete = <error>]
  193. // CHECK:STDOUT: complete_type_witness = %complete_type
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: !members:
  196. // CHECK:STDOUT: .Self = constants.%Bad
  197. // CHECK:STDOUT: .F = <poisoned>
  198. // CHECK:STDOUT: extend <error>
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: fn @Use(%b.param: %Bad) {
  202. // CHECK:STDOUT: !entry:
  203. // CHECK:STDOUT: %b.ref: %Bad = name_ref b, %b
  204. // CHECK:STDOUT: %F.ref: <error> = name_ref F, <error> [concrete = <error>]
  205. // CHECK:STDOUT: return
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: --- fail_repeated.carbon
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: constants {
  211. // CHECK:STDOUT: %MultipleAdapts: type = class_type @MultipleAdapts [concrete]
  212. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  213. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  214. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_tuple.type [concrete]
  215. // CHECK:STDOUT: %MultipleAdaptsSameType: type = class_type @MultipleAdaptsSameType [concrete]
  216. // CHECK:STDOUT: }
  217. // CHECK:STDOUT:
  218. // CHECK:STDOUT: imports {
  219. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  220. // CHECK:STDOUT: import Core//prelude
  221. // CHECK:STDOUT: import Core//prelude/...
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: file {
  226. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  227. // CHECK:STDOUT: .Core = imports.%Core
  228. // CHECK:STDOUT: .MultipleAdapts = %MultipleAdapts.decl
  229. // CHECK:STDOUT: .MultipleAdaptsSameType = %MultipleAdaptsSameType.decl
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT: %Core.import = import Core
  232. // CHECK:STDOUT: %MultipleAdapts.decl: type = class_decl @MultipleAdapts [concrete = constants.%MultipleAdapts] {} {}
  233. // CHECK:STDOUT: %MultipleAdaptsSameType.decl: type = class_decl @MultipleAdaptsSameType [concrete = constants.%MultipleAdaptsSameType] {} {}
  234. // CHECK:STDOUT: }
  235. // CHECK:STDOUT:
  236. // CHECK:STDOUT: class @MultipleAdapts {
  237. // CHECK:STDOUT: %.loc5_10: %empty_tuple.type = tuple_literal ()
  238. // CHECK:STDOUT: %.loc5_11: type = converted %.loc5_10, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
  239. // CHECK:STDOUT: adapt_decl %.loc5_11 [concrete]
  240. // CHECK:STDOUT: %.loc13: %empty_struct_type = struct_literal ()
  241. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_tuple.type [concrete = constants.%complete_type]
  242. // CHECK:STDOUT: complete_type_witness = %complete_type
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: !members:
  245. // CHECK:STDOUT: .Self = constants.%MultipleAdapts
  246. // CHECK:STDOUT: }
  247. // CHECK:STDOUT:
  248. // CHECK:STDOUT: class @MultipleAdaptsSameType {
  249. // CHECK:STDOUT: %.loc17_10: %empty_tuple.type = tuple_literal ()
  250. // CHECK:STDOUT: %.loc17_11: type = converted %.loc17_10, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
  251. // CHECK:STDOUT: adapt_decl %.loc17_11 [concrete]
  252. // CHECK:STDOUT: %.loc25: %empty_tuple.type = tuple_literal ()
  253. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_tuple.type [concrete = constants.%complete_type]
  254. // CHECK:STDOUT: complete_type_witness = %complete_type
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: !members:
  257. // CHECK:STDOUT: .Self = constants.%MultipleAdaptsSameType
  258. // CHECK:STDOUT: }
  259. // CHECK:STDOUT:
  260. // CHECK:STDOUT: --- fail_bad_scope.carbon
  261. // CHECK:STDOUT:
  262. // CHECK:STDOUT: constants {
  263. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  264. // CHECK:STDOUT: %I.type.733: type = facet_type <@I.1> [concrete]
  265. // CHECK:STDOUT: %Self.826: %I.type.733 = bind_symbolic_name Self, 0 [symbolic]
  266. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  267. // CHECK:STDOUT: %I.type.e30: type = facet_type <@I.2> [concrete]
  268. // CHECK:STDOUT: %Self.6ef: %I.type.e30 = bind_symbolic_name Self, 0 [symbolic]
  269. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  270. // CHECK:STDOUT: }
  271. // CHECK:STDOUT:
  272. // CHECK:STDOUT: imports {
  273. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  274. // CHECK:STDOUT: import Core//prelude
  275. // CHECK:STDOUT: import Core//prelude/...
  276. // CHECK:STDOUT: }
  277. // CHECK:STDOUT: }
  278. // CHECK:STDOUT:
  279. // CHECK:STDOUT: file {
  280. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  281. // CHECK:STDOUT: .Core = imports.%Core
  282. // CHECK:STDOUT: .I = %I.decl
  283. // CHECK:STDOUT: .C = %C.decl
  284. // CHECK:STDOUT: }
  285. // CHECK:STDOUT: %Core.import = import Core
  286. // CHECK:STDOUT: %.loc8: %empty_struct_type = struct_literal ()
  287. // CHECK:STDOUT: %I.decl: type = interface_decl @I.1 [concrete = constants.%I.type.733] {} {}
  288. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  289. // CHECK:STDOUT: }
  290. // CHECK:STDOUT:
  291. // CHECK:STDOUT: interface @I.1 {
  292. // CHECK:STDOUT: %Self: %I.type.733 = bind_symbolic_name Self, 0 [symbolic = constants.%Self.826]
  293. // CHECK:STDOUT: %.loc15: %empty_struct_type = struct_literal ()
  294. // CHECK:STDOUT:
  295. // CHECK:STDOUT: !members:
  296. // CHECK:STDOUT: .Self = %Self
  297. // CHECK:STDOUT: witness = ()
  298. // CHECK:STDOUT: }
  299. // CHECK:STDOUT:
  300. // CHECK:STDOUT: interface @I.2 {
  301. // CHECK:STDOUT: %Self: %I.type.e30 = bind_symbolic_name Self, 0 [symbolic = constants.%Self.6ef]
  302. // CHECK:STDOUT: %.loc24: %empty_struct_type = struct_literal ()
  303. // CHECK:STDOUT:
  304. // CHECK:STDOUT: !members:
  305. // CHECK:STDOUT: .Self = %Self
  306. // CHECK:STDOUT: witness = ()
  307. // CHECK:STDOUT: }
  308. // CHECK:STDOUT:
  309. // CHECK:STDOUT: class @C {
  310. // CHECK:STDOUT: %I.decl: type = interface_decl @I.2 [concrete = constants.%I.type.e30] {} {}
  311. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  312. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  313. // CHECK:STDOUT: complete_type_witness = %complete_type
  314. // CHECK:STDOUT:
  315. // CHECK:STDOUT: !members:
  316. // CHECK:STDOUT: .Self = constants.%C
  317. // CHECK:STDOUT: .I = %I.decl
  318. // CHECK:STDOUT: }
  319. // CHECK:STDOUT: