mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 22:02:45 +01:00
Rather than just adding `Self` to the lexical scope, add it to the class's name scope so that it is visible in later lexical scopes for the same class -- in particular, for out-of-line definitions of members. Also switch some tests in `check/testdata/class` over to making idiomatic use of `Self` both inside a class and out-of-line, now that it works more consistently. Note that this does not permit using `Class.Self`, but only because we don't yet support keyword names after `.` at all. If that changed, one could use `Class.Self` to redundantly refer to `Class`. Whether we allow that is left to a future decision.
128 lines
6.0 KiB
Plaintext
128 lines
6.0 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
|
|
|
|
class FinalClass {
|
|
|
|
// 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: class FinalClass {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
|
|
abstract fn Abstract[self: Self]();
|
|
|
|
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+6]]: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-13]]:1: Containing definition here.
|
|
// CHECK:STDERR: class FinalClass {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
|
|
virtual fn Virtual[self: Self]();
|
|
}
|
|
|
|
abstract class AbstractClass {
|
|
|
|
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+3]]:3: ERROR: `default` not allowed on `fn` declaration outside of an interface.
|
|
// CHECK:STDERR: default fn Default[self: Self]();
|
|
// CHECK:STDERR: ^~~~~~~
|
|
default fn Default[self: Self]();
|
|
|
|
// CHECK:STDERR: fail_method_modifiers.carbon:[[@LINE+3]]:3: ERROR: `final` not allowed on `fn` declaration outside of an interface.
|
|
// CHECK:STDERR: final fn Final[self: Self]();
|
|
// 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: %.1: type = struct_type {} [template]
|
|
// CHECK:STDOUT: %AbstractClass: type = class_type @AbstractClass [template]
|
|
// CHECK:STDOUT: %BaseClass: type = class_type @BaseClass [template]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace {
|
|
// CHECK:STDOUT: .FinalClass = %FinalClass.decl
|
|
// CHECK:STDOUT: .AbstractClass = %AbstractClass.decl
|
|
// CHECK:STDOUT: .BaseClass = %BaseClass.decl
|
|
// CHECK:STDOUT: } [template]
|
|
// CHECK:STDOUT: %FinalClass.decl = class_decl @FinalClass {} [template = constants.%FinalClass]
|
|
// CHECK:STDOUT: %AbstractClass.decl = class_decl @AbstractClass {} [template = constants.%AbstractClass]
|
|
// CHECK:STDOUT: %BaseClass.decl = class_decl @BaseClass {} [template = constants.%BaseClass]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: class @FinalClass {
|
|
// CHECK:STDOUT: %Abstract: <function> = fn_decl @Abstract.1 {
|
|
// CHECK:STDOUT: %Self.ref.loc15: type = name_ref Self, constants.%FinalClass [template = constants.%FinalClass]
|
|
// CHECK:STDOUT: %self.loc15_24.1: FinalClass = param self
|
|
// CHECK:STDOUT: %self.loc15_24.2: FinalClass = bind_name self, %self.loc15_24.1
|
|
// CHECK:STDOUT: } [template]
|
|
// CHECK:STDOUT: %Virtual: <function> = fn_decl @Virtual {
|
|
// CHECK:STDOUT: %Self.ref.loc23: type = name_ref Self, constants.%FinalClass [template = constants.%FinalClass]
|
|
// CHECK:STDOUT: %self.loc23_22.1: FinalClass = param self
|
|
// CHECK:STDOUT: %self.loc23_22.2: FinalClass = bind_name self, %self.loc23_22.1
|
|
// CHECK:STDOUT: } [template]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !members:
|
|
// CHECK:STDOUT: .Self = constants.%FinalClass
|
|
// CHECK:STDOUT: .Abstract = %Abstract
|
|
// CHECK:STDOUT: .Virtual = %Virtual
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: class @AbstractClass {
|
|
// CHECK:STDOUT: %Default: <function> = fn_decl @Default {
|
|
// CHECK:STDOUT: %Self.ref.loc31: type = name_ref Self, constants.%AbstractClass [template = constants.%AbstractClass]
|
|
// CHECK:STDOUT: %self.loc31_22.1: AbstractClass = param self
|
|
// CHECK:STDOUT: %self.loc31_22.2: AbstractClass = bind_name self, %self.loc31_22.1
|
|
// CHECK:STDOUT: } [template]
|
|
// CHECK:STDOUT: %Final: <function> = fn_decl @Final {
|
|
// CHECK:STDOUT: %Self.ref.loc36: type = name_ref Self, constants.%AbstractClass [template = constants.%AbstractClass]
|
|
// CHECK:STDOUT: %self.loc36_18.1: AbstractClass = param self
|
|
// CHECK:STDOUT: %self.loc36_18.2: AbstractClass = bind_name self, %self.loc36_18.1
|
|
// CHECK:STDOUT: } [template]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !members:
|
|
// CHECK:STDOUT: .Self = constants.%AbstractClass
|
|
// CHECK:STDOUT: .Default = %Default
|
|
// CHECK:STDOUT: .Final = %Final
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: class @BaseClass {
|
|
// CHECK:STDOUT: %Abstract: <function> = fn_decl @Abstract.2 {
|
|
// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%BaseClass [template = constants.%BaseClass]
|
|
// CHECK:STDOUT: %self.loc47_24.1: BaseClass = param self
|
|
// CHECK:STDOUT: %self.loc47_24.2: BaseClass = bind_name self, %self.loc47_24.1
|
|
// CHECK:STDOUT: } [template]
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !members:
|
|
// CHECK:STDOUT: .Self = constants.%BaseClass
|
|
// CHECK:STDOUT: .Abstract = %Abstract
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @Abstract.1[@FinalClass.%self.loc15_24.2: FinalClass]();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @Virtual[@FinalClass.%self.loc23_22.2: FinalClass]();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @Default[@AbstractClass.%self.loc31_22.2: AbstractClass]();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @Final[@AbstractClass.%self.loc36_18.2: AbstractClass]();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @Abstract.2[@BaseClass.%self.loc47_24.2: BaseClass]();
|
|
// CHECK:STDOUT:
|