mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Instead of hardcoding which types are copyable, add a `Core.Copy` interface to perform copying. Move almost all the current copy support to that interface. Some remaining pieces are still using builtin logic after this PR: * For tuples and structs, builtin logic is used to perform elementwise copies. This also supports copying *adapters of* tuples and structs, which seems like it may not be desirable, especially for non-extending adapters. A `Copy` impl is provided for tuples of at most 2 elements, so that `Core.Copy` constraints are satisfied, but we can't implement this generally until we have variadics support, and don't yet have a mechanism to generalize this to structs. * For `enum` types imported from C++, builtin logic is used to perform a copy. This is temporary until we have a mechanism to identify these types from an impl in the prelude. One lowering test in `toolchain/lower/testdata/class/generic.carbon` is disabled for now, as it causes a crash in the lowering code due to an ABI mismatch between the call signature in the lowered declaration of a specific function and the call that is generated in the specific callee. Fixing this is a little involved, and will be done in a separate PR. --------- Co-authored-by: Geoff Romer <gromer@google.com>
63 lines
2.5 KiB
Plaintext
63 lines
2.5 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
|
|
//
|
|
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_self.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_self.carbon
|
|
|
|
class Class {
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:8: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
|
|
// CHECK:STDERR: fn F(self: Self);
|
|
// CHECK:STDERR: ^~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
fn F(self: Self);
|
|
|
|
fn G() -> Self;
|
|
}
|
|
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:12: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
|
|
// CHECK:STDERR: fn Class.F(self: Self) {
|
|
// CHECK:STDERR: ^~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
fn Class.F(self: Self) {
|
|
}
|
|
|
|
fn Class.G() -> Self {
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:7: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
|
|
// CHECK:STDERR: var self: Self;
|
|
// CHECK:STDERR: ^~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
var self: Self;
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE+7]]:10: error: cannot copy value of type `Class` [CopyOfUncopyableType]
|
|
// CHECK:STDERR: return self;
|
|
// CHECK:STDERR: ^~~~
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:10: note: type `Class` does not implement interface `Core.Copy` [MissingImplInMemberAccessNote]
|
|
// CHECK:STDERR: return self;
|
|
// CHECK:STDERR: ^~~~
|
|
// CHECK:STDERR:
|
|
return self;
|
|
}
|
|
|
|
class WrongSelf {
|
|
fn F[self: Class]();
|
|
}
|
|
|
|
fn CallWrongSelf(ws: WrongSelf) {
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE+10]]:3: error: cannot implicitly convert expression of type `WrongSelf` to `Class` [ConversionFailure]
|
|
// CHECK:STDERR: ws.F();
|
|
// CHECK:STDERR: ^~
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE+7]]:3: note: type `WrongSelf` does not implement interface `Core.ImplicitAs(Class)` [MissingImplInMemberAccessNote]
|
|
// CHECK:STDERR: ws.F();
|
|
// CHECK:STDERR: ^~
|
|
// CHECK:STDERR: fail_self.carbon:[[@LINE-10]]:8: note: initializing function parameter [InCallToFunctionParam]
|
|
// CHECK:STDERR: fn F[self: Class]();
|
|
// CHECK:STDERR: ^~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
ws.F();
|
|
}
|