Files
carbon-lang/toolchain/check/testdata/primitives/string_literals.carbon
T
Chandler Carruth d0badecd2a Switch tests to use the rendered form of diagnostics (#8070)
Historically, we disabled the rendered form in tests outside of
`toolchain/diagnostics`. The idea was to keep tests shorter and avoid
very long chains of autoupdate before stabilizing due to all the line
numbers and other artifacts in the rendered form.

This PR adds `--diagnostic-line-numbers=[all|first]` and defaults to
`first` in tests. This keeps the rendered form so that we can easily
code review that form and the visual cues it does or doesn't provide.
But it removes all but the first location's line numbers to provide
significantly more stability in tests.

By default, tests get the full unicode line-drawing form of diagnostics
so we can ensure all of the artifacts there make sense with the specific
case being diagnosed, but we do retain some focused ASCII-rendering
tests to ensure that fallback remains usable.

There are some issues here that will be fixed in subsequent PRs.
Notably, we try to keep the frame together in the check lines, and so
force some parts to use the first location. This largely works, but
fails in some edge cases, causing the entire frame to be located
incorrectly. There is a TODO about this that we need to fix, and it only
occurs 6 times across the test suite, so it didn't seem worth
front-loading a fix for here.

Assisted-by: Claude Code
2026-11-05 07:19:59 +00:00

186 lines
7.2 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/uint.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/primitives/string_literals.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/primitives/string_literals.carbon
// --- fail_no_string.carbon
library "[[@TEST_NAME]]";
fn F() {
// CHECK:STDERR: ╭─┤ fail_no_string.carbon:[[@LINE+7]]:3
// CHECK:STDERR: -> │ "hello";
// CHECK:STDERR: · ━━━┳━━━
// CHECK:STDERR: · ┗━━┫ error: name `Core.String` implicitly referenced here, but not found
// CHECK:STDERR: · ┃ [CoreNameNotFound]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
"hello";
}
// --- unusual_string.carbon
package Core library "[[@TEST_NAME]]";
import library "prelude/parts/uint";
class String {
var begin: u8*;
var end: u8*;
}
// --- fail_use_unusual_string.carbon
import Core library "unusual_string";
// CHECK:STDERR: ╭─┤ fail_use_unusual_string.carbon:[[@LINE+7]]:14
// CHECK:STDERR: -> │ let x: str = "hello";
// CHECK:STDERR: · ━━━┳━━━
// CHECK:STDERR: · ┗━━┫ error: unexpected representation for type `str`
// CHECK:STDERR: · ┃ [StringLiteralTypeUnexpected]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
let x: str = "hello";
// --- incomplete_string.carbon
package Core library "[[@TEST_NAME]]";
class String;
// --- fail_use_incomplete_string.carbon
library "[[@TEST_NAME]]";
import Core library "incomplete_string";
fn F() {
// CHECK:STDERR: ╭─┤ fail_use_incomplete_string.carbon:[[@LINE+11]]:3
// CHECK:STDERR: -> │ "hello";
// CHECK:STDERR: · ━━━┳━━━
// CHECK:STDERR: · ┗━━┫ error: type `Core.String` is incomplete [StringLiteralTypeIncomplete]
// CHECK:STDERR: ┆
// CHECK:STDERR: ├─┤ incomplete_string.carbon
// CHECK:STDERR: │ class String;
// CHECK:STDERR: · ──────┬──────
// CHECK:STDERR: · ╰──┤ class was forward declared here [ClassForwardDeclaredHere]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
"hello";
}
// --- small_string.carbon
package Core library "[[@TEST_NAME]]";
import library "prelude/parts/uint";
class String {
var ptr: u8*;
// An intentionally small `size` field to test the behavior when a string
// literal is large enough that its size can't be represented.
var size: u8;
}
// --- use_small_string.carbon
library "[[@TEST_NAME]]";
import Core library "small_string";
//@dump-sem-ir-begin
let x: str = "hello";
//@dump-sem-ir-end
// --- fail_overfill_small_string.carbon
library "[[@TEST_NAME]]";
import Core library "small_string";
//@dump-sem-ir-begin
// CHECK:STDERR: ╭─┤ fail_overfill_small_string.carbon:[[@LINE+6]]:14
// CHECK:STDERR: -> │ let x: str = '''
// CHECK:STDERR: · ━┳━
// CHECK:STDERR: · ┗━━┫ error: string literal is too long [StringLiteralTooLong]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
let x: str = '''
A string so long its size does not fit in `u8`. This results in an error,
because we can't form a `Core.String` value that describes this string.
This string contains 256 characters. Among them are:
* 6 `g`s,
* 21 `s`s,
* 9 newlines,
* and only one `p`.
''';
//@dump-sem-ir-end
// CHECK:STDOUT: --- use_small_string.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %str.3d4: type = class_type @String [concrete]
// CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete]
// CHECK:STDOUT: %u8: type = class_type @UInt, @UInt(%int_8) [concrete]
// CHECK:STDOUT: %ptr.22f: type = ptr_type %u8 [concrete]
// CHECK:STDOUT: %pattern_type.adf: type = pattern_type %str.3d4 [concrete]
// CHECK:STDOUT: %x.patt: %pattern_type.adf = value_binding_pattern x [concrete]
// CHECK:STDOUT: %str.5d7: %ptr.22f = string_literal "hello" [concrete]
// CHECK:STDOUT: %int_5: %u8 = int_value 5 [concrete]
// CHECK:STDOUT: %String.val: %str.3d4 = struct_value (%str.5d7, %int_5) [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %str: type = type_literal constants.%str.3d4 [concrete = constants.%str.3d4]
// CHECK:STDOUT: %x: %str.3d4 = wrapper_binding x, @__global_init.%String.val
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %x.patt: %pattern_type.adf = value_binding_pattern x [concrete = constants.%x.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %str: %ptr.22f = string_literal "hello" [concrete = constants.%str.5d7]
// CHECK:STDOUT: %int_5: %u8 = int_value 5 [concrete = constants.%int_5]
// CHECK:STDOUT: %String.val: %str.3d4 = struct_value (%str, %int_5) [concrete = constants.%String.val]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_overfill_small_string.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %str.3d4: type = class_type @String [concrete]
// CHECK:STDOUT: %int_8: Core.IntLiteral = int_value 8 [concrete]
// CHECK:STDOUT: %u8: type = class_type @UInt, @UInt(%int_8) [concrete]
// CHECK:STDOUT: %ptr.22f: type = ptr_type %u8 [concrete]
// CHECK:STDOUT: %pattern_type.adf: type = pattern_type %str.3d4 [concrete]
// CHECK:STDOUT: %x.patt: %pattern_type.adf = value_binding_pattern x [concrete]
// CHECK:STDOUT: %str.18e: %ptr.22f = 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]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %str: type = type_literal constants.%str.3d4 [concrete = constants.%str.3d4]
// CHECK:STDOUT: %x: %str.3d4 = wrapper_binding x, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %x.patt: %pattern_type.adf = value_binding_pattern x [concrete = constants.%x.patt]
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %str: %ptr.22f = 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.18e]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT: