fail_expr_category.carbon 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/index/fail_expr_category.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_expr_category.carbon
  10. fn F() -> [i32; 3];
  11. fn G(b: [i32; 3]) {
  12. // Indexing an array value gives a value.
  13. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+4]]:18: error: cannot take the address of non-reference expression
  14. // CHECK:STDERR: var pb: i32* = &b[0];
  15. // CHECK:STDERR: ^
  16. // CHECK:STDERR:
  17. var pb: i32* = &b[0];
  18. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+4]]:3: error: expression is not assignable
  19. // CHECK:STDERR: b[0] = 4;
  20. // CHECK:STDERR: ^~~~
  21. // CHECK:STDERR:
  22. b[0] = 4;
  23. // Indexing an ephemeral reference (materialized from an initializing
  24. // expression) gives a value.
  25. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+4]]:18: error: cannot take the address of non-reference expression
  26. // CHECK:STDERR: var pf: i32* = &F()[0];
  27. // CHECK:STDERR: ^
  28. // CHECK:STDERR:
  29. var pf: i32* = &F()[0];
  30. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+3]]:3: error: expression is not assignable
  31. // CHECK:STDERR: F()[0] = 4;
  32. // CHECK:STDERR: ^~~~~~
  33. F()[0] = 4;
  34. }
  35. // CHECK:STDOUT: --- fail_expr_category.carbon
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: constants {
  38. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  39. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  40. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  41. // CHECK:STDOUT: %.2: i32 = int_literal 3 [template]
  42. // CHECK:STDOUT: %.3: type = array_type %.2, i32 [template]
  43. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  44. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  45. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  46. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  47. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  48. // CHECK:STDOUT: %.5: type = ptr_type i32 [template]
  49. // CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
  50. // CHECK:STDOUT: %.7: i32 = int_literal 4 [template]
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: imports {
  54. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  55. // CHECK:STDOUT: .Int32 = %import_ref
  56. // CHECK:STDOUT: import Core//prelude
  57. // CHECK:STDOUT: import Core//prelude/operators
  58. // CHECK:STDOUT: import Core//prelude/types
  59. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  60. // CHECK:STDOUT: import Core//prelude/operators/as
  61. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  62. // CHECK:STDOUT: import Core//prelude/operators/comparison
  63. // CHECK:STDOUT: import Core//prelude/types/bool
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: file {
  69. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  70. // CHECK:STDOUT: .Core = imports.%Core
  71. // CHECK:STDOUT: .F = %F.decl
  72. // CHECK:STDOUT: .G = %G.decl
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %Core.import = import Core
  75. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {
  76. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  77. // CHECK:STDOUT: %.loc11_17: i32 = int_literal 3 [template = constants.%.2]
  78. // CHECK:STDOUT: %.loc11_12.1: type = value_of_initializer %int.make_type_32 [template = i32]
  79. // CHECK:STDOUT: %.loc11_12.2: type = converted %int.make_type_32, %.loc11_12.1 [template = i32]
  80. // CHECK:STDOUT: %.loc11_18: type = array_type %.loc11_17, i32 [template = constants.%.3]
  81. // CHECK:STDOUT: %return: ref %.3 = var <return slot>
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  84. // CHECK:STDOUT: %b.patt: %.3 = binding_pattern b
  85. // CHECK:STDOUT: } {
  86. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  87. // CHECK:STDOUT: %.loc13_15: i32 = int_literal 3 [template = constants.%.2]
  88. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  89. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  90. // CHECK:STDOUT: %.loc13_16: type = array_type %.loc13_15, i32 [template = constants.%.3]
  91. // CHECK:STDOUT: %b.param: %.3 = param b, runtime_param0
  92. // CHECK:STDOUT: %b: %.3 = bind_name b, %b.param
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn @F() -> %.3;
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: fn @G(%b: %.3) {
  101. // CHECK:STDOUT: !entry:
  102. // CHECK:STDOUT: %int.make_type_32.loc19: init type = call constants.%Int32() [template = i32]
  103. // CHECK:STDOUT: %.loc19_14.1: type = value_of_initializer %int.make_type_32.loc19 [template = i32]
  104. // CHECK:STDOUT: %.loc19_14.2: type = converted %int.make_type_32.loc19, %.loc19_14.1 [template = i32]
  105. // CHECK:STDOUT: %.loc19_14.3: type = ptr_type i32 [template = constants.%.5]
  106. // CHECK:STDOUT: %pb.var: ref %.5 = var pb
  107. // CHECK:STDOUT: %pb: ref %.5 = bind_name pb, %pb.var
  108. // CHECK:STDOUT: %b.ref.loc19: %.3 = name_ref b, %b
  109. // CHECK:STDOUT: %.loc19_21: i32 = int_literal 0 [template = constants.%.6]
  110. // CHECK:STDOUT: %.loc19_22.1: ref %.3 = value_as_ref %b.ref.loc19
  111. // CHECK:STDOUT: %.loc19_22.2: ref i32 = array_index %.loc19_22.1, %.loc19_21
  112. // CHECK:STDOUT: %.loc19_22.3: i32 = bind_value %.loc19_22.2
  113. // CHECK:STDOUT: %.loc19_18: %.5 = addr_of <error> [template = <error>]
  114. // CHECK:STDOUT: assign %pb.var, %.loc19_18
  115. // CHECK:STDOUT: %b.ref.loc24: %.3 = name_ref b, %b
  116. // CHECK:STDOUT: %.loc24_5: i32 = int_literal 0 [template = constants.%.6]
  117. // CHECK:STDOUT: %.loc24_6.1: ref %.3 = value_as_ref %b.ref.loc24
  118. // CHECK:STDOUT: %.loc24_6.2: ref i32 = array_index %.loc24_6.1, %.loc24_5
  119. // CHECK:STDOUT: %.loc24_6.3: i32 = bind_value %.loc24_6.2
  120. // CHECK:STDOUT: %.loc24_10: i32 = int_literal 4 [template = constants.%.7]
  121. // CHECK:STDOUT: assign %.loc24_6.3, %.loc24_10
  122. // CHECK:STDOUT: %int.make_type_32.loc32: init type = call constants.%Int32() [template = i32]
  123. // CHECK:STDOUT: %.loc32_14.1: type = value_of_initializer %int.make_type_32.loc32 [template = i32]
  124. // CHECK:STDOUT: %.loc32_14.2: type = converted %int.make_type_32.loc32, %.loc32_14.1 [template = i32]
  125. // CHECK:STDOUT: %.loc32_14.3: type = ptr_type i32 [template = constants.%.5]
  126. // CHECK:STDOUT: %pf.var: ref %.5 = var pf
  127. // CHECK:STDOUT: %pf: ref %.5 = bind_name pf, %pf.var
  128. // CHECK:STDOUT: %F.ref.loc32: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  129. // CHECK:STDOUT: %.loc32_20.1: ref %.3 = temporary_storage
  130. // CHECK:STDOUT: %F.call.loc32: init %.3 = call %F.ref.loc32() to %.loc32_20.1
  131. // CHECK:STDOUT: %.loc32_23: i32 = int_literal 0 [template = constants.%.6]
  132. // CHECK:STDOUT: %.loc32_20.2: ref %.3 = temporary %.loc32_20.1, %F.call.loc32
  133. // CHECK:STDOUT: %.loc32_24.1: ref i32 = array_index %.loc32_20.2, %.loc32_23
  134. // CHECK:STDOUT: %.loc32_24.2: i32 = bind_value %.loc32_24.1
  135. // CHECK:STDOUT: %.loc32_18: %.5 = addr_of <error> [template = <error>]
  136. // CHECK:STDOUT: assign %pf.var, %.loc32_18
  137. // CHECK:STDOUT: %F.ref.loc36: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  138. // CHECK:STDOUT: %.loc36_4.1: ref %.3 = temporary_storage
  139. // CHECK:STDOUT: %F.call.loc36: init %.3 = call %F.ref.loc36() to %.loc36_4.1
  140. // CHECK:STDOUT: %.loc36_7: i32 = int_literal 0 [template = constants.%.6]
  141. // CHECK:STDOUT: %.loc36_4.2: ref %.3 = temporary %.loc36_4.1, %F.call.loc36
  142. // CHECK:STDOUT: %.loc36_8.1: ref i32 = array_index %.loc36_4.2, %.loc36_7
  143. // CHECK:STDOUT: %.loc36_8.2: i32 = bind_value %.loc36_8.1
  144. // CHECK:STDOUT: %.loc36_12: i32 = int_literal 4 [template = constants.%.7]
  145. // CHECK:STDOUT: assign %.loc36_8.2, %.loc36_12
  146. // CHECK:STDOUT: return
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: