Files
carbon-lang/toolchain/check/testdata/class/fail_addr_self.carbon
T
Richard SmithandJon Ross-Perkins 187a3608df Use As and ImplicitAs interfaces for conversions. (#4209)
Add these interfaces to the core library. For now, they're two separate
interfaces because we don't yet support one interface extending another.

This collapses a lot of the layering in check: for example, the call
building logic depends on implicit conversions, conversions now depend
on the overloaded operator machinery, and that machinery depends on
building calls.

In passing, improve the diagnostics for failing to find a name required
from the prelude. Also convert all the transitively-called code from
`NodeId` to `LocId` given the latter is what the conversion machinery
has available.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2024-09-05 23:39:58 +00:00

232 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
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_addr_self.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_addr_self.carbon
class Class {
fn F[addr self: Class*]();
fn G[addr self: Class]();
}
fn F(c: Class, p: Class*) {
// CHECK:STDERR: fail_addr_self.carbon:[[@LINE+7]]:6: ERROR: `addr self` method cannot be invoked on a value.
// CHECK:STDERR: c.F();
// CHECK:STDERR: ^
// CHECK:STDERR: fail_addr_self.carbon:[[@LINE-8]]:13: Initializing `addr self` parameter of method declared here.
// CHECK:STDERR: fn F[addr self: Class*]();
// CHECK:STDERR: ^~~~
// CHECK:STDERR:
c.F();
// CHECK:STDERR: fail_addr_self.carbon:[[@LINE+7]]:6: ERROR: `addr self` method cannot be invoked on a value.
// CHECK:STDERR: c.G();
// CHECK:STDERR: ^
// CHECK:STDERR: fail_addr_self.carbon:[[@LINE-16]]:13: Initializing `addr self` parameter of method declared here.
// CHECK:STDERR: fn G[addr self: Class]();
// CHECK:STDERR: ^~~~
// CHECK:STDERR:
c.G();
// This call is OK.
(*p).F();
// CHECK:STDERR: fail_addr_self.carbon:[[@LINE+9]]:3: ERROR: Cannot implicitly convert from `Class*` to `Class`.
// CHECK:STDERR: (*p).G();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_addr_self.carbon:[[@LINE+6]]:3: Type `Class*` does not implement interface `ImplicitAs`.
// CHECK:STDERR: (*p).G();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_addr_self.carbon:[[@LINE-31]]:13: Initializing `addr self` parameter of method declared here.
// CHECK:STDERR: fn G[addr self: Class]();
// CHECK:STDERR: ^~~~
(*p).G();
}
// CHECK:STDOUT: --- fail_addr_self.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %Class: type = class_type @Class [template]
// CHECK:STDOUT: %.1: type = ptr_type %Class [template]
// CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
// CHECK:STDOUT: %.2: type = tuple_type () [template]
// CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
// CHECK:STDOUT: %G.type: type = fn_type @G [template]
// CHECK:STDOUT: %G: %G.type = struct_value () [template]
// CHECK:STDOUT: %.3: type = struct_type {} [template]
// CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
// CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
// CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
// CHECK:STDOUT: %ImplicitAs.type: type = generic_interface_type @ImplicitAs [template]
// CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type = struct_value () [template]
// CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic]
// CHECK:STDOUT: %.5: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
// CHECK:STDOUT: %Self.1: @ImplicitAs.%.1 (%.5) = bind_symbolic_name Self 1 [symbolic]
// CHECK:STDOUT: %Self.2: %.5 = bind_symbolic_name Self 1 [symbolic]
// CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
// CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
// CHECK:STDOUT: %.6: type = assoc_entity_type %.5, %Convert.type.1 [symbolic]
// CHECK:STDOUT: %.7: %.6 = assoc_entity element0, imports.%import_ref.5 [symbolic]
// CHECK:STDOUT: %.8: type = interface_type @ImplicitAs, @ImplicitAs(%Class) [template]
// CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(%Class) [template]
// CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
// CHECK:STDOUT: %.9: type = assoc_entity_type %.8, %Convert.type.2 [template]
// CHECK:STDOUT: %.10: %.9 = assoc_entity element0, imports.%import_ref.5 [template]
// CHECK:STDOUT: %.11: %.6 = assoc_entity element0, imports.%import_ref.6 [symbolic]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
// CHECK:STDOUT: .ImplicitAs = %import_ref.1
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/operators
// CHECK:STDOUT: import Core//prelude/types
// CHECK:STDOUT: import Core//prelude/operators/arithmetic
// CHECK:STDOUT: import Core//prelude/operators/as
// CHECK:STDOUT: import Core//prelude/operators/bitwise
// CHECK:STDOUT: import Core//prelude/operators/comparison
// CHECK:STDOUT: import Core//prelude/types/bool
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: %ImplicitAs.type = import_ref Core//prelude/operators/as, inst+37, loaded [template = constants.%ImplicitAs]
// CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/as, inst+42, unloaded
// CHECK:STDOUT: %import_ref.3: @ImplicitAs.%.2 (%.6) = import_ref Core//prelude/operators/as, inst+59, loaded [symbolic = @ImplicitAs.%.3 (constants.%.11)]
// CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/as, inst+52, unloaded
// CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+52, unloaded
// CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+52, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .Class = %Class.decl
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
// CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
// CHECK:STDOUT: %Class.ref.loc16_9: type = name_ref Class, %Class.decl [template = constants.%Class]
// CHECK:STDOUT: %c.loc16_6.1: %Class = param c
// CHECK:STDOUT: @F.2.%c: %Class = bind_name c, %c.loc16_6.1
// CHECK:STDOUT: %Class.ref.loc16_19: type = name_ref Class, %Class.decl [template = constants.%Class]
// CHECK:STDOUT: %.loc16: type = ptr_type %Class [template = constants.%.1]
// CHECK:STDOUT: %p.loc16_16.1: %.1 = param p
// CHECK:STDOUT: @F.2.%p: %.1 = bind_name p, %p.loc16_16.1
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
// CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.5)]
// CHECK:STDOUT: %Self: %.5 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
// CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
// CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
// CHECK:STDOUT: %.2: type = assoc_entity_type @ImplicitAs.%.1 (%.5), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.2 (constants.%.6)]
// CHECK:STDOUT: %.3: @ImplicitAs.%.2 (%.6) = assoc_entity element0, imports.%import_ref.5 [symbolic = %.3 (constants.%.7)]
// CHECK:STDOUT:
// CHECK:STDOUT: interface {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.2
// CHECK:STDOUT: .Convert = imports.%import_ref.3
// CHECK:STDOUT: witness = (imports.%import_ref.4)
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Class {
// CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
// CHECK:STDOUT: %Class.ref.loc12: type = name_ref Class, file.%Class.decl [template = constants.%Class]
// CHECK:STDOUT: %.loc12_24: type = ptr_type %Class [template = constants.%.1]
// CHECK:STDOUT: %self.loc12_13.1: %.1 = param self
// CHECK:STDOUT: %self.loc12_13.3: %.1 = bind_name self, %self.loc12_13.1
// CHECK:STDOUT: %.loc12_8: %.1 = addr_pattern %self.loc12_13.3
// CHECK:STDOUT: }
// CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
// CHECK:STDOUT: %Class.ref.loc13: type = name_ref Class, file.%Class.decl [template = constants.%Class]
// CHECK:STDOUT: %self.loc13_13.1: %Class = param self
// CHECK:STDOUT: %self.loc13_13.3: %Class = bind_name self, %self.loc13_13.1
// CHECK:STDOUT: %.loc13: %Class = addr_pattern %self.loc13_13.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%Class
// CHECK:STDOUT: .F = %F.decl
// CHECK:STDOUT: .G = %G.decl
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F.1[addr @Class.%self.loc12_13.3: %.1]();
// CHECK:STDOUT:
// CHECK:STDOUT: fn @G[addr @Class.%self.loc13_13.3: %Class]();
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F.2(%c: %Class, %p: %.1) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %c.ref.loc24: %Class = name_ref c, %c
// CHECK:STDOUT: %F.ref.loc24: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
// CHECK:STDOUT: %.loc24: <bound method> = bound_method %c.ref.loc24, %F.ref.loc24
// CHECK:STDOUT: %F.call.loc24: init %.2 = call %.loc24(<invalid>) [template = <error>]
// CHECK:STDOUT: %c.ref.loc33: %Class = name_ref c, %c
// CHECK:STDOUT: %G.ref.loc33: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
// CHECK:STDOUT: %.loc33: <bound method> = bound_method %c.ref.loc33, %G.ref.loc33
// CHECK:STDOUT: %G.call.loc33: init %.2 = call %.loc33(<invalid>) [template = <error>]
// CHECK:STDOUT: %p.ref.loc36: %.1 = name_ref p, %p
// CHECK:STDOUT: %.loc36_4.1: ref %Class = deref %p.ref.loc36
// CHECK:STDOUT: %F.ref.loc36: %F.type.1 = name_ref F, @Class.%F.decl [template = constants.%F.1]
// CHECK:STDOUT: %.loc36_7: <bound method> = bound_method %.loc36_4.1, %F.ref.loc36
// CHECK:STDOUT: %.loc36_4.2: %.1 = addr_of %.loc36_4.1
// CHECK:STDOUT: %F.call.loc36: init %.2 = call %.loc36_7(%.loc36_4.2)
// CHECK:STDOUT: %p.ref.loc47: %.1 = name_ref p, %p
// CHECK:STDOUT: %.loc47_4.1: ref %Class = deref %p.ref.loc47
// CHECK:STDOUT: %G.ref.loc47: %G.type = name_ref G, @Class.%G.decl [template = constants.%G]
// CHECK:STDOUT: %.loc47_7: <bound method> = bound_method %.loc47_4.1, %G.ref.loc47
// CHECK:STDOUT: %.loc47_4.2: %.1 = addr_of %.loc47_4.1
// CHECK:STDOUT: %.loc47_9.1: init type = call constants.%ImplicitAs(constants.%Class) [template = constants.%.8]
// CHECK:STDOUT: %.loc47_9.2: %.9 = specific_constant imports.%import_ref.3, @ImplicitAs(constants.%Class) [template = constants.%.10]
// CHECK:STDOUT: %Convert.ref: %.9 = name_ref Convert, %.loc47_9.2 [template = constants.%.10]
// CHECK:STDOUT: %.loc47_9.3: %Class = converted %.loc47_4.2, <error> [template = <error>]
// CHECK:STDOUT: %G.call.loc47: init %.2 = call %.loc47_7(<invalid>) [template = <error>]
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%.1 (%.5)) {
// CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
// CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.5)]
// CHECK:STDOUT: %Self: %.5 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
// CHECK:STDOUT:
// CHECK:STDOUT: fn[%self: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
// CHECK:STDOUT: %Dest => constants.%Dest
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
// CHECK:STDOUT: %Dest => constants.%Dest
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
// CHECK:STDOUT: %Dest => constants.%Dest
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
// CHECK:STDOUT: %Dest => constants.%Dest
// CHECK:STDOUT: %.1 => constants.%.5
// CHECK:STDOUT: %Self => constants.%Self.1
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @ImplicitAs(constants.%Class) {
// CHECK:STDOUT: %Dest => constants.%Class
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %.1 => constants.%.8
// CHECK:STDOUT: %Self => constants.%Self.2
// CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
// CHECK:STDOUT: %Convert => constants.%Convert.2
// CHECK:STDOUT: %.2 => constants.%.9
// CHECK:STDOUT: %.3 => constants.%.10
// CHECK:STDOUT: }
// CHECK:STDOUT: