Files
carbon-lang/toolchain/check/testdata/impl/require_satisified_in_interface.carbon
T
Chandler Carruth 0072b28129 Start every diagnostic with a single message attached to the code (#8102)
This is the final step in landing the _structural_ updates to diagnostic
rendering.

The key idea here is that we want the primary diagnostic to have a
message clearly explaining the problem and attached to the code that has
that problem. This should be the starting point for readers working to
address the issue, but is rendered attached to the code snippet in
question so errors are always "in context" of the code with the error.
Everything else in a diagnostic is an additional label attached to code
to explain or help the reader understand the problem in the primary
message.

Previously, `ContextScope` resulted in some confusion here, as
diagnostic consumers turned messages into labels and made the message in
the context scope the message. In essence, this isn't a scope with
context _for_ a diagnostic (with two exceptions that should probably
have been an `AnnotationScope`), but a context within `check` where we
have a preferred message for a diagnostic emitted by general purpose
code we call into. For example, "class was forward declared here" starts
as an error, and then consumers render it as a label to make the message
from the context scope the error. We also grew an `AnnotationScope` to
handle the contextual information we want to show for a diagnostic (what
I would have assumed `ContextScope` actually did).

Now we have a `Diagnoser`: code in `check` that can better explain the
problem than shared logic it is calling into passes a callback that
builds a caller-specific diagnostic. The callee then can attach relevant
labels and emit it. Diagnostics no longer store `contexts` and consumers
no longer need the logic to restructure things. We move all (but two) of
the old contexts to be normal messages emitted in this way, and we move
some diagnostics to become labels. Each of the new normal messages
corresponds to a new diagnostic kind as usual. Common code following
this pattern now _requires_ the `Diagnoser` to be passed (or emit
nothing), and this uncovered three places where an empty context was
used that made the rendered form miss key information. All three are
fixed with new diagnostics, one already covered by tests, and two with
TODOs to add test coverage.

We also completely remove the old `ContextScope` and
`CARBON_DIAGNOSTIC_CONTEXT` and rename `AnnotationScope` to
`ContextScope`. It is only used to attach information about when/where a
diagnostic occurred.

Labels also no longer have categories. What were `Primary` are now
`ExtraRange`s on the diagnostic rather than labels. Labels are now
always informational.

`DeclaredName` now narrows the range of a label when the range is a
declaration of a named entity to only be the range of that name. For
entities that don't have a declared name, the entire declaration remains
the range. The code building and emitting these diagnostics can skip
them if the declared location isn't distinct from the diagnostic
location.

Wording is largely unchanged here so we can just review the _structural_
change. #7991 iterates using this to improve locations and adds missing
labels. A subsequent PR will iterate on the wording to increasingly fit
within this structural model.

This is separated out as a way to review incrementally, and so that
subsequently we don't have to review wording improvements that only make
sense in a new structure that you cannot see yet. It seems easier to
look at the structure here even with awkward wording.

Assisted-by: Claude Code
2026-11-22 05:19:35 +00:00

358 lines
11 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
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/require_satisified_in_interface.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/require_satisified_in_interface.carbon
// --- fail_missing_extend_require_for_concrete_type.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface Y {
extend require impls Z;
}
// () does not need to impl Z yet.
impl () as Y;
// () needs to impl Z at the start of the definition.
// CHECK:STDERR: ╭─┤ fail_missing_extend_require_for_concrete_type.carbon:[[@LINE+7]]:1
// CHECK:STDERR: -> │ impl () as Y {}
// CHECK:STDERR: · ━━━┳━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: interface `Y` being implemented requires that `()` implements `Z`
// CHECK:STDERR: · ┃ [InterfaceRequireImplsNotImplemented]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
impl () as Y {}
// --- fail_missing_require_for_concrete_type.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface Y {
require impls Z;
}
// () does not need to impl Z yet.
impl () as Y;
// () needs to impl Z at the start of the definition.
// CHECK:STDERR: ╭─┤ fail_missing_require_for_concrete_type.carbon:[[@LINE+7]]:1
// CHECK:STDERR: -> │ impl () as Y {}
// CHECK:STDERR: · ━━━┳━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: interface `Y` being implemented requires that `()` implements `Z`
// CHECK:STDERR: · ┃ [InterfaceRequireImplsNotImplemented]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
impl () as Y {}
// --- fail_missing_extend_require_for_symbolic_type.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface Y {
extend require impls Z;
}
// T does not need to impl Z yet.
impl forall [T: type] T as Y;
// T needs to impl Z at the start of the definition.
// CHECK:STDERR: ╭─┤ fail_missing_extend_require_for_symbolic_type.carbon:[[@LINE+7]]:1
// CHECK:STDERR: -> │ impl forall [T: type] T as Y {}
// CHECK:STDERR: · ━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: interface `Y` being implemented requires that `T` implements `Z`
// CHECK:STDERR: · ┃ [InterfaceRequireImplsNotImplemented]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
impl forall [T: type] T as Y {}
// --- fail_missing_require_for_symbolic_type.carbon
library "[[@TEST_NAME]]";
interface Z {}
interface Y {
require impls Z;
}
// T does not need to impl Z yet.
impl forall [T: type] T as Y;
// T needs to impl Z at the start of the definition.
// CHECK:STDERR: ╭─┤ fail_missing_require_for_symbolic_type.carbon:[[@LINE+7]]:1
// CHECK:STDERR: -> │ impl forall [T: type] T as Y {}
// CHECK:STDERR: · ━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: interface `Y` being implemented requires that `T` implements `Z`
// CHECK:STDERR: · ┃ [InterfaceRequireImplsNotImplemented]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
impl forall [T: type] T as Y {}
// --- fail_missing_extend_require_double.carbon
library "[[@TEST_NAME]]";
interface Z1(T: type) {}
interface Z2(T: type) {}
class A;
class B;
interface Y(U: type) {
extend require impls Z1(U) & Z2(B);
}
// CHECK:STDERR: ╭─┤ fail_missing_extend_require_double.carbon:[[@LINE+8]]:1
// CHECK:STDERR: -> │ impl () as Y(A) {}
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: interface `Y(A)` being implemented requires that `()` implements
// CHECK:STDERR: · ┃ `Z1(A) & Z2(B)`
// CHECK:STDERR: · ┃ [InterfaceRequireImplsNotImplemented]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
impl () as Y(A) {}
// --- fail_missing_require_double.carbon
library "[[@TEST_NAME]]";
interface Z1(T: type) {}
interface Z2(T: type) {}
class A;
class B;
interface Y(U: type) {
require impls Z1(U) & Z2(B);
}
// CHECK:STDERR: ╭─┤ fail_missing_require_double.carbon:[[@LINE+8]]:1
// CHECK:STDERR: -> │ impl () as Y(A) {}
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: interface `Y(A)` being implemented requires that `()` implements
// CHECK:STDERR: · ┃ `Z1(A) & Z2(B)`
// CHECK:STDERR: · ┃ [InterfaceRequireImplsNotImplemented]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
impl () as Y(A) {}
// --- require_for_concrete_type.carbon
library "[[@TEST_NAME]]";
interface Z1 {}
interface Z2 {}
interface Y {
extend require impls Z1;
require impls Z2;
}
// () does not need to impl Z1/Z2 yet.
impl () as Y;
// Now we know () impls Z1 and Z2.
impl () as Z1;
impl () as Z2;
// So no error here.
impl () as Y {}
impl () as Z1 {}
impl () as Z2 {}
// --- require_for_symbolic_type.carbon
library "[[@TEST_NAME]]";
interface Z1 {}
interface Z2 {}
interface Y {
extend require impls Z1;
require impls Z2;
}
// T does not need to impl Z1/Z2 yet.
impl forall [T: type] T as Y;
// Now we know T impls Z1 and Z2.
impl forall [T: type] T as Z1;
impl forall [T: type] T as Z2;
// So no error here.
impl forall [T: type] T as Y {}
impl forall [T: type] T as Z1 {}
impl forall [T: type] T as Z2 {}
// --- require_for_symbolic_type_impl_matches_same_interface.carbon
library "[[@TEST_NAME]]";
interface Z1 {}
interface Z2 {}
interface Y {
extend require impls Z1;
require impls Z2;
}
// This only matches T that impl Z1 and Z2 so no error here.
impl forall [T: Z1 & Z2] T as Y {}
// --- require_for_generic_interfaces.carbon
library "[[@TEST_NAME]]";
interface Z1(T: type) {}
interface Z2(T: type) {}
class A;
class B;
interface Y(U: type) {
extend require impls Z1(U);
require impls Z2(B);
}
// () does not need to impl Z1(A) and Z2(B) yet.
impl () as Y(A);
// Now we know () impls Z1(A) and Z2(B).
impl () as Z1(A);
impl () as Z2(B);
// So no error here.
impl () as Y(A) {}
impl () as Z1(A) {}
impl () as Z2(B) {}
// --- fail_require_non_self_missing.carbon
library "[[@TEST_NAME]]";
interface Z(T: type) {}
class C;
interface Y(U: type) {
// `Self` must appear somewhere in the require decl, so it's in the interface
// specific because we want to test a different self-type.
require C impls Z(Self);
}
// Y requires that C impls Z(Self), but it does not do so.
// CHECK:STDERR: ╭─┤ fail_require_non_self_missing.carbon:[[@LINE+8]]:1
// CHECK:STDERR: -> │ impl () as Y(()) {}
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: interface `Y(())` being implemented requires that `C` implements
// CHECK:STDERR: · ┃ `Z(())`
// CHECK:STDERR: · ┃ [InterfaceRequireImplsNotImplemented]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
impl () as Y(()) {}
// --- require_non_self.carbon
library "[[@TEST_NAME]]";
interface Z(T: type) {}
class C;
interface Y(U: type) {
// `Self` must appear somewhere in the require decl, so it's in the interface
// specific because we want to test a different self-type.
require C impls Z(Self);
}
impl C as Z(());
// Y requires that C impls Z(Self), which is done above.
impl () as Y(Self) {}
impl C as Z(()) {}
// --- impl_requires_itself_cycle.carbon
library "[[@TEST_NAME]]";
interface A { fn AA(); }
interface B {
require impls A;
}
interface C {
require impls B;
}
impl forall [T: B] T as A { fn AA() {} }
// When we get to the definition we check that anything B requires is satisfied.
// - The interface B requires A, so we must check T impls A.
// - The impl for A requires T impls B.
// - This impl is what provides T impls B.
impl forall [T: C] T as B {}
fn F(generic T: C) {
// If things go wrong, we find the impl `T as B`, but inside its definition is
// a lookup for `T as A`, which (through deducing `T`) includes a lookup for
// `T as B`. This creates a cycle of evaluating `T as B` recursively.
//
// This was solved by doing the check for requirements of `B` outside the
// definition of `T as B`.
// https://discord.com/channels/655572317891461132/941071822756143115/1463189087598022861
T.(A.AA)();
}
// --- fail_impl_requires_itself_cycle_with_monomorphization_error.carbon
library "[[@TEST_NAME]]";
// Unlike `array(T, N)`, this fails eagerly on an invalid bound.
fn Array(T: type, N: Core.IntLiteral) -> type = "array.make_type";
class W(T: type) {
adapt {};
}
interface A(N: Core.IntLiteral) {
// CHECK:STDERR: ╭─┤ fail_impl_requires_itself_cycle_with_monomorphization_error.carbon:[[@LINE+11]]:26
// CHECK:STDERR: -> │ fn AA() -> W(Array((), N));
// CHECK:STDERR: · ┳
// CHECK:STDERR: · ┗━━┫ error: array bound of -1 is negative
// CHECK:STDERR: · ┃ [ArrayBoundNegative]
// CHECK:STDERR: ┆
// CHECK:STDERR: │ T.(A(-1).AA)();
// CHECK:STDERR: · ───────┬──────
// CHECK:STDERR: · ╰──┤ in monomorphization of `AA()` here [ResolvingSpecificHere]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
fn AA() -> W(Array((), N));
}
interface B(N: Core.IntLiteral) {
require impls A(N);
}
interface C(N: Core.IntLiteral) {
require impls B(N);
}
impl forall [N: Core.IntLiteral, T: B(N)] T as A(N) {
fn AA() -> W(Array((), N)) { return {} as W(Array((), N)); }
}
impl forall [N: Core.IntLiteral, T: C(N)] T as B(N) {
// The definition here does not contain the lookups verifying that C(N) impls
// `A(N)`, so they do not get re-evaluated for a specific `N`. That doesn't
// prevent us from producing a reasonable diagnostic when that `N` causes an
// error in the specific use of this impl, which we use to get from `C(N)` to
// `A(N)` (in the deduction of `T` in the impl as `A(N)`). The error just
// happens where we use `N` in `A(N)` instead of inside the verification that
// `T as B(N)` implies `T as A(N)`.
}
fn F(generic T: C(-1)) {
T.(A(-1).AA)();
}