mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 19:55:11 +00:00
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
386 lines
17 KiB
Plaintext
386 lines
17 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/convert.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/lookup/impl_cycle.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/impl_cycle.carbon
|
|
|
|
// --- fail_impl_simple_cycle.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {}
|
|
|
|
// This creates a dependency cycle with itself.
|
|
impl forall [T: Z] T as Z {}
|
|
|
|
class Point {}
|
|
|
|
fn F() {
|
|
// TODO: We should just get one diagnostic here, either the conversion fails
|
|
// with a note about the cycle as the reason for impl lookup failing, or the
|
|
// cycle causes an error so the conversion is not diagnosed.
|
|
|
|
// CHECK:STDERR: ╭─┤ fail_impl_simple_cycle.carbon:[[@LINE+18]]:3
|
|
// CHECK:STDERR: │ impl forall [T: Z] T as Z {}
|
|
// CHECK:STDERR: · ─────────────┬─────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches [ImplLookupCycleNote]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Point as Z;
|
|
// CHECK:STDERR: · ━━━━━┳━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cycle found in search for impl of `Z` for type `Point`
|
|
// CHECK:STDERR: · ┃ [ImplLookupCycle]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: ╭─┤ fail_impl_simple_cycle.carbon:[[@LINE+7]]:3
|
|
// CHECK:STDERR: -> │ Point as Z;
|
|
// CHECK:STDERR: · ━━━━━┳━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot convert type `Point` into type implementing `Z`
|
|
// CHECK:STDERR: · ┃ [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
Point as Z;
|
|
}
|
|
|
|
// --- fail_impl_simple_where_cycle.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Y {}
|
|
interface Z {}
|
|
|
|
// This creates a dependency cycle with itself.
|
|
impl forall [T: type where .Self impls Z] T as Z {}
|
|
|
|
class Point {}
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: ╭─┤ fail_impl_simple_where_cycle.carbon:[[@LINE+20]]:3
|
|
// CHECK:STDERR: │ impl forall [T: type where .Self impls Z] T as Z {}
|
|
// CHECK:STDERR: · ─────────────────────────┬────────────────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches
|
|
// CHECK:STDERR: · │ [ImplLookupCycleNote]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Point as Z;
|
|
// CHECK:STDERR: · ┳━━━━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cycle found in search for impl of `type where .Self impls Z` for
|
|
// CHECK:STDERR: · ┃ type `Point`
|
|
// CHECK:STDERR: · ┃ [ImplLookupCycle]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: ╭─┤ fail_impl_simple_where_cycle.carbon:[[@LINE+7]]:3
|
|
// CHECK:STDERR: -> │ Point as Z;
|
|
// CHECK:STDERR: · ━━━━━┳━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot convert type `Point` into type implementing `Z`
|
|
// CHECK:STDERR: · ┃ [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
Point as Z;
|
|
}
|
|
|
|
// --- fail_impl_simple_two_interfaces.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {}
|
|
interface Y {}
|
|
|
|
// This creates a dependency cycle with itself.
|
|
impl forall [T: Z & Y] T as Z {}
|
|
|
|
class Point {}
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: ╭─┤ fail_impl_simple_two_interfaces.carbon:[[@LINE+19]]:3
|
|
// CHECK:STDERR: │ impl forall [T: Z & Y] T as Z {}
|
|
// CHECK:STDERR: · ───────────────┬───────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches
|
|
// CHECK:STDERR: · │ [ImplLookupCycleNote]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Point as Z;
|
|
// CHECK:STDERR: · ━━━━━┳━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cycle found in search for impl of `Z & Y` for type `Point`
|
|
// CHECK:STDERR: · ┃ [ImplLookupCycle]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: ╭─┤ fail_impl_simple_two_interfaces.carbon:[[@LINE+7]]:3
|
|
// CHECK:STDERR: -> │ Point as Z;
|
|
// CHECK:STDERR: · ━━━━━┳━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot convert type `Point` into type implementing `Z`
|
|
// CHECK:STDERR: · ┃ [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
Point as Z;
|
|
}
|
|
|
|
// --- fail_impl_long_cycle.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface X {}
|
|
interface Y {}
|
|
interface Z {}
|
|
|
|
// This creates a dependency cycle with itself.
|
|
impl forall [T: X] T as Y {}
|
|
impl forall [T: Y] T as Z {}
|
|
impl forall [T: Z] T as X {}
|
|
|
|
class C {}
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: ╭─┤ fail_impl_long_cycle.carbon:[[@LINE+24]]:3
|
|
// CHECK:STDERR: │ impl forall [T: X] T as Y {}
|
|
// CHECK:STDERR: · ─────────────┬─────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches [ImplLookupCycleNote]
|
|
// CHECK:STDERR: │ impl forall [T: Y] T as Z {}
|
|
// CHECK:STDERR: · ─────────────┬─────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches [ImplLookupCycleNote]
|
|
// CHECK:STDERR: │ impl forall [T: Z] T as X {}
|
|
// CHECK:STDERR: · ─────────────┬─────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches [ImplLookupCycleNote]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ C as Z;
|
|
// CHECK:STDERR: · ━━━┳━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cycle found in search for impl of `Z` for type `C`
|
|
// CHECK:STDERR: · ┃ [ImplLookupCycle]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: ╭─┤ fail_impl_long_cycle.carbon:[[@LINE+7]]:3
|
|
// CHECK:STDERR: -> │ C as Z;
|
|
// CHECK:STDERR: · ━━━┳━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot convert type `C` into type implementing `Z`
|
|
// CHECK:STDERR: · ┃ [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
C as Z;
|
|
}
|
|
|
|
// --- fail_impl_cycle_one_generic_param.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface ComparableWith(T: type) {}
|
|
|
|
// This creates a dependency cycle with itself.
|
|
impl forall [U: type, T: ComparableWith(U)]
|
|
U as ComparableWith(T) {}
|
|
|
|
class C {}
|
|
class D {}
|
|
|
|
fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: ╭─┤ fail_impl_cycle_one_generic_param.carbon:[[@LINE+29]]:3
|
|
// CHECK:STDERR: │ impl forall [U: type, T: ComparableWith(U)]
|
|
// CHECK:STDERR: · ─────────────────────┬─────────────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches
|
|
// CHECK:STDERR: · │ [ImplLookupCycleNote]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: │ fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
// CHECK:STDERR: · ───┬───
|
|
// CHECK:STDERR: · ╰──┤ while deducing parameters of generic declared here
|
|
// CHECK:STDERR: · │ [DeductionGenericHere]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Compare({} as C, {} as C);
|
|
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cycle found in search for impl of `ComparableWith(C)` for type `C`
|
|
// CHECK:STDERR: · ┃ [ImplLookupCycle]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: ╭─┤ fail_impl_cycle_one_generic_param.carbon:[[@LINE+12]]:3
|
|
// CHECK:STDERR: │ fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
// CHECK:STDERR: · ───┬───
|
|
// CHECK:STDERR: · ╰──┤ while deducing parameters of generic declared here
|
|
// CHECK:STDERR: · │ [DeductionGenericHere]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Compare({} as C, {} as C);
|
|
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot convert type `C` into type implementing `ComparableWith(C)`
|
|
// CHECK:STDERR: · ┃ [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
Compare({} as C, {} as C);
|
|
|
|
// CHECK:STDERR: ╭─┤ fail_impl_cycle_one_generic_param.carbon:[[@LINE+31]]:3
|
|
// CHECK:STDERR: │ impl forall [U: type, T: ComparableWith(U)]
|
|
// CHECK:STDERR: · ─────────────────────┬─────────────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches
|
|
// CHECK:STDERR: · │ │ [ImplLookupCycleNote]
|
|
// CHECK:STDERR: · ╰────┤ determining if this impl clause matches
|
|
// CHECK:STDERR: · │ [ImplLookupCycleNote]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: │ fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
// CHECK:STDERR: · ───┬───
|
|
// CHECK:STDERR: · ╰──┤ while deducing parameters of generic declared here
|
|
// CHECK:STDERR: · │ [DeductionGenericHere]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Compare({} as C, {} as D);
|
|
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cycle found in search for impl of `ComparableWith(C)` for type `D`
|
|
// CHECK:STDERR: · ┃ [ImplLookupCycle]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: ╭─┤ fail_impl_cycle_one_generic_param.carbon:[[@LINE+12]]:3
|
|
// CHECK:STDERR: │ fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
// CHECK:STDERR: · ───┬───
|
|
// CHECK:STDERR: · ╰──┤ while deducing parameters of generic declared here
|
|
// CHECK:STDERR: · │ [DeductionGenericHere]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Compare({} as C, {} as D);
|
|
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot convert type `D` into type implementing `ComparableWith(C)`
|
|
// CHECK:STDERR: · ┃ [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
Compare({} as C, {} as D);
|
|
|
|
// CHECK:STDERR: ╭─┤ fail_impl_cycle_one_generic_param.carbon:[[@LINE+31]]:3
|
|
// CHECK:STDERR: │ impl forall [U: type, T: ComparableWith(U)]
|
|
// CHECK:STDERR: · ─────────────────────┬─────────────────────
|
|
// CHECK:STDERR: · ╰──┤ determining if this impl clause matches
|
|
// CHECK:STDERR: · │ │ [ImplLookupCycleNote]
|
|
// CHECK:STDERR: · ╰────┤ determining if this impl clause matches
|
|
// CHECK:STDERR: · │ [ImplLookupCycleNote]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: │ fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
// CHECK:STDERR: · ───┬───
|
|
// CHECK:STDERR: · ╰──┤ while deducing parameters of generic declared here
|
|
// CHECK:STDERR: · │ [DeductionGenericHere]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Compare({} as D, {} as C);
|
|
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cycle found in search for impl of `ComparableWith(D)` for type `C`
|
|
// CHECK:STDERR: · ┃ [ImplLookupCycle]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: ╭─┤ fail_impl_cycle_one_generic_param.carbon:[[@LINE+12]]:3
|
|
// CHECK:STDERR: │ fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
// CHECK:STDERR: · ───┬───
|
|
// CHECK:STDERR: · ╰──┤ while deducing parameters of generic declared here
|
|
// CHECK:STDERR: · │ [DeductionGenericHere]
|
|
// CHECK:STDERR: ┆
|
|
// CHECK:STDERR: -> │ Compare({} as D, {} as C);
|
|
// CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot convert type `C` into type implementing `ComparableWith(D)`
|
|
// CHECK:STDERR: · ┃ [ConversionFailureTypeToFacet]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
Compare({} as D, {} as C);
|
|
}
|
|
|
|
// --- impl_recurse_with_simpler_type_no_cycle.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class Wraps(T: type) {}
|
|
|
|
interface Printable {}
|
|
|
|
// If T is printable, so is Wraps(T).
|
|
impl forall [T: Printable] Wraps(T) as Printable {}
|
|
|
|
class C {}
|
|
impl C as Printable {}
|
|
|
|
fn F() {
|
|
({} as C) as (C as Printable);
|
|
({} as Wraps(C)) as (Wraps(C) as Printable);
|
|
}
|
|
|
|
// --- impl_recurse_with_simpler_type_in_generic_param_no_cycle.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class Wraps(T: type) {}
|
|
|
|
interface ComparableTo(T: type) {}
|
|
|
|
// If U is comparable to T, then U is comparable to Wraps(T).
|
|
impl forall [T: type, U: ComparableTo(T)] U as ComparableTo(Wraps(T)) {}
|
|
// If U is comparable to T then Wraps(U) is comparable to T.
|
|
impl forall [T: type, U: ComparableTo(T)] Wraps(U) as ComparableTo(T) {}
|
|
|
|
class C {}
|
|
class D {}
|
|
impl C as ComparableTo(D) {}
|
|
|
|
fn F() {
|
|
({} as C) as (C as ComparableTo(D));
|
|
({} as C) as (C as ComparableTo(Wraps(D)));
|
|
({} as Wraps(C)) as (Wraps(C) as ComparableTo(D));
|
|
({} as Wraps(C)) as (Wraps(C) as ComparableTo(Wraps(D)));
|
|
}
|
|
|
|
// --- impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// Implement this for a type in one direction.
|
|
interface ComparableTo(T: type) {}
|
|
// Use this as a bound with two types in any direction.
|
|
interface ComparableWith(T: type) {}
|
|
|
|
class C {}
|
|
class D {}
|
|
// C is comparable to D. This should imply:
|
|
// - C is ComparableWith(C).
|
|
// - C is ComparableWith(D).
|
|
// - D is ComparableWith(C).
|
|
impl C as ComparableTo(D) {}
|
|
|
|
match_first {
|
|
|
|
// T is always ComparableWith(T).
|
|
impl forall [T: type] T as ComparableWith(T) {}
|
|
// If U is ComparableTo(T), U is ComparableWith(T).
|
|
impl forall [T: type, U: ComparableTo(T)] U as ComparableWith(T) {}
|
|
// If U is ComparableTo(T), T is ComparableWith(U).
|
|
impl forall [T: type, U: ComparableTo(T)] T as ComparableWith(U) {}
|
|
|
|
}
|
|
|
|
fn Compare[T: type, U: ComparableWith(T)](unused t: T, unused u: U) {}
|
|
|
|
fn F() {
|
|
Compare({} as C, {} as C);
|
|
Compare({} as C, {} as D);
|
|
Compare({} as D, {} as C);
|
|
}
|
|
|
|
// --- deduce_cycle_without_symbolic.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface First {}
|
|
interface Second {}
|
|
|
|
// When answering a query "does T impl Second", we must avoid trying to deduce
|
|
// parameters for this impl. Not only is doing so unnecessary, it would start a new
|
|
// "does T impl Second" query, leading to a "cycle in impl lookup" error.
|
|
impl forall [T: Second] T as First {}
|
|
impl forall [T: type] T as Second {}
|
|
|
|
class C {}
|
|
|
|
fn F() {
|
|
({} as C) as (C as Second);
|
|
({} as C) as (C as First);
|
|
}
|
|
|
|
// --- deduce_cycle_with_symbolic.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface First(T: type) {}
|
|
interface Second {}
|
|
|
|
// When answering a query "does T impl Second", we must avoid trying to deduce
|
|
// parameters for this impl. Not only is doing so unnecessary, it would start a new
|
|
// "does T impl Second" query, leading to a "cycle in impl lookup" error.
|
|
impl forall [T: Second, U: type] T as First(U) {}
|
|
impl forall [T: type] T as Second {}
|
|
|
|
class C {}
|
|
|
|
fn F() {
|
|
({} as C) as (C as Second);
|
|
({} as C) as (C as First(C));
|
|
}
|