fail_self.carbon 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_self.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_self.carbon
  12. class Class {
  13. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:8: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
  14. // CHECK:STDERR: fn F(self: Self);
  15. // CHECK:STDERR: ^~~~~~~~~~
  16. // CHECK:STDERR:
  17. fn F(self: Self);
  18. fn G() -> Self;
  19. }
  20. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:19: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
  21. // CHECK:STDERR: fn Class.F(unused self: Self) {
  22. // CHECK:STDERR: ^~~~~~~~~~
  23. // CHECK:STDERR:
  24. fn Class.F(unused self: Self) {
  25. }
  26. fn Class.G() -> Self {
  27. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:7: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
  28. // CHECK:STDERR: var self: Self;
  29. // CHECK:STDERR: ^~~~~~~~~~
  30. // CHECK:STDERR:
  31. var self: Self;
  32. // CHECK:STDERR: fail_self.carbon:[[@LINE+7]]:10: error: cannot copy value of type `Class` [CopyOfUncopyableType]
  33. // CHECK:STDERR: return self;
  34. // CHECK:STDERR: ^~~~
  35. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:10: note: type `Class` does not implement interface `Core.Copy` [MissingImplInMemberAccessNote]
  36. // CHECK:STDERR: return self;
  37. // CHECK:STDERR: ^~~~
  38. // CHECK:STDERR:
  39. return self;
  40. }
  41. class WrongSelf {
  42. fn F[self: Class]();
  43. }
  44. fn CallWrongSelf(ws: WrongSelf) {
  45. // CHECK:STDERR: fail_self.carbon:[[@LINE+10]]:3: error: cannot implicitly convert expression of type `WrongSelf` to `Class` [ConversionFailure]
  46. // CHECK:STDERR: ws.F();
  47. // CHECK:STDERR: ^~
  48. // CHECK:STDERR: fail_self.carbon:[[@LINE+7]]:3: note: type `WrongSelf` does not implement interface `Core.ImplicitAs(Class)` [MissingImplInMemberAccessNote]
  49. // CHECK:STDERR: ws.F();
  50. // CHECK:STDERR: ^~
  51. // CHECK:STDERR: fail_self.carbon:[[@LINE-10]]:8: note: initializing function parameter [InCallToFunctionParam]
  52. // CHECK:STDERR: fn F[self: Class]();
  53. // CHECK:STDERR: ^~~~~~~~~~~
  54. // CHECK:STDERR:
  55. ws.F();
  56. }