fail_numeric_literal_overflow.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/basics/fail_numeric_literal_overflow.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/basics/fail_numeric_literal_overflow.carbon
  10. // CHECK:STDERR: fail_numeric_literal_overflow.carbon:[[@LINE+4]]:14: error: integer value 39999999999999999993 too large for type `i32` [IntTooLargeForType]
  11. // CHECK:STDERR: let a: i32 = 39999999999999999993;
  12. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  13. // CHECK:STDERR:
  14. let a: i32 = 39999999999999999993;
  15. // CHECK:STDERR: fail_numeric_literal_overflow.carbon:[[@LINE+4]]:14: error: integer value 2147483648 too large for type `i32` [IntTooLargeForType]
  16. // CHECK:STDERR: let b: i32 = 2_147_483_648;
  17. // CHECK:STDERR: ^~~~~~~~~~~~~
  18. // CHECK:STDERR:
  19. let b: i32 = 2_147_483_648;
  20. // CHECK:STDERR: fail_numeric_literal_overflow.carbon:[[@LINE+4]]:14: error: integer value 2147483648 too large for type `i32` [IntTooLargeForType]
  21. // CHECK:STDERR: let c: i32 = 0x8000_0000;
  22. // CHECK:STDERR: ^~~~~~~~~~~
  23. // CHECK:STDERR:
  24. let c: i32 = 0x8000_0000;
  25. // CHECK:STDERR: fail_numeric_literal_overflow.carbon:[[@LINE+4]]:14: error: real mantissa with value 399999999999999999930 does not fit in i64 [RealMantissaTooLargeForI64]
  26. // CHECK:STDERR: let d: f64 = 39999999999999999993.0e3;
  27. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  28. // CHECK:STDERR:
  29. let d: f64 = 39999999999999999993.0e3;
  30. // CHECK:STDERR: fail_numeric_literal_overflow.carbon:[[@LINE+4]]:14: error: real exponent with value 39999999999999999992 does not fit in i64 [RealExponentTooLargeForI64]
  31. // CHECK:STDERR: let e: f64 = 5.0e39999999999999999993;
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  33. // CHECK:STDERR:
  34. let e: f64 = 5.0e39999999999999999993;
  35. // CHECK:STDOUT: --- fail_numeric_literal_overflow.carbon
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: constants {
  38. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  39. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  40. // CHECK:STDOUT: %int_39999999999999999993.af6: Core.IntLiteral = int_value 39999999999999999993 [concrete]
  41. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  42. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  43. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.4f9(%int_32) [concrete]
  44. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  45. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  46. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%impl_witness.d39) [concrete]
  47. // CHECK:STDOUT: %.be7: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  48. // CHECK:STDOUT: %Convert.bound.7ef: <bound method> = bound_method %int_39999999999999999993.af6, %Convert.956 [concrete]
  49. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  50. // CHECK:STDOUT: %bound_method.75f: <bound method> = bound_method %int_39999999999999999993.af6, %Convert.specific_fn [concrete]
  51. // CHECK:STDOUT: %int_39999999999999999993.dee: %i32 = int_value 39999999999999999993 [concrete]
  52. // CHECK:STDOUT: %int_2147483648.1db: Core.IntLiteral = int_value 2147483648 [concrete]
  53. // CHECK:STDOUT: %Convert.bound.85f: <bound method> = bound_method %int_2147483648.1db, %Convert.956 [concrete]
  54. // CHECK:STDOUT: %bound_method.f79: <bound method> = bound_method %int_2147483648.1db, %Convert.specific_fn [concrete]
  55. // CHECK:STDOUT: %int_2147483648.8df: %i32 = int_value 2147483648 [concrete]
  56. // CHECK:STDOUT: %int_64: Core.IntLiteral = int_value 64 [concrete]
  57. // CHECK:STDOUT: %Float.type: type = fn_type @Float [concrete]
  58. // CHECK:STDOUT: %Float: %Float.type = struct_value () [concrete]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: imports {
  62. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  63. // CHECK:STDOUT: .Int = %Core.Int
  64. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  65. // CHECK:STDOUT: .Float = %Core.Float
  66. // CHECK:STDOUT: import Core//prelude
  67. // CHECK:STDOUT: import Core//prelude/...
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: file {
  72. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  73. // CHECK:STDOUT: .Core = imports.%Core
  74. // CHECK:STDOUT: .a = %a
  75. // CHECK:STDOUT: .b = %b
  76. // CHECK:STDOUT: .c = %c
  77. // CHECK:STDOUT: .d = %d
  78. // CHECK:STDOUT: .e = %e
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %Core.import = import Core
  81. // CHECK:STDOUT: name_binding_decl {
  82. // CHECK:STDOUT: %a.patt: %i32 = binding_pattern a
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %.loc15_8: type = splice_block %i32.loc15 [concrete = constants.%i32] {
  85. // CHECK:STDOUT: %int_32.loc15: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  86. // CHECK:STDOUT: %i32.loc15: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %impl.elem0.loc15: %.be7 = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  89. // CHECK:STDOUT: %bound_method.loc15_14.1: <bound method> = bound_method @__global_init.%int_39999999999999999993, %impl.elem0.loc15 [concrete = constants.%Convert.bound.7ef]
  90. // CHECK:STDOUT: %specific_fn.loc15: <specific function> = specific_function %impl.elem0.loc15, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  91. // CHECK:STDOUT: %bound_method.loc15_14.2: <bound method> = bound_method @__global_init.%int_39999999999999999993, %specific_fn.loc15 [concrete = constants.%bound_method.75f]
  92. // CHECK:STDOUT: %int.convert_checked.loc15: init %i32 = call %bound_method.loc15_14.2(@__global_init.%int_39999999999999999993) [concrete = constants.%int_39999999999999999993.dee]
  93. // CHECK:STDOUT: %.loc15_14.1: %i32 = value_of_initializer %int.convert_checked.loc15 [concrete = constants.%int_39999999999999999993.dee]
  94. // CHECK:STDOUT: %.loc15_14.2: %i32 = converted @__global_init.%int_39999999999999999993, %.loc15_14.1 [concrete = constants.%int_39999999999999999993.dee]
  95. // CHECK:STDOUT: %a: %i32 = bind_name a, %.loc15_14.2
  96. // CHECK:STDOUT: name_binding_decl {
  97. // CHECK:STDOUT: %b.patt: %i32 = binding_pattern b
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %.loc21_8: type = splice_block %i32.loc21 [concrete = constants.%i32] {
  100. // CHECK:STDOUT: %int_32.loc21: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  101. // CHECK:STDOUT: %i32.loc21: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %impl.elem0.loc21: %.be7 = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  104. // CHECK:STDOUT: %bound_method.loc21_14.1: <bound method> = bound_method @__global_init.%int_2147483648.loc21, %impl.elem0.loc21 [concrete = constants.%Convert.bound.85f]
  105. // CHECK:STDOUT: %specific_fn.loc21: <specific function> = specific_function %impl.elem0.loc21, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  106. // CHECK:STDOUT: %bound_method.loc21_14.2: <bound method> = bound_method @__global_init.%int_2147483648.loc21, %specific_fn.loc21 [concrete = constants.%bound_method.f79]
  107. // CHECK:STDOUT: %int.convert_checked.loc21: init %i32 = call %bound_method.loc21_14.2(@__global_init.%int_2147483648.loc21) [concrete = constants.%int_2147483648.8df]
  108. // CHECK:STDOUT: %.loc21_14.1: %i32 = value_of_initializer %int.convert_checked.loc21 [concrete = constants.%int_2147483648.8df]
  109. // CHECK:STDOUT: %.loc21_14.2: %i32 = converted @__global_init.%int_2147483648.loc21, %.loc21_14.1 [concrete = constants.%int_2147483648.8df]
  110. // CHECK:STDOUT: %b: %i32 = bind_name b, %.loc21_14.2
  111. // CHECK:STDOUT: name_binding_decl {
  112. // CHECK:STDOUT: %c.patt: %i32 = binding_pattern c
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %.loc27_8: type = splice_block %i32.loc27 [concrete = constants.%i32] {
  115. // CHECK:STDOUT: %int_32.loc27: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  116. // CHECK:STDOUT: %i32.loc27: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %impl.elem0.loc27: %.be7 = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  119. // CHECK:STDOUT: %bound_method.loc27_14.1: <bound method> = bound_method @__global_init.%int_2147483648.loc27, %impl.elem0.loc27 [concrete = constants.%Convert.bound.85f]
  120. // CHECK:STDOUT: %specific_fn.loc27: <specific function> = specific_function %impl.elem0.loc27, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  121. // CHECK:STDOUT: %bound_method.loc27_14.2: <bound method> = bound_method @__global_init.%int_2147483648.loc27, %specific_fn.loc27 [concrete = constants.%bound_method.f79]
  122. // CHECK:STDOUT: %int.convert_checked.loc27: init %i32 = call %bound_method.loc27_14.2(@__global_init.%int_2147483648.loc27) [concrete = constants.%int_2147483648.8df]
  123. // CHECK:STDOUT: %.loc27_14.1: %i32 = value_of_initializer %int.convert_checked.loc27 [concrete = constants.%int_2147483648.8df]
  124. // CHECK:STDOUT: %.loc27_14.2: %i32 = converted @__global_init.%int_2147483648.loc27, %.loc27_14.1 [concrete = constants.%int_2147483648.8df]
  125. // CHECK:STDOUT: %c: %i32 = bind_name c, %.loc27_14.2
  126. // CHECK:STDOUT: name_binding_decl {
  127. // CHECK:STDOUT: %d.patt: f64 = binding_pattern d
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: %.loc33_8.1: type = splice_block %.loc33_8.3 [concrete = f64] {
  130. // CHECK:STDOUT: %int_64.loc33: Core.IntLiteral = int_value 64 [concrete = constants.%int_64]
  131. // CHECK:STDOUT: %float.make_type.loc33: init type = call constants.%Float(%int_64.loc33) [concrete = f64]
  132. // CHECK:STDOUT: %.loc33_8.2: type = value_of_initializer %float.make_type.loc33 [concrete = f64]
  133. // CHECK:STDOUT: %.loc33_8.3: type = converted %float.make_type.loc33, %.loc33_8.2 [concrete = f64]
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT: %d: f64 = bind_name d, <error>
  136. // CHECK:STDOUT: name_binding_decl {
  137. // CHECK:STDOUT: %e.patt: f64 = binding_pattern e
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: %.loc39_8.1: type = splice_block %.loc39_8.3 [concrete = f64] {
  140. // CHECK:STDOUT: %int_64.loc39: Core.IntLiteral = int_value 64 [concrete = constants.%int_64]
  141. // CHECK:STDOUT: %float.make_type.loc39: init type = call constants.%Float(%int_64.loc39) [concrete = f64]
  142. // CHECK:STDOUT: %.loc39_8.2: type = value_of_initializer %float.make_type.loc39 [concrete = f64]
  143. // CHECK:STDOUT: %.loc39_8.3: type = converted %float.make_type.loc39, %.loc39_8.2 [concrete = f64]
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: %e: f64 = bind_name e, <error>
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: fn @__global_init() {
  149. // CHECK:STDOUT: !entry:
  150. // CHECK:STDOUT: %int_39999999999999999993: Core.IntLiteral = int_value 39999999999999999993 [concrete = constants.%int_39999999999999999993.af6]
  151. // CHECK:STDOUT: %int_2147483648.loc21: Core.IntLiteral = int_value 2147483648 [concrete = constants.%int_2147483648.1db]
  152. // CHECK:STDOUT: %int_2147483648.loc27: Core.IntLiteral = int_value 2147483648 [concrete = constants.%int_2147483648.1db]
  153. // CHECK:STDOUT: return
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT: