numeric_literals.carbon 5.1 KB

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