extern.carbon 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/function/declaration/no_prelude/extern.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/declaration/no_prelude/extern.carbon
  10. // --- basic.carbon
  11. library "[[@TEST_NAME]]";
  12. extern fn F();
  13. // --- basic_use.carbon
  14. import library "basic";
  15. var x: () = F();
  16. // --- fail_redecl.carbon
  17. library "[[@TEST_NAME]]";
  18. extern fn F();
  19. // CHECK:STDERR: fail_redecl.carbon:[[@LINE+7]]:1: error: redeclaration of `fn F` is redundant
  20. // CHECK:STDERR: extern fn F();
  21. // CHECK:STDERR: ^~~~~~~~~~~~~~
  22. // CHECK:STDERR: fail_redecl.carbon:[[@LINE-4]]:1: note: previously declared here
  23. // CHECK:STDERR: extern fn F();
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. extern fn F();
  27. // --- fail_redecl_extern.carbon
  28. library "[[@TEST_NAME]]";
  29. extern fn F();
  30. // CHECK:STDERR: fail_redecl_extern.carbon:[[@LINE+7]]:1: error: redeclaration of `fn F` is redundant
  31. // CHECK:STDERR: fn F();
  32. // CHECK:STDERR: ^~~~~~~
  33. // CHECK:STDERR: fail_redecl_extern.carbon:[[@LINE-4]]:1: note: previously declared here
  34. // CHECK:STDERR: extern fn F();
  35. // CHECK:STDERR: ^~~~~~~~~~~~~~
  36. // CHECK:STDERR:
  37. fn F();
  38. // --- fail_member_extern.carbon
  39. library "[[@TEST_NAME]]";
  40. class C {
  41. // CHECK:STDERR: fail_member_extern.carbon:[[@LINE+4]]:3: error: `extern` not allowed on `fn` declaration that is a member
  42. // CHECK:STDERR: extern fn F();
  43. // CHECK:STDERR: ^~~~~~
  44. // CHECK:STDERR:
  45. extern fn F();
  46. // CHECK:STDERR: fail_member_extern.carbon:[[@LINE+3]]:3: error: `extern` not allowed on `fn` declaration that is a member
  47. // CHECK:STDERR: extern fn G[self: Self]();
  48. // CHECK:STDERR: ^~~~~~
  49. extern fn G[self: Self]();
  50. }
  51. // CHECK:STDOUT: --- basic.carbon
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: constants {
  54. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  55. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  56. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  61. // CHECK:STDOUT: .F = %F.decl
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: extern fn @F();
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: --- basic_use.carbon
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: constants {
  71. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  72. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  73. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: imports {
  77. // CHECK:STDOUT: %import_ref: %F.type = import_ref Main//basic, inst+1, loaded [template = constants.%F]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: file {
  81. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  82. // CHECK:STDOUT: .F = imports.%import_ref
  83. // CHECK:STDOUT: .x = %x
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %default.import = import <invalid>
  86. // CHECK:STDOUT: %.loc4_9.1: %.1 = tuple_literal ()
  87. // CHECK:STDOUT: %.loc4_9.2: type = converted %.loc4_9.1, constants.%.1 [template = constants.%.1]
  88. // CHECK:STDOUT: %x.var: ref %.1 = var x
  89. // CHECK:STDOUT: %x: ref %.1 = bind_name x, %x.var
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: extern fn @F();
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: fn @__global_init() {
  95. // CHECK:STDOUT: !entry:
  96. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, imports.%import_ref [template = constants.%F]
  97. // CHECK:STDOUT: %F.call: init %.1 = call %F.ref()
  98. // CHECK:STDOUT: assign file.%x.var, %F.call
  99. // CHECK:STDOUT: return
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: --- fail_redecl.carbon
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: constants {
  105. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  106. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  107. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: file {
  111. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  112. // CHECK:STDOUT: .F = %F.decl.loc4
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %F.decl.loc4: %F.type = fn_decl @F [template = constants.%F] {} {}
  115. // CHECK:STDOUT: %F.decl.loc12: %F.type = fn_decl @F [template = constants.%F] {} {}
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: extern fn @F();
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: --- fail_redecl_extern.carbon
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: constants {
  123. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  124. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  125. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: file {
  129. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  130. // CHECK:STDOUT: .F = %F.decl.loc4
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %F.decl.loc4: %F.type = fn_decl @F [template = constants.%F] {} {}
  133. // CHECK:STDOUT: %F.decl.loc12: %F.type = fn_decl @F [template = constants.%F] {} {}
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: extern fn @F();
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: --- fail_member_extern.carbon
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: constants {
  141. // CHECK:STDOUT: %C: type = class_type @C [template]
  142. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  143. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  144. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  145. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  146. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  147. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  148. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: file {
  152. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  153. // CHECK:STDOUT: .C = %C.decl
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: class @C {
  159. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  160. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  161. // CHECK:STDOUT: %self.patt: %C = binding_pattern self
  162. // CHECK:STDOUT: } {
  163. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  164. // CHECK:STDOUT: %self.param: %C = param self, runtime_param0
  165. // CHECK:STDOUT: %self: %C = bind_name self, %self.param
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.2 [template = constants.%.3]
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: !members:
  170. // CHECK:STDOUT: .Self = constants.%C
  171. // CHECK:STDOUT: .F = %F.decl
  172. // CHECK:STDOUT: .G = %G.decl
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: fn @F();
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: fn @G[%self: %C]();
  178. // CHECK:STDOUT: