Files
carbon-lang/toolchain/check/testdata/class/fail_method_modifiers.carbon
T
Richard Smith 0354efa1fc Rework how we check calls to support deduced implicit parameters (#4302)
Instead of the `call` instruction having a block with one argument per
explicit argument, preceded optionally by `self` and followed optionally
by a return slot, change the `call` to store only the *runtime*
arguments. Store an index on the runtime parameters to make it easier to
determine the correspondence between arguments and parameters in a call.
Compile-time parameters, whether implicit or explicit, are no longer
included in the call argument list. Instead, they're tracked only in the
`specific_id` on the callee.

For calls to generic classes and generic interfaces, it no longer makes
sense to form a `call` instruction, given that the entirety of the
result is determined by the `specific_id`, which is now formed when
checking the call. Instead, the `call` instruction now only models
function calls, and not calls to other kinds of parameterized entity
names, and we create a `class_type` or `interface_type` instead of a
`call` instruction to model these kinds of calls. Notionally the model
here is that we're following the #3720 approach for calls, but for now
we inline the `Call.Op` function when forming SemIR.

We now also track the enclosing specific for a generic class or generic
interface that appears within an enclosing generic. This is necessary in
order for deduction of the inner generic parameters to not get confused
by the outer generic parameters being absent.

In order to not regress diagnostics, the template argument deduction
mechanism has been extended to specify the name of the parameter we're
deducing against when possible, and call arity mismatch errors are now
diagnosed before performing deduction rather than afterwards.
2024-09-13 21:31:43 +00:00

162 lines
8.1 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_method_modifiers.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_method_modifiers.carbon
class FinalClass {
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+7]]:3: ERROR: `abstract` not allowed on `fn` declaration in a non-abstract `class` definition.
// CHECK:STDERR: abstract fn Abstract[self: Self]();
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE-5]]:1: Containing definition here.
// CHECK:STDERR: class FinalClass {
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
abstract fn Abstract[self: Self]();
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+7]]:3: ERROR: `virtual` not allowed on `fn` declaration in a non-abstract non-base `class` definition.
// CHECK:STDERR: virtual fn Virtual[self: Self]();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE-14]]:1: Containing definition here.
// CHECK:STDERR: class FinalClass {
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
virtual fn Virtual[self: Self]();
}
abstract class AbstractClass {
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+4]]:3: ERROR: `default` not allowed on `fn` declaration outside of an interface.
// CHECK:STDERR: default fn Default[self: Self]();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
default fn Default[self: Self]();
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+4]]:3: ERROR: `final` not allowed on `fn` declaration outside of an interface.
// CHECK:STDERR: final fn Final[self: Self]();
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
final fn Final[self: Self]();
}
base class BaseClass {
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+6]]:3: ERROR: `abstract` not allowed on `fn` declaration in a non-abstract `class` definition.
// CHECK:STDERR: abstract fn Abstract[self: Self]();
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE-5]]:1: Containing definition here.
// CHECK:STDERR: base class BaseClass {
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
abstract fn Abstract[self: Self]();
}
// CHECK:STDOUT: --- fail_method_modifiers.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %FinalClass: type = class_type @FinalClass [template]
// CHECK:STDOUT: %Abstract.type.1: type = fn_type @Abstract.1 [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %Abstract.1: %Abstract.type.1 = struct_value () [template]
// CHECK:STDOUT: %Virtual.type: type = fn_type @Virtual [template]
// CHECK:STDOUT: %Virtual: %Virtual.type = struct_value () [template]
// CHECK:STDOUT: %.2: type = struct_type {} [template]
// CHECK:STDOUT: %AbstractClass: type = class_type @AbstractClass [template]
// CHECK:STDOUT: %Default.type: type = fn_type @Default [template]
// CHECK:STDOUT: %Default: %Default.type = struct_value () [template]
// CHECK:STDOUT: %Final.type: type = fn_type @Final [template]
// CHECK:STDOUT: %Final: %Final.type = struct_value () [template]
// CHECK:STDOUT: %BaseClass: type = class_type @BaseClass [template]
// CHECK:STDOUT: %Abstract.type.2: type = fn_type @Abstract.2 [template]
// CHECK:STDOUT: %Abstract.2: %Abstract.type.2 = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
// 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: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .FinalClass = %FinalClass.decl
// CHECK:STDOUT: .AbstractClass = %AbstractClass.decl
// CHECK:STDOUT: .BaseClass = %BaseClass.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %FinalClass.decl: type = class_decl @FinalClass [template = constants.%FinalClass] {}
// CHECK:STDOUT: %AbstractClass.decl: type = class_decl @AbstractClass [template = constants.%AbstractClass] {}
// CHECK:STDOUT: %BaseClass.decl: type = class_decl @BaseClass [template = constants.%BaseClass] {}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @FinalClass {
// CHECK:STDOUT: %Abstract.decl: %Abstract.type.1 = fn_decl @Abstract.1 [template = constants.%Abstract.1] {
// CHECK:STDOUT: %Self.ref.loc20: type = name_ref Self, constants.%FinalClass [template = constants.%FinalClass]
// CHECK:STDOUT: %self.loc20_24.1: %FinalClass = param self, runtime_param0
// CHECK:STDOUT: %self.loc20_24.2: %FinalClass = bind_name self, %self.loc20_24.1
// CHECK:STDOUT: }
// CHECK:STDOUT: %Virtual.decl: %Virtual.type = fn_decl @Virtual [template = constants.%Virtual] {
// CHECK:STDOUT: %Self.ref.loc29: type = name_ref Self, constants.%FinalClass [template = constants.%FinalClass]
// CHECK:STDOUT: %self.loc29_22.1: %FinalClass = param self, runtime_param0
// CHECK:STDOUT: %self.loc29_22.2: %FinalClass = bind_name self, %self.loc29_22.1
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%FinalClass
// CHECK:STDOUT: .Abstract = %Abstract.decl
// CHECK:STDOUT: .Virtual = %Virtual.decl
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @AbstractClass {
// CHECK:STDOUT: %Default.decl: %Default.type = fn_decl @Default [template = constants.%Default] {
// CHECK:STDOUT: %Self.ref.loc38: type = name_ref Self, constants.%AbstractClass [template = constants.%AbstractClass]
// CHECK:STDOUT: %self.loc38_22.1: %AbstractClass = param self, runtime_param0
// CHECK:STDOUT: %self.loc38_22.2: %AbstractClass = bind_name self, %self.loc38_22.1
// CHECK:STDOUT: }
// CHECK:STDOUT: %Final.decl: %Final.type = fn_decl @Final [template = constants.%Final] {
// CHECK:STDOUT: %Self.ref.loc44: type = name_ref Self, constants.%AbstractClass [template = constants.%AbstractClass]
// CHECK:STDOUT: %self.loc44_18.1: %AbstractClass = param self, runtime_param0
// CHECK:STDOUT: %self.loc44_18.2: %AbstractClass = bind_name self, %self.loc44_18.1
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%AbstractClass
// CHECK:STDOUT: .Default = %Default.decl
// CHECK:STDOUT: .Final = %Final.decl
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @BaseClass {
// CHECK:STDOUT: %Abstract.decl: %Abstract.type.2 = fn_decl @Abstract.2 [template = constants.%Abstract.2] {
// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%BaseClass [template = constants.%BaseClass]
// CHECK:STDOUT: %self.loc55_24.1: %BaseClass = param self, runtime_param0
// CHECK:STDOUT: %self.loc55_24.2: %BaseClass = bind_name self, %self.loc55_24.1
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%BaseClass
// CHECK:STDOUT: .Abstract = %Abstract.decl
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Abstract.1[@FinalClass.%self.loc20_24.2: %FinalClass]();
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Virtual[@FinalClass.%self.loc29_22.2: %FinalClass]();
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Default[@AbstractClass.%self.loc38_22.2: %AbstractClass]();
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Final[@AbstractClass.%self.loc44_18.2: %AbstractClass]();
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Abstract.2[@BaseClass.%self.loc55_24.2: %BaseClass]();
// CHECK:STDOUT: