numeric_literals.carbon 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/numeric_literals.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/basics/numeric_literals.carbon
  10. // --- literal_values.carbon
  11. library "[[@TEST_NAME]]";
  12. fn F() {
  13. // 8 and 9 trigger special behavior in APInt when mishandling signed versus
  14. // unsigned, so we pay extra attention to those.
  15. var ints: array(i32, 6) = (
  16. //@dump-sem-ir-begin
  17. 8,
  18. 9,
  19. 0x8,
  20. 0b1000,
  21. 2147483647,
  22. 0x7FFFFFFF,
  23. //@dump-sem-ir-end
  24. );
  25. var floats: array(f64, 6) = (
  26. //@dump-sem-ir-begin
  27. 0.9,
  28. 8.0,
  29. 80.0,
  30. 1.0e7,
  31. 1.0e8,
  32. 1.0e-8
  33. //@dump-sem-ir-end
  34. );
  35. }
  36. // --- fail_overflow_very_large.carbon
  37. library "[[@TEST_NAME]]";
  38. // CHECK:STDERR: fail_overflow_very_large.carbon:[[@LINE+4]]:14: error: integer value 39999999999999999993 too large for type `i32` [IntTooLargeForType]
  39. // CHECK:STDERR: let a: i32 = 39999999999999999993;
  40. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  41. // CHECK:STDERR:
  42. let a: i32 = 39999999999999999993;
  43. // --- fail_overflow_boundary.carbon
  44. library "[[@TEST_NAME]]";
  45. // CHECK:STDERR: fail_overflow_boundary.carbon:[[@LINE+4]]:14: error: integer value 2147483648 too large for type `i32` [IntTooLargeForType]
  46. // CHECK:STDERR: let b: i32 = 2_147_483_648;
  47. // CHECK:STDERR: ^~~~~~~~~~~~~
  48. // CHECK:STDERR:
  49. let b: i32 = 2_147_483_648;
  50. // --- fail_overflow_boundary_hex.carbon
  51. library "[[@TEST_NAME]]";
  52. // CHECK:STDERR: fail_overflow_boundary_hex.carbon:[[@LINE+4]]:14: error: integer value 2147483648 too large for type `i32` [IntTooLargeForType]
  53. // CHECK:STDERR: let c: i32 = 0x8000_0000;
  54. // CHECK:STDERR: ^~~~~~~~~~~
  55. // CHECK:STDERR:
  56. let c: i32 = 0x8000_0000;
  57. // --- fail_overflow_very_large_mantissa.carbon
  58. library "[[@TEST_NAME]]";
  59. // CHECK:STDERR: fail_overflow_very_large_mantissa.carbon:[[@LINE+4]]:14: error: real mantissa with value 399999999999999999930 does not fit in i64 [RealMantissaTooLargeForI64]
  60. // CHECK:STDERR: let d: f64 = 39999999999999999993.0e3;
  61. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  62. // CHECK:STDERR:
  63. let d: f64 = 39999999999999999993.0e3;
  64. // --- fail_overflow_very_large_exponent.carbon
  65. library "[[@TEST_NAME]]";
  66. // CHECK:STDERR: fail_overflow_very_large_exponent.carbon:[[@LINE+4]]:14: error: real exponent with value 39999999999999999992 does not fit in i64 [RealExponentTooLargeForI64]
  67. // CHECK:STDERR: let e: f64 = 5.0e39999999999999999993;
  68. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  69. // CHECK:STDERR:
  70. let e: f64 = 5.0e39999999999999999993;
  71. // CHECK:STDOUT: --- literal_values.carbon
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: constants {
  74. // CHECK:STDOUT: %int_8.b85: Core.IntLiteral = int_value 8 [concrete]
  75. // CHECK:STDOUT: %int_9.988: Core.IntLiteral = int_value 9 [concrete]
  76. // CHECK:STDOUT: %int_2147483647.d89: Core.IntLiteral = int_value 2147483647 [concrete]
  77. // CHECK:STDOUT: %float.952: f64 = float_literal 0.90000000000000002 [concrete]
  78. // CHECK:STDOUT: %float.298: f64 = float_literal 8 [concrete]
  79. // CHECK:STDOUT: %float.dcb: f64 = float_literal 80 [concrete]
  80. // CHECK:STDOUT: %float.1d0: f64 = float_literal 1.0E+7 [concrete]
  81. // CHECK:STDOUT: %float.9f6: f64 = float_literal 1.0E+8 [concrete]
  82. // CHECK:STDOUT: %float.401: f64 = float_literal 1.0E-8 [concrete]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: imports {
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: fn @F() {
  89. // CHECK:STDOUT: !entry:
  90. // CHECK:STDOUT: <elided>
  91. // CHECK:STDOUT: %int_8.loc9: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  92. // CHECK:STDOUT: %int_9: Core.IntLiteral = int_value 9 [concrete = constants.%int_9.988]
  93. // CHECK:STDOUT: %int_8.loc11: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  94. // CHECK:STDOUT: %int_8.loc12: Core.IntLiteral = int_value 8 [concrete = constants.%int_8.b85]
  95. // CHECK:STDOUT: %int_2147483647.loc13: Core.IntLiteral = int_value 2147483647 [concrete = constants.%int_2147483647.d89]
  96. // CHECK:STDOUT: %int_2147483647.loc14: Core.IntLiteral = int_value 2147483647 [concrete = constants.%int_2147483647.d89]
  97. // CHECK:STDOUT: <elided>
  98. // CHECK:STDOUT: %float.loc19: f64 = float_literal 0.90000000000000002 [concrete = constants.%float.952]
  99. // CHECK:STDOUT: %float.loc20: f64 = float_literal 8 [concrete = constants.%float.298]
  100. // CHECK:STDOUT: %float.loc21: f64 = float_literal 80 [concrete = constants.%float.dcb]
  101. // CHECK:STDOUT: %float.loc22: f64 = float_literal 1.0E+7 [concrete = constants.%float.1d0]
  102. // CHECK:STDOUT: %float.loc23: f64 = float_literal 1.0E+8 [concrete = constants.%float.9f6]
  103. // CHECK:STDOUT: %float.loc24: f64 = float_literal 1.0E-8 [concrete = constants.%float.401]
  104. // CHECK:STDOUT: <elided>
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: