fail_numeric_literal_overflow.carbon 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: %.1: Core.IntLiteral = int_value 32 [template]
  38. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  39. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  40. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  41. // CHECK:STDOUT: %.2: Core.IntLiteral = int_value 39999999999999999993 [template]
  42. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  43. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  44. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  45. // CHECK:STDOUT: %.26: <witness> = interface_witness (%Convert.14) [template]
  46. // CHECK:STDOUT: %.27: <bound method> = bound_method %.2, %Convert.14 [template]
  47. // CHECK:STDOUT: %.28: <specific function> = specific_function %.27, @Convert.2(%.1) [template]
  48. // CHECK:STDOUT: %.29: %i32 = int_value 39999999999999999993 [template]
  49. // CHECK:STDOUT: %.30: Core.IntLiteral = int_value 2147483648 [template]
  50. // CHECK:STDOUT: %.31: <bound method> = bound_method %.30, %Convert.14 [template]
  51. // CHECK:STDOUT: %.32: <specific function> = specific_function %.31, @Convert.2(%.1) [template]
  52. // CHECK:STDOUT: %.33: %i32 = int_value 2147483648 [template]
  53. // CHECK:STDOUT: %.34: Core.IntLiteral = int_value 64 [template]
  54. // CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
  55. // CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: imports {
  59. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  60. // CHECK:STDOUT: .Int = %import_ref.1
  61. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  62. // CHECK:STDOUT: .Float = %import_ref.38
  63. // CHECK:STDOUT: import Core//prelude
  64. // CHECK:STDOUT: import Core//prelude/...
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: file {
  69. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  70. // CHECK:STDOUT: .Core = imports.%Core
  71. // CHECK:STDOUT: .a = @__global_init.%a
  72. // CHECK:STDOUT: .b = @__global_init.%b
  73. // CHECK:STDOUT: .c = @__global_init.%c
  74. // CHECK:STDOUT: .d = @__global_init.%d
  75. // CHECK:STDOUT: .e = @__global_init.%e
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %Core.import = import Core
  78. // CHECK:STDOUT: %.loc15_8.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  79. // CHECK:STDOUT: %int.make_type_signed.loc15: init type = call constants.%Int(%.loc15_8.1) [template = constants.%i32]
  80. // CHECK:STDOUT: %.loc15_8.2: type = value_of_initializer %int.make_type_signed.loc15 [template = constants.%i32]
  81. // CHECK:STDOUT: %.loc15_8.3: type = converted %int.make_type_signed.loc15, %.loc15_8.2 [template = constants.%i32]
  82. // CHECK:STDOUT: %.loc21_8.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  83. // CHECK:STDOUT: %int.make_type_signed.loc21: init type = call constants.%Int(%.loc21_8.1) [template = constants.%i32]
  84. // CHECK:STDOUT: %.loc21_8.2: type = value_of_initializer %int.make_type_signed.loc21 [template = constants.%i32]
  85. // CHECK:STDOUT: %.loc21_8.3: type = converted %int.make_type_signed.loc21, %.loc21_8.2 [template = constants.%i32]
  86. // CHECK:STDOUT: %.loc27_8.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  87. // CHECK:STDOUT: %int.make_type_signed.loc27: init type = call constants.%Int(%.loc27_8.1) [template = constants.%i32]
  88. // CHECK:STDOUT: %.loc27_8.2: type = value_of_initializer %int.make_type_signed.loc27 [template = constants.%i32]
  89. // CHECK:STDOUT: %.loc27_8.3: type = converted %int.make_type_signed.loc27, %.loc27_8.2 [template = constants.%i32]
  90. // CHECK:STDOUT: %.loc33_8.1: Core.IntLiteral = int_value 64 [template = constants.%.34]
  91. // CHECK:STDOUT: %float.make_type.loc33: init type = call constants.%Float(%.loc33_8.1) [template = f64]
  92. // CHECK:STDOUT: %.loc33_8.2: type = value_of_initializer %float.make_type.loc33 [template = f64]
  93. // CHECK:STDOUT: %.loc33_8.3: type = converted %float.make_type.loc33, %.loc33_8.2 [template = f64]
  94. // CHECK:STDOUT: %.loc38_8.1: Core.IntLiteral = int_value 64 [template = constants.%.34]
  95. // CHECK:STDOUT: %float.make_type.loc38: init type = call constants.%Float(%.loc38_8.1) [template = f64]
  96. // CHECK:STDOUT: %.loc38_8.2: type = value_of_initializer %float.make_type.loc38 [template = f64]
  97. // CHECK:STDOUT: %.loc38_8.3: type = converted %float.make_type.loc38, %.loc38_8.2 [template = f64]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: fn @__global_init() {
  101. // CHECK:STDOUT: !entry:
  102. // CHECK:STDOUT: %.loc15_14: Core.IntLiteral = int_value 39999999999999999993 [template = constants.%.2]
  103. // CHECK:STDOUT: %.loc15_34.1: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  104. // CHECK:STDOUT: %.loc15_34.2: <bound method> = bound_method %.loc15_14, %.loc15_34.1 [template = constants.%.27]
  105. // CHECK:STDOUT: %.loc15_34.3: <specific function> = specific_function %.loc15_34.2, @Convert.2(constants.%.1) [template = constants.%.28]
  106. // CHECK:STDOUT: %int.convert_checked.loc15: init %i32 = call %.loc15_34.3(%.loc15_14) [template = constants.%.29]
  107. // CHECK:STDOUT: %.loc15_34.4: %i32 = value_of_initializer %int.convert_checked.loc15 [template = constants.%.29]
  108. // CHECK:STDOUT: %.loc15_34.5: %i32 = converted %.loc15_14, %.loc15_34.4 [template = constants.%.29]
  109. // CHECK:STDOUT: %a: %i32 = bind_name a, %.loc15_34.5
  110. // CHECK:STDOUT: %.loc21_14: Core.IntLiteral = int_value 2147483648 [template = constants.%.30]
  111. // CHECK:STDOUT: %.loc21_27.1: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  112. // CHECK:STDOUT: %.loc21_27.2: <bound method> = bound_method %.loc21_14, %.loc21_27.1 [template = constants.%.31]
  113. // CHECK:STDOUT: %.loc21_27.3: <specific function> = specific_function %.loc21_27.2, @Convert.2(constants.%.1) [template = constants.%.32]
  114. // CHECK:STDOUT: %int.convert_checked.loc21: init %i32 = call %.loc21_27.3(%.loc21_14) [template = constants.%.33]
  115. // CHECK:STDOUT: %.loc21_27.4: %i32 = value_of_initializer %int.convert_checked.loc21 [template = constants.%.33]
  116. // CHECK:STDOUT: %.loc21_27.5: %i32 = converted %.loc21_14, %.loc21_27.4 [template = constants.%.33]
  117. // CHECK:STDOUT: %b: %i32 = bind_name b, %.loc21_27.5
  118. // CHECK:STDOUT: %.loc27_14: Core.IntLiteral = int_value 2147483648 [template = constants.%.30]
  119. // CHECK:STDOUT: %.loc27_25.1: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  120. // CHECK:STDOUT: %.loc27_25.2: <bound method> = bound_method %.loc27_14, %.loc27_25.1 [template = constants.%.31]
  121. // CHECK:STDOUT: %.loc27_25.3: <specific function> = specific_function %.loc27_25.2, @Convert.2(constants.%.1) [template = constants.%.32]
  122. // CHECK:STDOUT: %int.convert_checked.loc27: init %i32 = call %.loc27_25.3(%.loc27_14) [template = constants.%.33]
  123. // CHECK:STDOUT: %.loc27_25.4: %i32 = value_of_initializer %int.convert_checked.loc27 [template = constants.%.33]
  124. // CHECK:STDOUT: %.loc27_25.5: %i32 = converted %.loc27_14, %.loc27_25.4 [template = constants.%.33]
  125. // CHECK:STDOUT: %c: %i32 = bind_name c, %.loc27_25.5
  126. // CHECK:STDOUT: %d: f64 = bind_name d, <error>
  127. // CHECK:STDOUT: %e: f64 = bind_name e, <error>
  128. // CHECK:STDOUT: return
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: