fail_method.carbon 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_method.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_method.carbon
  10. class Class {
  11. fn NoSelf();
  12. fn WithSelf[self: Class]();
  13. }
  14. alias A = Class.WithSelf;
  15. fn F(c: Class) {
  16. c.NoSelf();
  17. c.WithSelf();
  18. Class.NoSelf();
  19. // CHECK:STDERR: fail_method.carbon:[[@LINE+7]]:3: error(MissingObjectInMethodCall): missing object argument in method call
  20. // CHECK:STDERR: Class.WithSelf();
  21. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  22. // CHECK:STDERR: fail_method.carbon:[[@LINE-13]]:3: note(InCallToFunction): calling function declared here
  23. // CHECK:STDERR: fn WithSelf[self: Class]();
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. Class.WithSelf();
  27. // CHECK:STDERR: fail_method.carbon:[[@LINE+7]]:3: error(CallArgCountMismatch): 1 argument passed to function expecting 0 arguments
  28. // CHECK:STDERR: Class.WithSelf(c);
  29. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  30. // CHECK:STDERR: fail_method.carbon:[[@LINE-21]]:3: note(InCallToEntity): calling function declared here
  31. // CHECK:STDERR: fn WithSelf[self: Class]();
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. // CHECK:STDERR:
  34. Class.WithSelf(c);
  35. // CHECK:STDERR: fail_method.carbon:[[@LINE+6]]:3: error(MissingObjectInMethodCall): missing object argument in method call
  36. // CHECK:STDERR: A();
  37. // CHECK:STDERR: ^~
  38. // CHECK:STDERR: fail_method.carbon:[[@LINE-30]]:3: note(InCallToFunction): calling function declared here
  39. // CHECK:STDERR: fn WithSelf[self: Class]();
  40. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. A();
  42. }
  43. // CHECK:STDOUT: --- fail_method.carbon
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: constants {
  46. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  47. // CHECK:STDOUT: %NoSelf.type: type = fn_type @NoSelf [template]
  48. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  49. // CHECK:STDOUT: %NoSelf: %NoSelf.type = struct_value () [template]
  50. // CHECK:STDOUT: %WithSelf.type: type = fn_type @WithSelf [template]
  51. // CHECK:STDOUT: %WithSelf: %WithSelf.type = struct_value () [template]
  52. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  53. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  54. // CHECK:STDOUT: %.4: type = ptr_type %.2 [template]
  55. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  56. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: imports {
  60. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  61. // CHECK:STDOUT: import Core//prelude
  62. // CHECK:STDOUT: import Core//prelude/operators
  63. // CHECK:STDOUT: import Core//prelude/types
  64. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  65. // CHECK:STDOUT: import Core//prelude/operators/as
  66. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  67. // CHECK:STDOUT: import Core//prelude/operators/comparison
  68. // CHECK:STDOUT: import Core//prelude/types/bool
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: file {
  73. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  74. // CHECK:STDOUT: .Core = imports.%Core
  75. // CHECK:STDOUT: .Class = %Class.decl
  76. // CHECK:STDOUT: .A = %A
  77. // CHECK:STDOUT: .F = %F.decl
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %Core.import = import Core
  80. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  81. // CHECK:STDOUT: %Class.ref: type = name_ref Class, %Class.decl [template = constants.%Class]
  82. // CHECK:STDOUT: %WithSelf.ref: %WithSelf.type = name_ref WithSelf, @Class.%WithSelf.decl [template = constants.%WithSelf]
  83. // CHECK:STDOUT: %A: %WithSelf.type = bind_alias A, @Class.%WithSelf.decl [template = constants.%WithSelf]
  84. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  85. // CHECK:STDOUT: %c.patt: %Class = binding_pattern c
  86. // CHECK:STDOUT: %c.param_patt: %Class = param_pattern %c.patt, runtime_param0
  87. // CHECK:STDOUT: } {
  88. // CHECK:STDOUT: %Class.ref.loc18: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  89. // CHECK:STDOUT: %param: %Class = param runtime_param0
  90. // CHECK:STDOUT: %c: %Class = bind_name c, %param
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: class @Class {
  95. // CHECK:STDOUT: %NoSelf.decl: %NoSelf.type = fn_decl @NoSelf [template = constants.%NoSelf] {} {}
  96. // CHECK:STDOUT: %WithSelf.decl: %WithSelf.type = fn_decl @WithSelf [template = constants.%WithSelf] {
  97. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  98. // CHECK:STDOUT: %self.param_patt: %Class = param_pattern %self.patt, runtime_param0
  99. // CHECK:STDOUT: } {
  100. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  101. // CHECK:STDOUT: %param: %Class = param runtime_param0
  102. // CHECK:STDOUT: %self: %Class = bind_name self, %param
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.2 [template = constants.%.3]
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: !members:
  107. // CHECK:STDOUT: .Self = constants.%Class
  108. // CHECK:STDOUT: .NoSelf = %NoSelf.decl
  109. // CHECK:STDOUT: .WithSelf = %WithSelf.decl
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: fn @NoSelf();
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @WithSelf[%self.param_patt: %Class]();
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @F(%c.param_patt: %Class) {
  117. // CHECK:STDOUT: !entry:
  118. // CHECK:STDOUT: %c.ref.loc19: %Class = name_ref c, %c
  119. // CHECK:STDOUT: %NoSelf.ref.loc19: %NoSelf.type = name_ref NoSelf, @Class.%NoSelf.decl [template = constants.%NoSelf]
  120. // CHECK:STDOUT: %NoSelf.call.loc19: init %.1 = call %NoSelf.ref.loc19()
  121. // CHECK:STDOUT: %c.ref.loc20: %Class = name_ref c, %c
  122. // CHECK:STDOUT: %WithSelf.ref.loc20: %WithSelf.type = name_ref WithSelf, @Class.%WithSelf.decl [template = constants.%WithSelf]
  123. // CHECK:STDOUT: %.loc20: <bound method> = bound_method %c.ref.loc20, %WithSelf.ref.loc20
  124. // CHECK:STDOUT: %WithSelf.call.loc20: init %.1 = call %.loc20(%c.ref.loc20)
  125. // CHECK:STDOUT: %Class.ref.loc22: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  126. // CHECK:STDOUT: %NoSelf.ref.loc22: %NoSelf.type = name_ref NoSelf, @Class.%NoSelf.decl [template = constants.%NoSelf]
  127. // CHECK:STDOUT: %NoSelf.call.loc22: init %.1 = call %NoSelf.ref.loc22()
  128. // CHECK:STDOUT: %Class.ref.loc30: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  129. // CHECK:STDOUT: %WithSelf.ref.loc30: %WithSelf.type = name_ref WithSelf, @Class.%WithSelf.decl [template = constants.%WithSelf]
  130. // CHECK:STDOUT: %WithSelf.call.loc30: init %.1 = call %WithSelf.ref.loc30(<invalid>) [template = <error>]
  131. // CHECK:STDOUT: %Class.ref.loc38: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  132. // CHECK:STDOUT: %WithSelf.ref.loc38: %WithSelf.type = name_ref WithSelf, @Class.%WithSelf.decl [template = constants.%WithSelf]
  133. // CHECK:STDOUT: %c.ref.loc38: %Class = name_ref c, %c
  134. // CHECK:STDOUT: %A.ref: %WithSelf.type = name_ref A, file.%A [template = constants.%WithSelf]
  135. // CHECK:STDOUT: %WithSelf.call.loc46: init %.1 = call %A.ref(<invalid>) [template = <error>]
  136. // CHECK:STDOUT: return
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: