Files
carbon-lang/toolchain/check/testdata/class/fail_derived_to_base.carbon
T
67f2c9ce26 Add a FacetValue instruction (#4545)
The new `FacetValue` instruction represents `C as I` for some type `C`
and facet type `I`. It is named `FacetValue` instead of just `Facet` to
parallel the `FacetType` instruction.

This PR uses this instruction represent the facet value `Self` in an
`impl` declaration. This instruction will be used in the future to also
support things like:

* `C as I` where `C` is a class; and
* forming a specific for a generic with a `T:! I` parameter where `T` is
being given a concrete value.

(Here `I` is an interface or other non-`type` facet type.)

Also do some renaming and add some comments to make things a bit more
clear.

* `FacetTypeAccess` -> `FacetAccessType` to clarify this is not access
of a facet type, but access of the type of a facet
* `.facet_id` -> `.facet_value_inst_id` to parallel the `FacetValue`
instruction

`FacetAccessWitness` will be in a future PR.

---------

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-11-21 22:29:53 +00:00

187 lines
10 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
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_derived_to_base.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_derived_to_base.carbon
base class A1 {
var a: i32;
}
base class A2 {
var a: i32;
}
class B2 {
extend base: A2;
var b: i32;
}
// CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+7]]:38: error: cannot implicitly convert from `B2*` to `A1*` [ImplicitAsConversionFailure]
// CHECK:STDERR: fn ConvertUnrelated(p: B2*) -> A1* { return p; }
// CHECK:STDERR: ^~~~~~~~~
// CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+4]]:38: note: type `B2*` does not implement interface `ImplicitAs(A1*)` [MissingImplInMemberAccessNote]
// CHECK:STDERR: fn ConvertUnrelated(p: B2*) -> A1* { return p; }
// CHECK:STDERR: ^~~~~~~~~
// CHECK:STDERR:
fn ConvertUnrelated(p: B2*) -> A1* { return p; }
class Incomplete;
// CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+6]]:47: error: cannot implicitly convert from `Incomplete*` to `A2*` [ImplicitAsConversionFailure]
// CHECK:STDERR: fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
// CHECK:STDERR: ^~~~~~~~~
// CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+3]]:47: note: type `Incomplete*` does not implement interface `ImplicitAs(A2*)` [MissingImplInMemberAccessNote]
// CHECK:STDERR: fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
// CHECK:STDERR: ^~~~~~~~~
fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
// CHECK:STDOUT: --- fail_derived_to_base.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %A1: type = class_type @A1 [template]
// CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
// CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
// CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
// CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
// CHECK:STDOUT: %.2: type = unbound_element_type %A1, %i32 [template]
// CHECK:STDOUT: %.3: type = struct_type {.a: %i32} [template]
// CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
// CHECK:STDOUT: %A2: type = class_type @A2 [template]
// CHECK:STDOUT: %.5: type = unbound_element_type %A2, %i32 [template]
// CHECK:STDOUT: %B2: type = class_type @B2 [template]
// CHECK:STDOUT: %.7: type = unbound_element_type %B2, %A2 [template]
// CHECK:STDOUT: %.8: type = unbound_element_type %B2, %i32 [template]
// CHECK:STDOUT: %.9: type = struct_type {.base: %A2, .b: %i32} [template]
// CHECK:STDOUT: %.10: <witness> = complete_type_witness %.9 [template]
// CHECK:STDOUT: %.11: type = ptr_type %B2 [template]
// CHECK:STDOUT: %.12: type = ptr_type %A1 [template]
// CHECK:STDOUT: %ConvertUnrelated.type: type = fn_type @ConvertUnrelated [template]
// CHECK:STDOUT: %ConvertUnrelated: %ConvertUnrelated.type = struct_value () [template]
// CHECK:STDOUT: %Incomplete: type = class_type @Incomplete [template]
// CHECK:STDOUT: %.39: type = ptr_type %Incomplete [template]
// CHECK:STDOUT: %.40: type = ptr_type %A2 [template]
// CHECK:STDOUT: %ConvertIncomplete.type: type = fn_type @ConvertIncomplete [template]
// CHECK:STDOUT: %ConvertIncomplete: %ConvertIncomplete.type = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
// CHECK:STDOUT: .Int = %import_ref.1
// CHECK:STDOUT: .ImplicitAs = %import_ref.2
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .A1 = %A1.decl
// CHECK:STDOUT: .A2 = %A2.decl
// CHECK:STDOUT: .B2 = %B2.decl
// CHECK:STDOUT: .ConvertUnrelated = %ConvertUnrelated.decl
// CHECK:STDOUT: .Incomplete = %Incomplete.decl
// CHECK:STDOUT: .ConvertIncomplete = %ConvertIncomplete.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %A1.decl: type = class_decl @A1 [template = constants.%A1] {} {}
// CHECK:STDOUT: %A2.decl: type = class_decl @A2 [template = constants.%A2] {} {}
// CHECK:STDOUT: %B2.decl: type = class_decl @B2 [template = constants.%B2] {} {}
// CHECK:STDOUT: %ConvertUnrelated.decl: %ConvertUnrelated.type = fn_decl @ConvertUnrelated [template = constants.%ConvertUnrelated] {
// CHECK:STDOUT: %p.patt: %.11 = binding_pattern p
// CHECK:STDOUT: %p.param_patt: %.11 = value_param_pattern %p.patt, runtime_param0
// CHECK:STDOUT: %return.patt: %.12 = return_slot_pattern
// CHECK:STDOUT: %return.param_patt: %.12 = out_param_pattern %return.patt, runtime_param1
// CHECK:STDOUT: } {
// CHECK:STDOUT: %B2.ref: type = name_ref B2, file.%B2.decl [template = constants.%B2]
// CHECK:STDOUT: %.loc31_26: type = ptr_type %B2 [template = constants.%.11]
// CHECK:STDOUT: %A1.ref: type = name_ref A1, file.%A1.decl [template = constants.%A1]
// CHECK:STDOUT: %.loc31_34: type = ptr_type %A1 [template = constants.%.12]
// CHECK:STDOUT: %p.param: %.11 = value_param runtime_param0
// CHECK:STDOUT: %p: %.11 = bind_name p, %p.param
// CHECK:STDOUT: %return.param: ref %.12 = out_param runtime_param1
// CHECK:STDOUT: %return: ref %.12 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %Incomplete.decl: type = class_decl @Incomplete [template = constants.%Incomplete] {} {}
// CHECK:STDOUT: %ConvertIncomplete.decl: %ConvertIncomplete.type = fn_decl @ConvertIncomplete [template = constants.%ConvertIncomplete] {
// CHECK:STDOUT: %p.patt: %.39 = binding_pattern p
// CHECK:STDOUT: %p.param_patt: %.39 = value_param_pattern %p.patt, runtime_param0
// CHECK:STDOUT: %return.patt: %.40 = return_slot_pattern
// CHECK:STDOUT: %return.param_patt: %.40 = out_param_pattern %return.patt, runtime_param1
// CHECK:STDOUT: } {
// CHECK:STDOUT: %Incomplete.ref: type = name_ref Incomplete, file.%Incomplete.decl [template = constants.%Incomplete]
// CHECK:STDOUT: %.loc41_35: type = ptr_type %Incomplete [template = constants.%.39]
// CHECK:STDOUT: %A2.ref: type = name_ref A2, file.%A2.decl [template = constants.%A2]
// CHECK:STDOUT: %.loc41_43: type = ptr_type %A2 [template = constants.%.40]
// CHECK:STDOUT: %p.param: %.39 = value_param runtime_param0
// CHECK:STDOUT: %p: %.39 = bind_name p, %p.param
// CHECK:STDOUT: %return.param: ref %.40 = out_param runtime_param1
// CHECK:STDOUT: %return: ref %.40 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A1 {
// CHECK:STDOUT: %.loc12_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
// CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc12_10.1) [template = constants.%i32]
// CHECK:STDOUT: %.loc12_10.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
// CHECK:STDOUT: %.loc12_10.3: type = converted %int.make_type_signed, %.loc12_10.2 [template = constants.%i32]
// CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
// CHECK:STDOUT: %.loc13: <witness> = complete_type_witness %.3 [template = constants.%.4]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%A1
// CHECK:STDOUT: .a = %.loc12_8
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A2 {
// CHECK:STDOUT: %.loc16_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
// CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc16_10.1) [template = constants.%i32]
// CHECK:STDOUT: %.loc16_10.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
// CHECK:STDOUT: %.loc16_10.3: type = converted %int.make_type_signed, %.loc16_10.2 [template = constants.%i32]
// CHECK:STDOUT: %.loc16_8: %.5 = field_decl a, element0 [template]
// CHECK:STDOUT: %.loc17: <witness> = complete_type_witness %.3 [template = constants.%.4]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%A2
// CHECK:STDOUT: .a = %.loc16_8
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @B2 {
// CHECK:STDOUT: %A2.ref: type = name_ref A2, file.%A2.decl [template = constants.%A2]
// CHECK:STDOUT: %.loc20: %.7 = base_decl %A2, element0 [template]
// CHECK:STDOUT: %.loc21_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
// CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc21_10.1) [template = constants.%i32]
// CHECK:STDOUT: %.loc21_10.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
// CHECK:STDOUT: %.loc21_10.3: type = converted %int.make_type_signed, %.loc21_10.2 [template = constants.%i32]
// CHECK:STDOUT: %.loc21_8: %.8 = field_decl b, element1 [template]
// CHECK:STDOUT: %.loc22: <witness> = complete_type_witness %.9 [template = constants.%.10]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%B2
// CHECK:STDOUT: .base = %.loc20
// CHECK:STDOUT: .b = %.loc21_8
// CHECK:STDOUT: extend %A2.ref
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Incomplete;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertUnrelated(%p.param_patt: %.11) -> %.12 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: %.11 = name_ref p, %p
// CHECK:STDOUT: %.loc31_46: %.12 = converted %p.ref, <error> [template = <error>]
// CHECK:STDOUT: return <error>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertIncomplete(%p.param_patt: %.39) -> %.40 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: %.39 = name_ref p, %p
// CHECK:STDOUT: %.loc41_55: %.40 = converted %p.ref, <error> [template = <error>]
// CHECK:STDOUT: return <error>
// CHECK:STDOUT: }
// CHECK:STDOUT: