From 49212feac668a4adfe5e8cdcebfe994a470e28bb Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Thu, 26 Jun 2025 14:59:55 -0700 Subject: [PATCH] Allow repeated includes, particularly for min_prelude (#5741) Make min_prelude parts include what they use, and remove the inclusions which were for indirect uses from the main min_prelude files. --- testing/file_test/BUILD | 1 + testing/file_test/test_file.cpp | 7 +++++++ .../file_test/testdata/include_repeated.carbon | 16 ++++++++++++++++ .../testing/testdata/min_prelude/int.carbon | 6 ------ .../testdata/min_prelude/parts/float.carbon | 2 ++ .../testdata/min_prelude/parts/int.carbon | 3 +++ .../testdata/min_prelude/parts/iterate.carbon | 2 ++ .../testdata/min_prelude/parts/optional.carbon | 2 ++ .../testdata/min_prelude/parts/uint.carbon | 3 +++ .../testdata/min_prelude/primitives.carbon | 6 ------ .../testing/testdata/min_prelude/uint.carbon | 6 ------ 11 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 testing/file_test/testdata/include_repeated.carbon diff --git a/testing/file_test/BUILD b/testing/file_test/BUILD index 49fbc7c2d302..3c7c6a2835cc 100644 --- a/testing/file_test/BUILD +++ b/testing/file_test/BUILD @@ -50,6 +50,7 @@ cc_library( "//common:ostream", "//common:pretty_stack_trace_function", "//common:raw_string_ostream", + "//common:set", "//testing/base:file_helpers", "@abseil-cpp//absl/flags:flag", "@abseil-cpp//absl/flags:parse", diff --git a/testing/file_test/test_file.cpp b/testing/file_test/test_file.cpp index cba68362dcd8..c88242a9a65f 100644 --- a/testing/file_test/test_file.cpp +++ b/testing/file_test/test_file.cpp @@ -13,6 +13,7 @@ #include "common/error.h" #include "common/find.h" #include "common/raw_string_ostream.h" +#include "common/set.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/JSON.h" #include "testing/base/file_helpers.h" @@ -835,8 +836,14 @@ auto ProcessTestFile(llvm::StringRef test_name, bool running_autoupdate) } // Process includes. This can add entries to `include_files`. + Set processed_includes; for (size_t i = 0; i < include_files.size(); ++i) { const auto& filename = include_files[i]; + if (!processed_includes.Insert(filename).is_inserted()) { + // Ignore repeated includes, mainly so that included files can include the + // same file (i.e., repeated indirectly). + continue; + } CARBON_ASSIGN_OR_RETURN(std::string content, ReadFile(filename)); // Note autoupdate never touches included files. CARBON_RETURN_IF_ERROR(ProcessFileContent( diff --git a/testing/file_test/testdata/include_repeated.carbon b/testing/file_test/testdata/include_repeated.carbon new file mode 100644 index 000000000000..21e64ad1a619 --- /dev/null +++ b/testing/file_test/testdata/include_repeated.carbon @@ -0,0 +1,16 @@ +// 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: testing/file_test/testdata/include_files/no_split.carbon +// INCLUDE-FILE: testing/file_test/testdata/include_files/no_split.carbon +// INCLUDE-FILE: testing/file_test/testdata/include_files/no_split.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //testing/file_test:file_test_base_test --test_arg=--file_tests=testing/file_test/testdata/include_repeated.carbon +// TIP: To dump output, run: +// TIP: bazel run //testing/file_test:file_test_base_test -- --dump_output --file_tests=testing/file_test/testdata/include_repeated.carbon + +// CHECK:STDOUT: 3 args: `default_args`, `include_repeated.carbon`, `include_files/no_split.carbon` +// CHECK:STDOUT: include_files/no_split.carbon:5: no split diff --git a/toolchain/testing/testdata/min_prelude/int.carbon b/toolchain/testing/testdata/min_prelude/int.carbon index 2ad073c48b85..f709432bbc91 100644 --- a/toolchain/testing/testdata/min_prelude/int.carbon +++ b/toolchain/testing/testdata/min_prelude/int.carbon @@ -2,12 +2,6 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -// TODO: This should move to the place where it's imported, but it ends up -// included more than once and that fails to build. -// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/as.carbon -// TODO: This should move to the place where it's imported, but it ends up -// included more than once and that fails to build. -// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int_literal.carbon // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int.carbon // EXTRA-ARGS: --custom-core --exclude-dump-file-prefix=min_prelude/ diff --git a/toolchain/testing/testdata/min_prelude/parts/float.carbon b/toolchain/testing/testdata/min_prelude/parts/float.carbon index 189ed59665c1..978f32245798 100644 --- a/toolchain/testing/testdata/min_prelude/parts/float.carbon +++ b/toolchain/testing/testdata/min_prelude/parts/float.carbon @@ -1,6 +1,8 @@ // 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/parts/int_literal.carbon // --- min_prelude/parts/float.carbon diff --git a/toolchain/testing/testdata/min_prelude/parts/int.carbon b/toolchain/testing/testdata/min_prelude/parts/int.carbon index 779d4c580f00..c96c190d6962 100644 --- a/toolchain/testing/testdata/min_prelude/parts/int.carbon +++ b/toolchain/testing/testdata/min_prelude/parts/int.carbon @@ -1,6 +1,9 @@ // 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/parts/as.carbon +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int_literal.carbon // --- min_prelude/parts/int.carbon diff --git a/toolchain/testing/testdata/min_prelude/parts/iterate.carbon b/toolchain/testing/testdata/min_prelude/parts/iterate.carbon index bbd36ce723ae..507787bc117b 100644 --- a/toolchain/testing/testdata/min_prelude/parts/iterate.carbon +++ b/toolchain/testing/testdata/min_prelude/parts/iterate.carbon @@ -1,6 +1,8 @@ // 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/parts/optional.carbon // --- min_prelude/parts/iterate.carbon diff --git a/toolchain/testing/testdata/min_prelude/parts/optional.carbon b/toolchain/testing/testdata/min_prelude/parts/optional.carbon index 07e9d68e23fa..6eb8e5fceeb8 100644 --- a/toolchain/testing/testdata/min_prelude/parts/optional.carbon +++ b/toolchain/testing/testdata/min_prelude/parts/optional.carbon @@ -1,6 +1,8 @@ // 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/parts/bool.carbon // --- min_prelude/parts/optional.carbon diff --git a/toolchain/testing/testdata/min_prelude/parts/uint.carbon b/toolchain/testing/testdata/min_prelude/parts/uint.carbon index ac6d9c1dde26..2c9427fc1622 100644 --- a/toolchain/testing/testdata/min_prelude/parts/uint.carbon +++ b/toolchain/testing/testdata/min_prelude/parts/uint.carbon @@ -1,6 +1,9 @@ // 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/parts/as.carbon +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int_literal.carbon // --- min_prelude/parts/uint.carbon diff --git a/toolchain/testing/testdata/min_prelude/primitives.carbon b/toolchain/testing/testdata/min_prelude/primitives.carbon index a26e0714d536..d6c0e8817a9d 100644 --- a/toolchain/testing/testdata/min_prelude/primitives.carbon +++ b/toolchain/testing/testdata/min_prelude/primitives.carbon @@ -2,14 +2,8 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -// TODO: This should move to the place where it's imported, but it ends up -// included more than once and that fails to build. -// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/as.carbon // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/bool.carbon -// TODO: This should move to the place where it's imported, but it ends up -// included more than once and that fails to build. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/float.carbon -// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int_literal.carbon // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int.carbon // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/uint.carbon // EXTRA-ARGS: --custom-core --exclude-dump-file-prefix=min_prelude/ diff --git a/toolchain/testing/testdata/min_prelude/uint.carbon b/toolchain/testing/testdata/min_prelude/uint.carbon index e5002c3a24bd..2df1dd6f8805 100644 --- a/toolchain/testing/testdata/min_prelude/uint.carbon +++ b/toolchain/testing/testdata/min_prelude/uint.carbon @@ -2,12 +2,6 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -// TODO: This should move to the place where it's imported, but it ends up -// included more than once and that fails to build. -// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/as.carbon -// TODO: This should move to the place where it's imported, but it ends up -// included more than once and that fails to build. -// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int_literal.carbon // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/int.carbon // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/parts/uint.carbon // EXTRA-ARGS: --custom-core --exclude-dump-file-prefix=min_prelude/