symbolic.carbon 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/bool.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/eval/symbolic.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/eval/symbolic.carbon
  12. // --- symbolic_call_to_eval_fn.carbon
  13. library "[[@TEST_NAME]]";
  14. eval fn F(B:! bool) -> type {
  15. if (B) { return (); }
  16. return ({}, {});
  17. }
  18. fn G(B:! bool) {
  19. let unused n: F(B) = ({}, {});
  20. }
  21. fn H() {
  22. G(false);
  23. }
  24. // --- fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon
  25. library "[[@TEST_NAME]]";
  26. eval fn F(B:! bool) -> type {
  27. if (B) { return (); }
  28. return ({}, {});
  29. }
  30. fn G(B:! bool) {
  31. let unused n: F(B) = ({}, {});
  32. }
  33. // TODO: Provide a location for the "cannot initialize" error message.
  34. fn H() {
  35. // CHECK:STDERR: fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon:[[@LINE+5]]:3: error: unable to monomorphize specific `G(true)` [ResolvingSpecificHere]
  36. // CHECK:STDERR: G(true);
  37. // CHECK:STDERR: ^
  38. // CHECK:STDERR: fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon: note: cannot initialize tuple of 0 elements from tuple with 2 elements [TupleInitElementCountMismatch]
  39. // CHECK:STDERR:
  40. G(true);
  41. }
  42. // --- dependent_symbolic_instruction.carbon
  43. library "[[@TEST_NAME]]";
  44. interface Z {}
  45. impl {} as Z {}
  46. eval fn E(U:! Z) -> type {
  47. // This makes a FacetAccessType(U) instruction, for which we have to get a
  48. // constant value from the definition in the called function's specific. This
  49. // requires that the called function has its specific definition resolved.
  50. //@dump-sem-ir-begin
  51. return U;
  52. //@dump-sem-ir-end
  53. }
  54. fn F() {
  55. E({});
  56. }
  57. // CHECK:STDOUT: --- dependent_symbolic_instruction.carbon
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: constants {
  60. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  61. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  62. // CHECK:STDOUT: %Z.impl_witness: <witness> = impl_witness @empty_struct_type.as.Z.impl.%Z.impl_witness_table [concrete]
  63. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %empty_struct_type, (%Z.impl_witness) [concrete]
  64. // CHECK:STDOUT: %U.ea3: %Z.type = symbolic_binding U, 0 [symbolic]
  65. // CHECK:STDOUT: %U.as_type.829: type = facet_access_type %U.ea3 [symbolic]
  66. // CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
  67. // CHECK:STDOUT: %Copy.impl_witness.b47: <witness> = impl_witness imports.%Copy.impl_witness_table.b1c [concrete]
  68. // CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value type, (%Copy.impl_witness.b47) [concrete]
  69. // CHECK:STDOUT: %Copy.WithSelf.Op.type.a4f: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet) [concrete]
  70. // CHECK:STDOUT: %.070: type = fn_type_with_self_type %Copy.WithSelf.Op.type.a4f, %Copy.facet [concrete]
  71. // CHECK:STDOUT: %type.as.Copy.impl.Op.type: type = fn_type @type.as.Copy.impl.Op [concrete]
  72. // CHECK:STDOUT: %type.as.Copy.impl.Op: %type.as.Copy.impl.Op.type = struct_value () [concrete]
  73. // CHECK:STDOUT: %type.as.Copy.impl.Op.bound.500: <bound method> = bound_method %U.as_type.829, %type.as.Copy.impl.Op [symbolic]
  74. // CHECK:STDOUT: %type.as.Copy.impl.Op.bound.bca: <bound method> = bound_method %empty_struct_type, %type.as.Copy.impl.Op [concrete]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: imports {
  78. // CHECK:STDOUT: %Core.import_ref.1a7: %type.as.Copy.impl.Op.type = import_ref Core//prelude/parts/copy, loc{{\d+_\d+}}, loaded [concrete = constants.%type.as.Copy.impl.Op]
  79. // CHECK:STDOUT: %Copy.impl_witness_table.b1c = impl_witness_table (%Core.import_ref.1a7), @type.as.Copy.impl [concrete]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: generic fn @E(%U.loc6_12.2: %Z.type) {
  83. // CHECK:STDOUT: <elided>
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: !definition:
  86. // CHECK:STDOUT: %U.as_type.loc11_11.2: type = facet_access_type %U.loc6_12.1 [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.829)]
  87. // CHECK:STDOUT: %type.as.Copy.impl.Op.bound: <bound method> = bound_method %U.as_type.loc11_11.2, constants.%type.as.Copy.impl.Op [symbolic = %type.as.Copy.impl.Op.bound (constants.%type.as.Copy.impl.Op.bound.500)]
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn() -> out %return.param: type {
  90. // CHECK:STDOUT: !entry:
  91. // CHECK:STDOUT: %U.ref: %Z.type = name_ref U, %U.loc6_12.2 [symbolic = %U.loc6_12.1 (constants.%U.ea3)]
  92. // CHECK:STDOUT: %U.as_type.loc11_11.1: type = facet_access_type %U.ref [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.829)]
  93. // CHECK:STDOUT: %.loc11: type = converted %U.ref, %U.as_type.loc11_11.1 [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.829)]
  94. // CHECK:STDOUT: %impl.elem0: %.070 = impl_witness_access constants.%Copy.impl_witness.b47, element0 [concrete = constants.%type.as.Copy.impl.Op]
  95. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc11, %impl.elem0 [symbolic = %type.as.Copy.impl.Op.bound (constants.%type.as.Copy.impl.Op.bound.500)]
  96. // CHECK:STDOUT: %type.as.Copy.impl.Op.call: init type = call %bound_method(%.loc11) [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.829)]
  97. // CHECK:STDOUT: return %type.as.Copy.impl.Op.call
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: specific @E(constants.%U.ea3) {
  102. // CHECK:STDOUT: %U.loc6_12.1 => constants.%U.ea3
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: specific @E(constants.%Z.facet) {
  106. // CHECK:STDOUT: %U.loc6_12.1 => constants.%Z.facet
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: !definition:
  109. // CHECK:STDOUT: %U.as_type.loc11_11.2 => constants.%empty_struct_type
  110. // CHECK:STDOUT: %type.as.Copy.impl.Op.bound => constants.%type.as.Copy.impl.Op.bound.bca
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: