string_literals.carbon 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/uint.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/primitives/string_literals.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/primitives/string_literals.carbon
  12. // --- fail_no_string.carbon
  13. library "[[@TEST_NAME]]";
  14. fn F() {
  15. // CHECK:STDERR: fail_no_string.carbon:[[@LINE+4]]:3: error: name `Core.String` implicitly referenced here, but not found [CoreNameNotFound]
  16. // CHECK:STDERR: "hello";
  17. // CHECK:STDERR: ^~~~~~~
  18. // CHECK:STDERR:
  19. "hello";
  20. }
  21. // --- unusual_string.carbon
  22. package Core library "[[@TEST_NAME]]";
  23. import library "prelude/parts/uint";
  24. class String {
  25. var begin: u8*;
  26. var end: u8*;
  27. }
  28. // --- fail_use_unusual_string.carbon
  29. import Core library "unusual_string";
  30. // CHECK:STDERR: fail_use_unusual_string.carbon:[[@LINE+4]]:14: error: unexpected representation for type `str` [StringLiteralTypeUnexpected]
  31. // CHECK:STDERR: let x: str = "hello";
  32. // CHECK:STDERR: ^~~~~~~
  33. // CHECK:STDERR:
  34. let x: str = "hello";
  35. // --- incomplete_string.carbon
  36. package Core library "[[@TEST_NAME]]";
  37. class String;
  38. // --- fail_use_incomplete_string.carbon
  39. library "[[@TEST_NAME]]";
  40. import Core library "incomplete_string";
  41. fn F() {
  42. // CHECK:STDERR: fail_use_incomplete_string.carbon:[[@LINE+7]]:3: error: type `Core.String` is incomplete [StringLiteralTypeIncomplete]
  43. // CHECK:STDERR: "hello";
  44. // CHECK:STDERR: ^~~~~~~
  45. // CHECK:STDERR: incomplete_string.carbon:4:1: note: class was forward declared here [ClassForwardDeclaredHere]
  46. // CHECK:STDERR: class String;
  47. // CHECK:STDERR: ^~~~~~~~~~~~~
  48. // CHECK:STDERR:
  49. "hello";
  50. }
  51. // --- small_string.carbon
  52. package Core library "[[@TEST_NAME]]";
  53. import library "prelude/parts/uint";
  54. class String {
  55. var ptr: u8*;
  56. // An intentionally small `size` field to test the behavior when a string
  57. // literal is large enough that its size can't be represented.
  58. var size: u8;
  59. }
  60. // --- use_small_string.carbon
  61. library "[[@TEST_NAME]]";
  62. import Core library "small_string";
  63. //@dump-sem-ir-begin
  64. let x: str = "hello";
  65. //@dump-sem-ir-end
  66. // --- fail_overfill_small_string.carbon
  67. library "[[@TEST_NAME]]";
  68. import Core library "small_string";
  69. //@dump-sem-ir-begin
  70. // CHECK:STDERR: fail_overfill_small_string.carbon:[[@LINE+4]]:14: error: string literal is too long [StringLiteralTooLong]
  71. // CHECK:STDERR: let x: str = '''
  72. // CHECK:STDERR: ^~~
  73. // CHECK:STDERR:
  74. let x: str = '''
  75. A string so long its size does not fit in `u8`. This results in an error,
  76. because we can't form a `Core.String` value that describes this string.
  77. This string contains 256 characters. Among them are:
  78. * 6 `g`s,
  79. * 21 `s`s,
  80. * 9 newlines,
  81. * and only one `p`.
  82. ''';
  83. //@dump-sem-ir-end
  84. // CHECK:STDOUT: --- use_small_string.carbon
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: constants {
  87. // CHECK:STDOUT: %str.ee0: type = class_type @String [concrete]
  88. // CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete]
  89. // CHECK:STDOUT: %u8: type = class_type @UInt, @UInt(%int_8) [concrete]
  90. // CHECK:STDOUT: %ptr.3e8: type = ptr_type %u8 [concrete]
  91. // CHECK:STDOUT: %pattern_type.461: type = pattern_type %str.ee0 [concrete]
  92. // CHECK:STDOUT: %str.3b1: %ptr.3e8 = string_literal "hello" [concrete]
  93. // CHECK:STDOUT: %int_5: %u8 = int_value 5 [concrete]
  94. // CHECK:STDOUT: %String.val: %str.ee0 = struct_value (%str.3b1, %int_5) [concrete]
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: imports {
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: file {
  101. // CHECK:STDOUT: name_binding_decl {
  102. // CHECK:STDOUT: %x.patt: %pattern_type.461 = value_binding_pattern x [concrete]
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %x: %str.ee0 = value_binding x, @__global_init.%String.val
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: fn @__global_init() {
  108. // CHECK:STDOUT: !entry:
  109. // CHECK:STDOUT: %str: %ptr.3e8 = string_literal "hello" [concrete = constants.%str.3b1]
  110. // CHECK:STDOUT: %int_5: %u8 = int_value 5 [concrete = constants.%int_5]
  111. // CHECK:STDOUT: %String.val: %str.ee0 = struct_value (%str, %int_5) [concrete = constants.%String.val]
  112. // CHECK:STDOUT: <elided>
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: --- fail_overfill_small_string.carbon
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: constants {
  118. // CHECK:STDOUT: %str.ee0: type = class_type @String [concrete]
  119. // CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete]
  120. // CHECK:STDOUT: %u8: type = class_type @UInt, @UInt(%int_8) [concrete]
  121. // CHECK:STDOUT: %ptr.3e8: type = ptr_type %u8 [concrete]
  122. // CHECK:STDOUT: %pattern_type.461: type = pattern_type %str.ee0 [concrete]
  123. // CHECK:STDOUT: %str.294: %ptr.3e8 = string_literal "A string so long its size does not fit in `u8`. This results in an error,\nbecause we can't form a `Core.String` value that describes this string.\n\nThis string contains 256 characters. Among them are:\n\n* 6 `g`s,\n* 21 `s`s,\n* 9 newlines,\n* and only one `p`.\n" [concrete]
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: imports {
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: file {
  130. // CHECK:STDOUT: name_binding_decl {
  131. // CHECK:STDOUT: %x.patt: %pattern_type.461 = value_binding_pattern x [concrete]
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %x: %str.ee0 = value_binding x, <error> [concrete = <error>]
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: fn @__global_init() {
  137. // CHECK:STDOUT: !entry:
  138. // CHECK:STDOUT: %str: %ptr.3e8 = string_literal "A string so long its size does not fit in `u8`. This results in an error,\nbecause we can't form a `Core.String` value that describes this string.\n\nThis string contains 256 characters. Among them are:\n\n* 6 `g`s,\n* 21 `s`s,\n* 9 newlines,\n* and only one `p`.\n" [concrete = constants.%str.294]
  139. // CHECK:STDOUT: <elided>
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: