mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 20:48:12 +00:00
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
178 lines
5.2 KiB
Plaintext
178 lines
5.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/int.carbon
|
|
// EXTRA-ARGS: --share-cpp-ast
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/basics/share_ast.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/basics/share_ast.carbon
|
|
|
|
// --- a.h
|
|
#ifndef A_H
|
|
#define A_H
|
|
inline int GetA() { return 42; }
|
|
#endif
|
|
|
|
// --- b.h
|
|
#ifndef B_H
|
|
#define B_H
|
|
#include "a.h"
|
|
inline int GetB() { return 100; }
|
|
#endif
|
|
|
|
// --- a.carbon
|
|
library "[[@TEST_NAME]]";
|
|
import Cpp library "a.h";
|
|
|
|
fn CallA() -> i32 {
|
|
return Cpp.GetA();
|
|
}
|
|
|
|
// --- fail_a_does_not_leak.carbon
|
|
library "[[@TEST_NAME]]";
|
|
import Cpp;
|
|
|
|
fn CallLeakedA() -> i32 {
|
|
// This should not be found as we have not imported "a.h".
|
|
// CHECK:STDERR: ╭─┤ fail_a_does_not_leak.carbon:[[@LINE+7]]:10
|
|
// CHECK:STDERR: -> │ return Cpp.GetA();
|
|
// CHECK:STDERR: · ━━━━┳━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: member name `GetA` not found in `Cpp`
|
|
// CHECK:STDERR: · ┃ [MemberNameNotFoundInInstScope]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
return Cpp.GetA();
|
|
}
|
|
|
|
// --- a_again.carbon
|
|
library "[[@TEST_NAME]]";
|
|
import Cpp library "a.h";
|
|
|
|
fn CallAAgain() -> i32 {
|
|
// This should be accepted as we have imported "a.h", even though we will just reuse the version from `a.carbon`.
|
|
return Cpp.GetA();
|
|
}
|
|
|
|
// --- b.carbon
|
|
library "[[@TEST_NAME]]";
|
|
import Cpp library "b.h";
|
|
|
|
fn CallAIndirect() -> i32 {
|
|
return Cpp.GetA();
|
|
}
|
|
|
|
fn CallB() -> i32 {
|
|
return Cpp.GetB();
|
|
}
|
|
|
|
// --- pkg_a.carbon
|
|
package PkgA;
|
|
import Cpp inline '''
|
|
template <typename T> void Accept(T) {}
|
|
''';
|
|
|
|
class SameName {}
|
|
alias TypeAlias = i32;
|
|
namespace N;
|
|
class N.SameNested {}
|
|
|
|
inline Cpp '''
|
|
void CheckPkgA(Carbon::SameName, Carbon::N::SameNested) {
|
|
Carbon::TypeAlias x = 0;
|
|
static_assert(sizeof(x) == 4);
|
|
}
|
|
''';
|
|
|
|
fn TestPkgA(s: SameName, n: N.SameNested) {
|
|
Cpp.Accept(s);
|
|
Cpp.CheckPkgA(s, n);
|
|
}
|
|
|
|
// --- pkg_b.carbon
|
|
package PkgB;
|
|
import Cpp inline '''
|
|
template <typename T> void Accept(T) {}
|
|
''';
|
|
|
|
class SameName {}
|
|
alias TypeAlias = i64;
|
|
namespace N;
|
|
class N.SameNested {}
|
|
|
|
inline Cpp '''
|
|
void CheckPkgB(Carbon::SameName, Carbon::N::SameNested) {
|
|
Carbon::TypeAlias x = 0;
|
|
static_assert(sizeof(x) == 8);
|
|
}
|
|
''';
|
|
|
|
fn TestPkgB(s: SameName, n: N.SameNested) {
|
|
Cpp.Accept(s);
|
|
Cpp.CheckPkgB(s, n);
|
|
}
|
|
|
|
// --- use_both_packages.carbon
|
|
library "[[@TEST_NAME]]";
|
|
import PkgA;
|
|
import PkgB;
|
|
import Cpp inline '''
|
|
// SameName and N::SameNested in PkgA and PkgB have distinct C++ DeclContexts
|
|
// (Carbon::PkgA vs Carbon::PkgB), so overloading on them in C++ succeeds.
|
|
void OverloadByPackage(Carbon::PkgA::SameName*) {}
|
|
void OverloadByPackage(Carbon::PkgB::SameName*) {}
|
|
void OverloadNestedByPackage(Carbon::PkgA::N::SameNested*) {}
|
|
void OverloadNestedByPackage(Carbon::PkgB::N::SameNested*) {}
|
|
''';
|
|
|
|
// --- export_in_c.carbon
|
|
library "[[@TEST_NAME]]";
|
|
import Cpp;
|
|
|
|
class ExportedClass {}
|
|
namespace N;
|
|
class N.ExportedNested {}
|
|
|
|
inline Cpp '''
|
|
using UseExported = Carbon::ExportedClass;
|
|
using UseNested = Carbon::N::ExportedNested;
|
|
''';
|
|
|
|
// --- fail_carbon_export_does_not_leak.carbon
|
|
library "[[@TEST_NAME]]";
|
|
import Cpp;
|
|
|
|
namespace N;
|
|
|
|
inline Cpp '''
|
|
// CHECK:STDERR: ╭─┤ fail_carbon_export_does_not_leak.carbon:[[@LINE+8]]:23
|
|
// CHECK:STDERR: -> │ using Leak1 = Carbon::ExportedClass;
|
|
// CHECK:STDERR: · ───────╴╻
|
|
// CHECK:STDERR: · ┗━━┫ error: no type named 'ExportedClass' in namespace
|
|
// CHECK:STDERR: · ┃ 'Carbon'
|
|
// CHECK:STDERR: · ┃ [CppInteropParseError]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
using Leak1 = Carbon::ExportedClass;
|
|
// CHECK:STDERR: ╭─┤ fail_carbon_export_does_not_leak.carbon:[[@LINE+8]]:26
|
|
// CHECK:STDERR: -> │ using Leak2 = Carbon::N::ExportedNested;
|
|
// CHECK:STDERR: · ──────────╴╻
|
|
// CHECK:STDERR: · ┗━━┫ error: no type named 'ExportedNested' in namespace
|
|
// CHECK:STDERR: · ┃ 'Carbon::N'
|
|
// CHECK:STDERR: · ┃ [CppInteropParseError]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
using Leak2 = Carbon::N::ExportedNested;
|
|
// CHECK:STDERR: ╭─┤ fail_carbon_export_does_not_leak.carbon:[[@LINE+7]]:23
|
|
// CHECK:STDERR: -> │ using Leak3 = Carbon::PkgA::SameName;
|
|
// CHECK:STDERR: · ───────╴╻
|
|
// CHECK:STDERR: · ┗━━┫ error: no member named 'PkgA' in namespace 'Carbon'
|
|
// CHECK:STDERR: · ┃ [CppInteropParseError]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
using Leak3 = Carbon::PkgA::SameName;
|
|
''';
|