fail_numeric_literal_overflow.carbon 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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]]:1: 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]]:1: 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]]:1: 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+3]]: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. let e: f64 = 5.0e39999999999999999993;
  34. // CHECK:STDOUT: --- fail_numeric_literal_overflow.carbon
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: constants {
  37. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  38. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  39. // CHECK:STDOUT: %int_39999999999999999993.af6: Core.IntLiteral = int_value 39999999999999999993 [template]
  40. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  41. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.1(%int_32) [template]
  42. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  43. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  44. // CHECK:STDOUT: %Convert.bound.212: <bound method> = bound_method %int_39999999999999999993.af6, %Convert.925 [template]
  45. // CHECK:STDOUT: %Convert.specific_fn.e77: <specific function> = specific_function %Convert.bound.212, @Convert.2(%int_32) [template]
  46. // CHECK:STDOUT: %int_39999999999999999993.2c0: %i32 = int_value 39999999999999999993 [template]
  47. // CHECK:STDOUT: %int_2147483648.1db: Core.IntLiteral = int_value 2147483648 [template]
  48. // CHECK:STDOUT: %Convert.bound.dbd: <bound method> = bound_method %int_2147483648.1db, %Convert.925 [template]
  49. // CHECK:STDOUT: %Convert.specific_fn.b90: <specific function> = specific_function %Convert.bound.dbd, @Convert.2(%int_32) [template]
  50. // CHECK:STDOUT: %int_2147483648.928: %i32 = int_value 2147483648 [template]
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: imports {
  54. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  55. // CHECK:STDOUT: .Int = %import_ref.187
  56. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  57. // CHECK:STDOUT: .Float = %import_ref.20bd
  58. // CHECK:STDOUT: import Core//prelude
  59. // CHECK:STDOUT: import Core//prelude/...
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  65. // CHECK:STDOUT: .Core = imports.%Core
  66. // CHECK:STDOUT: .a = @__global_init.%a
  67. // CHECK:STDOUT: .b = @__global_init.%b
  68. // CHECK:STDOUT: .c = @__global_init.%c
  69. // CHECK:STDOUT: .d = @__global_init.%d
  70. // CHECK:STDOUT: .e = @__global_init.%e
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Core.import = import Core
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: fn @__global_init() {
  76. // CHECK:STDOUT: !entry:
  77. // CHECK:STDOUT: %int_39999999999999999993: Core.IntLiteral = int_value 39999999999999999993 [template = constants.%int_39999999999999999993.af6]
  78. // CHECK:STDOUT: %impl.elem0.loc15: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  79. // CHECK:STDOUT: %Convert.bound.loc15: <bound method> = bound_method %int_39999999999999999993, %impl.elem0.loc15 [template = constants.%Convert.bound.212]
  80. // CHECK:STDOUT: %Convert.specific_fn.loc15: <specific function> = specific_function %Convert.bound.loc15, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.e77]
  81. // CHECK:STDOUT: %int.convert_checked.loc15: init %i32 = call %Convert.specific_fn.loc15(%int_39999999999999999993) [template = constants.%int_39999999999999999993.2c0]
  82. // CHECK:STDOUT: %.loc15_34.1: %i32 = value_of_initializer %int.convert_checked.loc15 [template = constants.%int_39999999999999999993.2c0]
  83. // CHECK:STDOUT: %.loc15_34.2: %i32 = converted %int_39999999999999999993, %.loc15_34.1 [template = constants.%int_39999999999999999993.2c0]
  84. // CHECK:STDOUT: %a: %i32 = bind_name a, %.loc15_34.2
  85. // CHECK:STDOUT: %int_2147483648.loc21: Core.IntLiteral = int_value 2147483648 [template = constants.%int_2147483648.1db]
  86. // CHECK:STDOUT: %impl.elem0.loc21: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  87. // CHECK:STDOUT: %Convert.bound.loc21: <bound method> = bound_method %int_2147483648.loc21, %impl.elem0.loc21 [template = constants.%Convert.bound.dbd]
  88. // CHECK:STDOUT: %Convert.specific_fn.loc21: <specific function> = specific_function %Convert.bound.loc21, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b90]
  89. // CHECK:STDOUT: %int.convert_checked.loc21: init %i32 = call %Convert.specific_fn.loc21(%int_2147483648.loc21) [template = constants.%int_2147483648.928]
  90. // CHECK:STDOUT: %.loc21_27.1: %i32 = value_of_initializer %int.convert_checked.loc21 [template = constants.%int_2147483648.928]
  91. // CHECK:STDOUT: %.loc21_27.2: %i32 = converted %int_2147483648.loc21, %.loc21_27.1 [template = constants.%int_2147483648.928]
  92. // CHECK:STDOUT: %b: %i32 = bind_name b, %.loc21_27.2
  93. // CHECK:STDOUT: %int_2147483648.loc27: Core.IntLiteral = int_value 2147483648 [template = constants.%int_2147483648.1db]
  94. // CHECK:STDOUT: %impl.elem0.loc27: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  95. // CHECK:STDOUT: %Convert.bound.loc27: <bound method> = bound_method %int_2147483648.loc27, %impl.elem0.loc27 [template = constants.%Convert.bound.dbd]
  96. // CHECK:STDOUT: %Convert.specific_fn.loc27: <specific function> = specific_function %Convert.bound.loc27, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b90]
  97. // CHECK:STDOUT: %int.convert_checked.loc27: init %i32 = call %Convert.specific_fn.loc27(%int_2147483648.loc27) [template = constants.%int_2147483648.928]
  98. // CHECK:STDOUT: %.loc27_25.1: %i32 = value_of_initializer %int.convert_checked.loc27 [template = constants.%int_2147483648.928]
  99. // CHECK:STDOUT: %.loc27_25.2: %i32 = converted %int_2147483648.loc27, %.loc27_25.1 [template = constants.%int_2147483648.928]
  100. // CHECK:STDOUT: %c: %i32 = bind_name c, %.loc27_25.2
  101. // CHECK:STDOUT: %d: f64 = bind_name d, <error>
  102. // CHECK:STDOUT: %e: f64 = bind_name e, <error>
  103. // CHECK:STDOUT: return
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: