Produce helpful diagnostics when converting to a facet fails (#5109)

When converting to a facet there are three different failure modes:

1. You provided a non-type value. Only types can convert to facets. So
we tell you that we found a non-type value.
2. You provided a facet type (which has type TypeType) which does not
have witnesses for the the target facet's type. So we tell you that the
type `T` implements `X` but needs to implement `Y`.
2. You provided a (non-facet-type) concrete type (of type TypeType)
which does not implement the target facet's type (which is a FacetType).
So we tell you that we need the type to implement the FacetType but it
does not.
3. You provided a FacetAccessType (which is of type TypeType also, but
we special case this), whose underlying FacetType is not compatible with
the target facet's type. So we tell you that we need the type to
implement `X` but found a FacetAccessType `T` which implements `Y`.

Closes #5027
This commit is contained in:
Dana Jansens
2025-03-14 15:50:03 +00:00
committed by GitHub
parent db21c38550
commit 417b3833e2
67 changed files with 184 additions and 124 deletions
@@ -11,7 +11,7 @@
fn G(a: i32) {}
fn F() {
// CHECK:STDERR: fail_param_type.carbon:[[@LINE+10]]:5: error: cannot implicitly convert value of type `f64` to `i32` [ImplicitAsConversionFailure]
// CHECK:STDERR: fail_param_type.carbon:[[@LINE+10]]:5: error: cannot implicitly convert value of type `f64` to `i32` [ConversionFailure]
// CHECK:STDERR: G(1.0);
// CHECK:STDERR: ^~~
// CHECK:STDERR: fail_param_type.carbon:[[@LINE+7]]:5: note: type `f64` does not implement interface `Core.ImplicitAs(i32)` [MissingImplInMemberAccessNote]
@@ -11,7 +11,7 @@
fn Foo() -> f64 { return 1.0; }
fn Run() {
// CHECK:STDERR: fail_return_type_mismatch.carbon:[[@LINE+7]]:3: error: cannot implicitly convert value of type `f64` to `i32` [ImplicitAsConversionFailure]
// CHECK:STDERR: fail_return_type_mismatch.carbon:[[@LINE+7]]:3: error: cannot implicitly convert value of type `f64` to `i32` [ConversionFailure]
// CHECK:STDERR: var x: i32 = Foo();
// CHECK:STDERR: ^~~~~~~~~~
// CHECK:STDERR: fail_return_type_mismatch.carbon:[[@LINE+4]]:3: note: type `f64` does not implement interface `Core.ImplicitAs(i32)` [MissingImplInMemberAccessNote]