resolve_used.carbon 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/function/generic/resolve_used.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/generic/resolve_used.carbon
  10. // --- fail_call_monomorphization_error.carbon
  11. library "[[@TEST_NAME]]";
  12. fn ErrorIfNIsZero(N:! i32) {
  13. // Check that we resolve the definition of a used specific function by
  14. // ensuring we produce an error when doing so. Notionally this error is
  15. // produced as a result of instantiating the `Core.Int` template, although
  16. // that's not how we currently model `Core.Int`.
  17. // CHECK:STDERR: fail_call_monomorphization_error.carbon:[[@LINE+3]]:10: error: integer type width of 0 is not positive
  18. // CHECK:STDERR: var v: Core.Int(N);
  19. // CHECK:STDERR: ^~~~~~~~~
  20. var v: Core.Int(N);
  21. }
  22. fn CallNegative() {
  23. ErrorIfNIsZero(0);
  24. }
  25. // CHECK:STDOUT: --- fail_call_monomorphization_error.carbon
  26. // CHECK:STDOUT:
  27. // CHECK:STDOUT: constants {
  28. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  29. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  30. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  31. // CHECK:STDOUT: %N: i32 = bind_symbolic_name N, 0 [symbolic]
  32. // CHECK:STDOUT: %N.patt: i32 = symbolic_binding_pattern N, 0 [symbolic]
  33. // CHECK:STDOUT: %ErrorIfNIsZero.type: type = fn_type @ErrorIfNIsZero [template]
  34. // CHECK:STDOUT: %ErrorIfNIsZero: %ErrorIfNIsZero.type = struct_value () [template]
  35. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  36. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  37. // CHECK:STDOUT: %.2: type = int_type signed, %N [symbolic]
  38. // CHECK:STDOUT: %CallNegative.type: type = fn_type @CallNegative [template]
  39. // CHECK:STDOUT: %CallNegative: %CallNegative.type = struct_value () [template]
  40. // CHECK:STDOUT: %.3: i32 = int_literal 0 [template]
  41. // CHECK:STDOUT: %.4: <specific function> = specific_function %ErrorIfNIsZero, @ErrorIfNIsZero(%.3) [template]
  42. // CHECK:STDOUT: }
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: imports {
  45. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  46. // CHECK:STDOUT: .Int32 = %import_ref.1
  47. // CHECK:STDOUT: .Int = %import_ref.2
  48. // CHECK:STDOUT: import Core//prelude
  49. // CHECK:STDOUT: import Core//prelude/operators
  50. // CHECK:STDOUT: import Core//prelude/types
  51. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  52. // CHECK:STDOUT: import Core//prelude/operators/as
  53. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  54. // CHECK:STDOUT: import Core//prelude/operators/comparison
  55. // CHECK:STDOUT: import Core//prelude/types/bool
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  58. // CHECK:STDOUT: %import_ref.2: %Int.type = import_ref Core//prelude/types, inst+20, loaded [template = constants.%Int]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: file {
  62. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  63. // CHECK:STDOUT: .Core = imports.%Core
  64. // CHECK:STDOUT: .ErrorIfNIsZero = %ErrorIfNIsZero.decl
  65. // CHECK:STDOUT: .CallNegative = %CallNegative.decl
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Core.import = import Core
  68. // CHECK:STDOUT: %ErrorIfNIsZero.decl: %ErrorIfNIsZero.type = fn_decl @ErrorIfNIsZero [template = constants.%ErrorIfNIsZero] {
  69. // CHECK:STDOUT: %N.patt.loc4_19.1: i32 = symbolic_binding_pattern N, 0 [symbolic = %N.patt.loc4_19.2 (constants.%N.patt)]
  70. // CHECK:STDOUT: %N.param_patt: i32 = param_pattern %N.patt.loc4_19.1, runtime_param<invalid> [symbolic = %N.patt.loc4_19.2 (constants.%N.patt)]
  71. // CHECK:STDOUT: } {
  72. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  73. // CHECK:STDOUT: %.loc4_23.1: type = value_of_initializer %int.make_type_32 [template = i32]
  74. // CHECK:STDOUT: %.loc4_23.2: type = converted %int.make_type_32, %.loc4_23.1 [template = i32]
  75. // CHECK:STDOUT: %param: i32 = param runtime_param<invalid>
  76. // CHECK:STDOUT: %N.loc4_19.1: i32 = bind_symbolic_name N, 0, %param [symbolic = %N.loc4_19.2 (constants.%N)]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %CallNegative.decl: %CallNegative.type = fn_decl @CallNegative [template = constants.%CallNegative] {} {}
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: generic fn @ErrorIfNIsZero(%N.loc4_19.1: i32) {
  84. // CHECK:STDOUT: %N.loc4_19.2: i32 = bind_symbolic_name N, 0 [symbolic = %N.loc4_19.2 (constants.%N)]
  85. // CHECK:STDOUT: %N.patt.loc4_19.2: i32 = symbolic_binding_pattern N, 0 [symbolic = %N.patt.loc4_19.2 (constants.%N.patt)]
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: !definition:
  88. // CHECK:STDOUT: %.loc12_18: type = int_type signed, %N.loc4_19.2 [symbolic = %.loc12_18 (constants.%.2)]
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: fn(%N.param_patt: i32) {
  91. // CHECK:STDOUT: !entry:
  92. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  93. // CHECK:STDOUT: %Int.ref: %Int.type = name_ref Int, imports.%import_ref.2 [template = constants.%Int]
  94. // CHECK:STDOUT: %N.ref: i32 = name_ref N, %N.loc4_19.1 [symbolic = %N.loc4_19.2 (constants.%N)]
  95. // CHECK:STDOUT: %int.make_type_signed: init type = call %Int.ref(%N.ref) [symbolic = %.loc12_18 (constants.%.2)]
  96. // CHECK:STDOUT: %.loc12_20.1: type = value_of_initializer %int.make_type_signed [symbolic = %.loc12_18 (constants.%.2)]
  97. // CHECK:STDOUT: %.loc12_20.2: type = converted %int.make_type_signed, %.loc12_20.1 [symbolic = %.loc12_18 (constants.%.2)]
  98. // CHECK:STDOUT: %v.var: ref @ErrorIfNIsZero.%.loc12_18 (%.2) = var v
  99. // CHECK:STDOUT: %v: ref @ErrorIfNIsZero.%.loc12_18 (%.2) = bind_name v, %v.var
  100. // CHECK:STDOUT: return
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: fn @Int(%size.param_patt: i32) -> type = "int.make_type_signed";
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: fn @CallNegative() {
  107. // CHECK:STDOUT: !entry:
  108. // CHECK:STDOUT: %ErrorIfNIsZero.ref: %ErrorIfNIsZero.type = name_ref ErrorIfNIsZero, file.%ErrorIfNIsZero.decl [template = constants.%ErrorIfNIsZero]
  109. // CHECK:STDOUT: %.loc16_18: i32 = int_literal 0 [template = constants.%.3]
  110. // CHECK:STDOUT: %.loc16_3: <specific function> = specific_function %ErrorIfNIsZero.ref, @ErrorIfNIsZero(constants.%.3) [template = constants.%.4]
  111. // CHECK:STDOUT: %ErrorIfNIsZero.call: init %.1 = call %.loc16_3()
  112. // CHECK:STDOUT: return
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: specific @ErrorIfNIsZero(constants.%N) {
  116. // CHECK:STDOUT: %N.loc4_19.2 => constants.%N
  117. // CHECK:STDOUT: %N.patt.loc4_19.2 => constants.%N
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: specific @ErrorIfNIsZero(constants.%.3) {
  121. // CHECK:STDOUT: %N.loc4_19.2 => constants.%.3
  122. // CHECK:STDOUT: %N.patt.loc4_19.2 => constants.%.3
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: !definition:
  125. // CHECK:STDOUT: %.loc12_18 => <error>
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: