fail_generic.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/let/fail_generic.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/let/fail_generic.carbon
  10. // TODO: Should this be valid?
  11. fn F(a: i32) -> i32 {
  12. let T:! type = i32;
  13. // CHECK:STDERR: fail_generic.carbon:[[@LINE+7]]:3: error: cannot implicitly convert from `i32` to `T`
  14. // CHECK:STDERR: let x: T = 5;
  15. // CHECK:STDERR: ^~~~~~~~~~~~~
  16. // CHECK:STDERR: fail_generic.carbon:[[@LINE+4]]:3: note: type `i32` does not implement interface `ImplicitAs`
  17. // CHECK:STDERR: let x: T = 5;
  18. // CHECK:STDERR: ^~~~~~~~~~~~~
  19. // CHECK:STDERR:
  20. let x: T = 5;
  21. // CHECK:STDERR: fail_generic.carbon:[[@LINE+6]]:3: error: cannot implicitly convert from `T` to `i32`
  22. // CHECK:STDERR: return x;
  23. // CHECK:STDERR: ^~~~~~~~~
  24. // CHECK:STDERR: fail_generic.carbon:[[@LINE+3]]:3: note: type `<error>` does not implement interface `ImplicitAs`
  25. // CHECK:STDERR: return x;
  26. // CHECK:STDERR: ^~~~~~~~~
  27. return x;
  28. }
  29. // CHECK:STDOUT: --- fail_generic.carbon
  30. // CHECK:STDOUT:
  31. // CHECK:STDOUT: constants {
  32. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  33. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  34. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  35. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  36. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  37. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  38. // CHECK:STDOUT: %.2: i32 = int_literal 5 [template]
  39. // CHECK:STDOUT: %ImplicitAs.type.1: type = generic_interface_type @ImplicitAs [template]
  40. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type.1 = struct_value () [template]
  41. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic]
  42. // CHECK:STDOUT: %ImplicitAs.type.2: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  43. // CHECK:STDOUT: %Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2) = bind_symbolic_name Self, 1 [symbolic]
  44. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic]
  45. // CHECK:STDOUT: %Self.2: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic]
  46. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
  47. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  48. // CHECK:STDOUT: %.3: type = assoc_entity_type %ImplicitAs.type.2, %Convert.type.1 [symbolic]
  49. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  50. // CHECK:STDOUT: %ImplicitAs.type.3: type = interface_type @ImplicitAs, @ImplicitAs(%T) [symbolic]
  51. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(%T) [symbolic]
  52. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [symbolic]
  53. // CHECK:STDOUT: %.5: type = assoc_entity_type %ImplicitAs.type.3, %Convert.type.2 [symbolic]
  54. // CHECK:STDOUT: %.6: %.5 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  55. // CHECK:STDOUT: %.7: %.3 = assoc_entity element0, imports.%import_ref.7 [symbolic]
  56. // CHECK:STDOUT: %ImplicitAs.type.4: type = interface_type @ImplicitAs, @ImplicitAs(i32) [template]
  57. // CHECK:STDOUT: %Convert.type.3: type = fn_type @Convert, @ImplicitAs(i32) [template]
  58. // CHECK:STDOUT: %Convert.3: %Convert.type.3 = struct_value () [template]
  59. // CHECK:STDOUT: %.8: type = assoc_entity_type %ImplicitAs.type.4, %Convert.type.3 [template]
  60. // CHECK:STDOUT: %.9: %.8 = assoc_entity element0, imports.%import_ref.6 [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/operators
  69. // CHECK:STDOUT: import Core//prelude/types
  70. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  71. // CHECK:STDOUT: import Core//prelude/operators/as
  72. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  73. // CHECK:STDOUT: import Core//prelude/operators/comparison
  74. // CHECK:STDOUT: import Core//prelude/types/bool
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  77. // CHECK:STDOUT: %import_ref.2: %ImplicitAs.type.1 = import_ref Core//prelude/operators/as, inst+46, loaded [template = constants.%ImplicitAs]
  78. // CHECK:STDOUT: %import_ref.3 = import_ref Core//prelude/operators/as, inst+52, unloaded
  79. // CHECK:STDOUT: %import_ref.4: @ImplicitAs.%.1 (%.3) = import_ref Core//prelude/operators/as, inst+71, loaded [symbolic = @ImplicitAs.%.2 (constants.%.7)]
  80. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+64, unloaded
  81. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+64, unloaded
  82. // CHECK:STDOUT: %import_ref.7 = import_ref Core//prelude/operators/as, inst+64, unloaded
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: file {
  86. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  87. // CHECK:STDOUT: .Core = imports.%Core
  88. // CHECK:STDOUT: .F = %F.decl
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %Core.import = import Core
  91. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  92. // CHECK:STDOUT: %a.patt: i32 = binding_pattern a
  93. // CHECK:STDOUT: %a.param_patt: i32 = param_pattern %a.patt, runtime_param0
  94. // CHECK:STDOUT: } {
  95. // CHECK:STDOUT: %int.make_type_32.loc12_9: init type = call constants.%Int32() [template = i32]
  96. // CHECK:STDOUT: %.loc12_9.1: type = value_of_initializer %int.make_type_32.loc12_9 [template = i32]
  97. // CHECK:STDOUT: %.loc12_9.2: type = converted %int.make_type_32.loc12_9, %.loc12_9.1 [template = i32]
  98. // CHECK:STDOUT: %int.make_type_32.loc12_17: init type = call constants.%Int32() [template = i32]
  99. // CHECK:STDOUT: %.loc12_17.1: type = value_of_initializer %int.make_type_32.loc12_17 [template = i32]
  100. // CHECK:STDOUT: %.loc12_17.2: type = converted %int.make_type_32.loc12_17, %.loc12_17.1 [template = i32]
  101. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  102. // CHECK:STDOUT: %param: i32 = param runtime_param0
  103. // CHECK:STDOUT: %a: i32 = bind_name a, %param
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  108. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  109. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic = %Dest.patt (constants.%Dest.patt)]
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: !definition:
  112. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  113. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  114. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  115. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  116. // CHECK:STDOUT: %.1: type = assoc_entity_type @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.1 (constants.%.3)]
  117. // CHECK:STDOUT: %.2: @ImplicitAs.%.1 (%.3) = assoc_entity element0, imports.%import_ref.6 [symbolic = %.2 (constants.%.4)]
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: interface {
  120. // CHECK:STDOUT: !members:
  121. // CHECK:STDOUT: .Self = imports.%import_ref.3
  122. // CHECK:STDOUT: .Convert = imports.%import_ref.4
  123. // CHECK:STDOUT: witness = (imports.%import_ref.5)
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: fn @F(%a.param_patt: i32) -> i32 {
  130. // CHECK:STDOUT: !entry:
  131. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  132. // CHECK:STDOUT: %.loc13_21.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  133. // CHECK:STDOUT: %.loc13_21.2: type = converted %int.make_type_32.loc13, %.loc13_21.1 [template = i32]
  134. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0, %.loc13_21.2 [symbolic = constants.%T]
  135. // CHECK:STDOUT: %T.ref: type = name_ref T, %T [symbolic = constants.%T]
  136. // CHECK:STDOUT: %.loc21_14: i32 = int_literal 5 [template = constants.%.2]
  137. // CHECK:STDOUT: %ImplicitAs.type.loc21: type = interface_type @ImplicitAs, @ImplicitAs(constants.%T) [symbolic = constants.%ImplicitAs.type.3]
  138. // CHECK:STDOUT: %.loc21_15.1: %.5 = specific_constant imports.%import_ref.4, @ImplicitAs(constants.%T) [symbolic = constants.%.6]
  139. // CHECK:STDOUT: %Convert.ref.loc21: %.5 = name_ref Convert, %.loc21_15.1 [symbolic = constants.%.6]
  140. // CHECK:STDOUT: %.loc21_15.2: %T = converted %.loc21_14, <error> [template = <error>]
  141. // CHECK:STDOUT: %x: %T = bind_name x, <error>
  142. // CHECK:STDOUT: %x.ref: %T = name_ref x, %x
  143. // CHECK:STDOUT: %ImplicitAs.type.loc28: type = interface_type @ImplicitAs, @ImplicitAs(i32) [template = constants.%ImplicitAs.type.4]
  144. // CHECK:STDOUT: %.loc28_11.1: %.8 = specific_constant imports.%import_ref.4, @ImplicitAs(i32) [template = constants.%.9]
  145. // CHECK:STDOUT: %Convert.ref.loc28: %.8 = name_ref Convert, %.loc28_11.1 [template = constants.%.9]
  146. // CHECK:STDOUT: %.loc28_11.2: i32 = converted %x.ref, <error> [template = <error>]
  147. // CHECK:STDOUT: return <error>
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2)) {
  151. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  152. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  153. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: fn[%self.param_patt: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  159. // CHECK:STDOUT: %Dest => constants.%Dest
  160. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  164. // CHECK:STDOUT: %Dest => constants.%Dest
  165. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  169. // CHECK:STDOUT: %Dest => constants.%Dest
  170. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  174. // CHECK:STDOUT: %Dest => constants.%Dest
  175. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.2
  176. // CHECK:STDOUT: %Self => constants.%Self.1
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: specific @ImplicitAs(constants.%T) {
  180. // CHECK:STDOUT: %Dest => constants.%T
  181. // CHECK:STDOUT: %Dest.patt => constants.%T
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: !definition:
  184. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.3
  185. // CHECK:STDOUT: %Self => constants.%Self.2
  186. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  187. // CHECK:STDOUT: %Convert => constants.%Convert.2
  188. // CHECK:STDOUT: %.1 => constants.%.5
  189. // CHECK:STDOUT: %.2 => constants.%.6
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: specific @ImplicitAs(i32) {
  193. // CHECK:STDOUT: %Dest => i32
  194. // CHECK:STDOUT: %Dest.patt => i32
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: !definition:
  197. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.4
  198. // CHECK:STDOUT: %Self => constants.%Self.2
  199. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.3
  200. // CHECK:STDOUT: %Convert => constants.%Convert.3
  201. // CHECK:STDOUT: %.1 => constants.%.8
  202. // CHECK:STDOUT: %.2 => constants.%.9
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT: