basics.carbon 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/alias/basics.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/alias/basics.carbon
  12. // --- alias.carbon
  13. library "[[@TEST_NAME]]";
  14. class C {};
  15. //@dump-sem-ir-begin
  16. alias c = C;
  17. //@dump-sem-ir-end
  18. let l: c = {};
  19. // --- alias_of_alias.carbon
  20. library "[[@TEST_NAME]]";
  21. class C {}
  22. //@dump-sem-ir-begin
  23. alias a = C;
  24. alias b = a;
  25. alias c = b;
  26. let d: c = {};
  27. //@dump-sem-ir-end
  28. // --- fail_control_flow.carbon
  29. library "[[@TEST_NAME]]";
  30. // CHECK:STDERR: fail_control_flow.carbon:[[@LINE+8]]:11: error: semantics TODO: `Control flow expressions are currently only supported inside functions.` [SemanticsTodo]
  31. // CHECK:STDERR: alias a = true or false;
  32. // CHECK:STDERR: ^~~~~~~
  33. // CHECK:STDERR:
  34. // CHECK:STDERR: fail_control_flow.carbon:[[@LINE+4]]:11: error: semantics TODO: `Control flow expressions are currently only supported inside functions.` [SemanticsTodo]
  35. // CHECK:STDERR: alias a = true or false;
  36. // CHECK:STDERR: ^~~~~~~~~~~~~
  37. // CHECK:STDERR:
  38. alias a = true or false;
  39. // --- fail_name_conflict.carbon
  40. library "[[@TEST_NAME]]";
  41. class C {}
  42. alias a = C;
  43. // CHECK:STDERR: fail_name_conflict.carbon:[[@LINE+7]]:5: error: duplicate name `a` being declared in the same scope [NameDeclDuplicate]
  44. // CHECK:STDERR: var a: C = {};
  45. // CHECK:STDERR: ^
  46. // CHECK:STDERR: fail_name_conflict.carbon:[[@LINE-4]]:7: note: name is previously declared here [NameDeclPrevious]
  47. // CHECK:STDERR: alias a = C;
  48. // CHECK:STDERR: ^
  49. // CHECK:STDERR:
  50. var a: C = {};
  51. var b: C = {};
  52. // CHECK:STDERR: fail_name_conflict.carbon:[[@LINE+7]]:7: error: duplicate name `b` being declared in the same scope [NameDeclDuplicate]
  53. // CHECK:STDERR: alias b = C;
  54. // CHECK:STDERR: ^
  55. // CHECK:STDERR: fail_name_conflict.carbon:[[@LINE-4]]:5: note: name is previously declared here [NameDeclPrevious]
  56. // CHECK:STDERR: var b: C = {};
  57. // CHECK:STDERR: ^
  58. // CHECK:STDERR:
  59. alias b = C;
  60. // --- fail_not_constant.carbon
  61. library "[[@TEST_NAME]]";
  62. var a: () = ();
  63. var b: ()* = &a;
  64. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE+4]]:11: error: alias initializer must be a name reference [AliasRequiresNameRef]
  65. // CHECK:STDERR: alias c = *b;
  66. // CHECK:STDERR: ^~
  67. // CHECK:STDERR:
  68. alias c = *b;
  69. // --- fail_params.carbon
  70. library "[[@TEST_NAME]]";
  71. // CHECK:STDERR: fail_params.carbon:[[@LINE+8]]:8: error: `alias` declaration cannot have parameters [UnexpectedDeclNameParams]
  72. // CHECK:STDERR: alias A(T:! type) = T*;
  73. // CHECK:STDERR: ^~~~~~~~~~
  74. // CHECK:STDERR:
  75. // CHECK:STDERR: fail_params.carbon:[[@LINE+4]]:21: error: alias initializer must be a name reference [AliasRequiresNameRef]
  76. // CHECK:STDERR: alias A(T:! type) = T*;
  77. // CHECK:STDERR: ^~
  78. // CHECK:STDERR:
  79. alias A(T:! type) = T*;
  80. // --- fail_modifiers.carbon
  81. library "[[@TEST_NAME]]";
  82. class Class {}
  83. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+25]]:10: error: `base` not allowed on declaration with `abstract` [ModifierNotAllowedWith]
  84. // CHECK:STDERR: abstract base default final alias A = Class;
  85. // CHECK:STDERR: ^~~~
  86. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+22]]:1: note: `abstract` previously appeared here [ModifierPrevious]
  87. // CHECK:STDERR: abstract base default final alias A = Class;
  88. // CHECK:STDERR: ^~~~~~~~
  89. // CHECK:STDERR:
  90. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+18]]:15: error: `default` not allowed on declaration with `abstract` [ModifierNotAllowedWith]
  91. // CHECK:STDERR: abstract base default final alias A = Class;
  92. // CHECK:STDERR: ^~~~~~~
  93. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+15]]:1: note: `abstract` previously appeared here [ModifierPrevious]
  94. // CHECK:STDERR: abstract base default final alias A = Class;
  95. // CHECK:STDERR: ^~~~~~~~
  96. // CHECK:STDERR:
  97. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+11]]:23: error: `final` not allowed on declaration with `abstract` [ModifierNotAllowedWith]
  98. // CHECK:STDERR: abstract base default final alias A = Class;
  99. // CHECK:STDERR: ^~~~~
  100. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+8]]:1: note: `abstract` previously appeared here [ModifierPrevious]
  101. // CHECK:STDERR: abstract base default final alias A = Class;
  102. // CHECK:STDERR: ^~~~~~~~
  103. // CHECK:STDERR:
  104. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+4]]:1: error: `abstract` not allowed on `alias` declaration [ModifierNotAllowedOnDeclaration]
  105. // CHECK:STDERR: abstract base default final alias A = Class;
  106. // CHECK:STDERR: ^~~~~~~~
  107. // CHECK:STDERR:
  108. abstract base default final alias A = Class;
  109. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+4]]:1: error: `impl` not allowed on `alias` declaration [ModifierNotAllowedOnDeclaration]
  110. // CHECK:STDERR: impl alias B = Class;
  111. // CHECK:STDERR: ^~~~
  112. // CHECK:STDERR:
  113. impl alias B = Class;
  114. // CHECK:STDERR: fail_modifiers.carbon:[[@LINE+4]]:1: error: `extern` not allowed on `alias` declaration [ModifierNotAllowedOnDeclaration]
  115. // CHECK:STDERR: extern alias C = Class;
  116. // CHECK:STDERR: ^~~~~~
  117. // CHECK:STDERR:
  118. extern alias C = Class;
  119. // CHECK:STDOUT: --- alias.carbon
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: constants {
  122. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: imports {
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: file {
  129. // CHECK:STDOUT: %C.ref: type = name_ref C, %C.decl [concrete = constants.%C]
  130. // CHECK:STDOUT: %c: type = alias_binding c, %C.decl [concrete = constants.%C]
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: fn @__global_init() {
  134. // CHECK:STDOUT: !entry:
  135. // CHECK:STDOUT: <elided>
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: --- alias_of_alias.carbon
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: constants {
  141. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  142. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  143. // CHECK:STDOUT: %pattern_type: type = pattern_type %C [concrete]
  144. // CHECK:STDOUT: %empty_struct: %empty_struct_type = struct_value () [concrete]
  145. // CHECK:STDOUT: %C.val: %C = struct_value () [concrete]
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: imports {
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: file {
  152. // CHECK:STDOUT: %C.ref: type = name_ref C, %C.decl [concrete = constants.%C]
  153. // CHECK:STDOUT: %a: type = alias_binding a, %C.decl [concrete = constants.%C]
  154. // CHECK:STDOUT: %a.ref: type = name_ref a, %a [concrete = constants.%C]
  155. // CHECK:STDOUT: %b: type = alias_binding b, %a [concrete = constants.%C]
  156. // CHECK:STDOUT: %b.ref: type = name_ref b, %b [concrete = constants.%C]
  157. // CHECK:STDOUT: %c: type = alias_binding c, %b [concrete = constants.%C]
  158. // CHECK:STDOUT: name_binding_decl {
  159. // CHECK:STDOUT: %d.patt: %pattern_type = value_binding_pattern d [concrete]
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: %c.ref: type = name_ref c, %c [concrete = constants.%C]
  162. // CHECK:STDOUT: %.loc10_13.1: ref %C = temporary_storage
  163. // CHECK:STDOUT: %.loc10_13.2: init %C = class_init (), %.loc10_13.1 [concrete = constants.%C.val]
  164. // CHECK:STDOUT: %.loc10_13.3: ref %C = temporary %.loc10_13.1, %.loc10_13.2
  165. // CHECK:STDOUT: %.loc10_13.4: ref %C = converted @__global_init.%.loc10, %.loc10_13.3
  166. // CHECK:STDOUT: %.loc10_13.5: %C = acquire_value %.loc10_13.4
  167. // CHECK:STDOUT: %d: %C = value_binding d, %.loc10_13.5
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: fn @__global_init() {
  171. // CHECK:STDOUT: !entry:
  172. // CHECK:STDOUT: %.loc10: %empty_struct_type = struct_literal () [concrete = constants.%empty_struct]
  173. // CHECK:STDOUT: <elided>
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: