mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 15:02:44 +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
515 lines
13 KiB
Plaintext
515 lines
13 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/specialization.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/specialization.carbon
|
|
|
|
// --- specialized_self_first.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C {}
|
|
impl C as Z(C) where .X = C {}
|
|
|
|
impl forall [T: type] T as Z(T) where .X = () {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket impl
|
|
// of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: C.(Z(C).X) = {} as C;
|
|
}
|
|
|
|
// --- specialized_self_second.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type] T as Z(T) where .X = () {}
|
|
|
|
class C {}
|
|
impl C as Z(C) where .X = C {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket impl
|
|
// of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: C.(Z(C).X) = {} as C;
|
|
}
|
|
|
|
// --- specialized_constraint_first.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C {}
|
|
|
|
impl C as Z(C) where .X = C {}
|
|
impl forall [T: type] C as Z(T) where .X = () {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket impl
|
|
// of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: C.(Z(C).X) = {} as C;
|
|
}
|
|
|
|
// --- specialized_constraint_second.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C {}
|
|
|
|
impl forall [T: type] C as Z(T) where .X = () {}
|
|
impl C as Z(C) where .X = C {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket impl
|
|
// of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: C.(Z(C).X) = {} as C;
|
|
}
|
|
|
|
// --- specialized_self_vs_constraint_self_first.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C(T: type) {}
|
|
|
|
impl forall [T: type] C(()) as Z(T) where .X = C(()) {}
|
|
impl forall [T: type] C(T) as Z(C(())) where .X = () {}
|
|
|
|
fn F() {
|
|
// The specialization of `C(())` should match in preference to the blanket impl
|
|
// of `C(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: C(()).(Z(C(())).X) = {} as C(());
|
|
}
|
|
|
|
// --- specialized_self_vs_constraint_self_second.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C(T: type) {}
|
|
|
|
impl forall [T: type] C(T) as Z(C(())) where .X = () {}
|
|
impl forall [T: type] C(()) as Z(T) where .X = C(()) {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket impl
|
|
// of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: C(()).(Z(C(())).X) = {} as C(());
|
|
}
|
|
|
|
// --- generic_class_with_fully_specified_impl.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type] T as Z(T) where .X = {.a: ()} {}
|
|
|
|
class C(T: type) {}
|
|
impl C(()) as Z(()) where .X = C(()) {}
|
|
|
|
impl forall [T: type] C(T) as Z(T) where .X = {.b: ()} {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket
|
|
// impls of `Z(T)`. If a blanket impl is chosen, then `a` will have a struct
|
|
// type which will fail to typecheck here when constructed from a `C` value.
|
|
let unused a: C(()).(Z(()).X) = {} as C(());
|
|
}
|
|
|
|
// --- generic_class_with_blanket_impl_first.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C(T: type) {}
|
|
|
|
impl forall [T: type] C(T) as Z(T) where .X = C(()) {}
|
|
|
|
impl forall [T: type] T as Z(T) where .X = () {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket
|
|
// impls of `Z(T)`. If a blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here when constructed from a `C` value.
|
|
let unused a: C(()).(Z(()).X) = {} as C(());
|
|
}
|
|
|
|
// --- generic_class_with_blanket_impl_second.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type] T as Z(T) where .X = () {}
|
|
|
|
class C(T: type) {}
|
|
|
|
impl forall [T: type] C(T) as Z(T) where .X = C(()) {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket
|
|
// impls of `Z(T)`. If a blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here when constructed from a `C` value.
|
|
let unused a: C(()).(Z(()).X) = {} as C(());
|
|
}
|
|
|
|
// --- specialized_class_with_facet_value_param.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X: type;
|
|
}
|
|
|
|
class D(T: type) {}
|
|
impl forall [T: type] D(T) as Z where .X = D(()) {}
|
|
|
|
class E {}
|
|
impl E as Z where .X = () {}
|
|
|
|
class C(T: Z, U: Z) {}
|
|
|
|
// This places a FacetValue of type FacetType(Z) at the position of `D` in the
|
|
// self type, because the class C requires a facet value satisfying Z. It tests
|
|
// that we correctly determine that this FacetType is not symbolic, and look at
|
|
// the parametes of D for symbolic references.
|
|
impl forall [T: Z] C(T, D(E)) as Z where .X = () {}
|
|
impl forall [T: Z] C(D(T), T) as Z where .X = () {}
|
|
// This is the best match, `T` is in the last position compared to the others.
|
|
impl forall [T: Z] C(D(E), T) as Z where .X = C(E, E) {}
|
|
impl forall [T: Z] C(T, T) as Z where .X = () {}
|
|
|
|
fn F() {
|
|
let unused a: C(D(E), D(E)).(Z.X) = {} as C(E, E);
|
|
}
|
|
|
|
// --- fail_specialized_class_with_symbolic_facet_value_param.carbon
|
|
|
|
interface Z {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type] T as Z where .X = T {}
|
|
|
|
interface Y {}
|
|
class C(T: Y) {}
|
|
|
|
class D {}
|
|
impl D as Y {}
|
|
|
|
// D can be either a concrete or symbolic FacetValue, depending on what the
|
|
// caller has.
|
|
impl forall [D: Y] C(D) as Z where .X = () {}
|
|
|
|
fn F[D: Y](unused d: D) {
|
|
// The FacetValue deduced for the param of `C` will be a symbolic FacetValue
|
|
// because we are in a generic where `D` is an unknown type, which will cause
|
|
// the query and impl self type to be C(FacetValue) for a symbolic FacetValue.
|
|
//
|
|
// CHECK:STDERR: ╭─┤ fail_specialized_class_with_symbolic_facet_value_param.carbon:[[@LINE+8]]:30
|
|
// CHECK:STDERR: -> │ let unused a: C(D).(Z.X) = ();
|
|
// CHECK:STDERR: · ┳━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot implicitly convert expression of
|
|
// CHECK:STDERR: · ┃ type `()` to `C(D).(Z.X)`
|
|
// CHECK:STDERR: · ┃ [ConversionFailure]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
let unused a: C(D).(Z.X) = ();
|
|
}
|
|
|
|
// --- pointer_specialization_first.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X: type;
|
|
}
|
|
|
|
class C {}
|
|
impl C* as Z where .X = C {}
|
|
|
|
impl forall [T: type] T* as Z where .X = () {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket impl
|
|
// of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: (C*).(Z.X) = {} as C;
|
|
}
|
|
|
|
// --- pointer_specialization_second.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type] T* as Z where .X = () {}
|
|
|
|
class C {}
|
|
impl C* as Z where .X = C {}
|
|
|
|
fn F() {
|
|
// The specialization of `Z(C)` should match in preference to the blanket impl
|
|
// of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
|
|
// which will fail to typecheck here.
|
|
let unused a: (C*).(Z.X) = {} as C;
|
|
}
|
|
|
|
// --- cycle_in_deduce_avoided_by_specialization.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let X: type;
|
|
}
|
|
|
|
class C(T: type) {}
|
|
|
|
// This impl makes a cycle, but it's not considered at all for `C(())` since
|
|
// there is another impl with a better type structure, so no diagnostic is
|
|
// emitted.
|
|
impl forall [T: Z] T as Z where .X = () {}
|
|
// Also a cycle, and also a worse match for `C(())`.
|
|
impl forall [T: Z] C(T) as Z where .X = () {}
|
|
|
|
impl C(()) as Z where .X = C(()) {}
|
|
|
|
fn F() {
|
|
let unused a: C(()).(Z.X) = {} as C(());
|
|
}
|
|
|
|
// --- final_specialization_before_generic_use_of_type_constant.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C {}
|
|
|
|
impl forall [T: type, U: type] T as Z(U) where .X = () {}
|
|
|
|
final impl forall [T: type] T as Z(C) where .X = C {}
|
|
|
|
fn F[unused U: type](generic T: Z(C)) {
|
|
// The value of `.X` can be known to be `C` here when the impl `T as Z(C)` is
|
|
// final.
|
|
let unused a: T.X = {} as C;
|
|
}
|
|
|
|
// --- fail_specialization_written_after_generic_use_of_type_constant.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
class C {}
|
|
|
|
impl forall [T: type, U: type] T as Z(U) where .X = () {}
|
|
|
|
fn F[unused U: type](generic T: Z(C)) {
|
|
// The value of `.X` is symbolic, it can't be assigned a value of type `C`.
|
|
// CHECK:STDERR: ╭─┤ fail_specialization_written_after_generic_use_of_type_constant.carbon:[[@LINE+8]]:23
|
|
// CHECK:STDERR: -> │ let unused a: T.X = {} as C;
|
|
// CHECK:STDERR: · ┳━━━━━━
|
|
// CHECK:STDERR: · ┗━━┫ error: cannot implicitly convert expression of type
|
|
// CHECK:STDERR: · ┃ `C` to `T.(Z(C).X)`
|
|
// CHECK:STDERR: · ┃ [ConversionFailure]
|
|
// CHECK:STDERR: ╰────
|
|
// CHECK:STDERR:
|
|
let unused a: T.X = {} as C;
|
|
}
|
|
|
|
final impl forall [T: type] T as Z(C) where .X = C {}
|
|
|
|
// --- specialization_written_after_generic_use.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z {
|
|
let V: type;
|
|
fn ZZ() -> V*;
|
|
}
|
|
|
|
var t: ();
|
|
|
|
interface Y {}
|
|
impl forall [T: Y] T as Z where .V = () {
|
|
fn ZZ() -> ()* { return &t; }
|
|
}
|
|
|
|
fn H(generic W: Z, X: W.(Z.V)*) -> W.(Z.V)* {
|
|
return X;
|
|
}
|
|
|
|
fn G(generic U: Y) -> U.(Z.V)* {
|
|
return H(U, U.(Z.ZZ)());
|
|
}
|
|
|
|
class C {
|
|
impl as Y {}
|
|
}
|
|
|
|
var s: {};
|
|
|
|
impl C as Z where .V = {} {
|
|
fn ZZ() -> {}* { return &s; }
|
|
}
|
|
|
|
fn F() {
|
|
let unused x: {}* = G(C);
|
|
}
|
|
|
|
// --- specialization_written_after_generic_use_with_generic_interface.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let V: type;
|
|
fn ZZ() -> V*;
|
|
}
|
|
|
|
var t: ();
|
|
|
|
interface Y {}
|
|
impl forall [T: Y] T as Z(T) where .V = () {
|
|
fn ZZ() -> ()* { return &t; }
|
|
}
|
|
|
|
fn H(generic U: Y, generic W: Y & Z(U), X: W.(Z(U).V)*) -> W.(Z(U).V)* {
|
|
return X;
|
|
}
|
|
|
|
fn G(generic U: Y) -> U.(Z(U).V)* {
|
|
return H(U, U, U.(Z(U).ZZ)());
|
|
}
|
|
|
|
class C {
|
|
impl as Y {}
|
|
}
|
|
|
|
var s: {};
|
|
|
|
impl C as Z(C) where .V = {} {
|
|
fn ZZ() -> {}* { return &s; }
|
|
}
|
|
|
|
fn F() {
|
|
let unused x: {}* = G(C);
|
|
}
|
|
|
|
// --- type_structure_first_difference.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
fn MakeX() -> X;
|
|
}
|
|
|
|
class C {}
|
|
|
|
// Type structure: "?(?)"
|
|
impl forall [T: type] T as Z(T) where .X = {.less_good: ()} {
|
|
fn MakeX() -> {.less_good: ()} { return {.less_good = ()}; }
|
|
}
|
|
|
|
// Type structure: "?(c)". Should outrank the previous impl.
|
|
impl forall [T: type] T as Z(C) where .X = () {
|
|
fn MakeX() -> () { return (); }
|
|
}
|
|
|
|
fn F(generic T: Z(C)) -> T.(Z(C).X) {
|
|
return T.MakeX();
|
|
}
|
|
|
|
fn G() {
|
|
// This won't typecheck if the first impl is selected.
|
|
let unused a: () = F(C);
|
|
}
|
|
|
|
// --- extend_impl_as_specialization.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type, S: type] T as Z(S) where .X = {} {}
|
|
|
|
class C(S: type) {
|
|
extend impl as Z(S) where .X = () {}
|
|
fn CC(a: Self.(Z(S).X)*) -> Self.(Z(S).X)* { return a; }
|
|
}
|
|
|
|
fn F() {
|
|
var t: ();
|
|
let a: ()* = C(()).CC(&t);
|
|
let unused b: C(()).X = *a;
|
|
}
|
|
|
|
// --- final_impl_as_specialization.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type, S: type] T as Z(S) where .X = {} {}
|
|
|
|
class C(S: type) {
|
|
final impl as Z(S) where .X = () {}
|
|
fn CC() -> Self.(Z(S).X) { return (); }
|
|
}
|
|
|
|
fn F() {
|
|
let unused a: () = C(()).CC();
|
|
}
|
|
|
|
// --- final_extend_impl_as_specialization.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface Z(T: type) {
|
|
let X: type;
|
|
}
|
|
|
|
impl forall [T: type, S: type] T as Z(S) where .X = {} {}
|
|
|
|
class C(S: type) {
|
|
extend final impl as Z(S) where .X = () {}
|
|
fn CC() -> Self.(Z(S).X) { return (); }
|
|
}
|
|
|
|
fn F() {
|
|
let a: () = C(()).CC();
|
|
let unused b: C(()).X = a;
|
|
}
|