fail_invalid_base.carbon 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_invalid_base.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_invalid_base.carbon
  10. namespace N;
  11. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: expression cannot be used as a value [UseOfNonExprAsValue]
  12. // CHECK:STDERR: var a: i32 = N[0];
  13. // CHECK:STDERR: ^
  14. // CHECK:STDERR:
  15. var a: i32 = N[0];
  16. fn F();
  17. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: expression cannot be used as a value [UseOfNonExprAsValue]
  18. // CHECK:STDERR: var b: i32 = F[1];
  19. // CHECK:STDERR: ^
  20. // CHECK:STDERR:
  21. var b: i32 = F[1];
  22. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: type `{.a: Core.IntLiteral, .b: Core.IntLiteral}` does not support indexing [TypeNotIndexable]
  23. // CHECK:STDERR: var c: i32 = {.a = 1, .b = 2}[0];
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. var c: i32 = {.a = 1, .b = 2}[0];
  27. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: type `type` does not support indexing [TypeNotIndexable]
  28. // CHECK:STDERR: var d: i32 = {.a: i32, .b: i32}[0];
  29. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  30. // CHECK:STDERR:
  31. var d: i32 = {.a: i32, .b: i32}[0];
  32. // CHECK:STDOUT: --- fail_invalid_base.carbon
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: constants {
  35. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  36. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  37. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  38. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  39. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  40. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  41. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  42. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  43. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  44. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  45. // CHECK:STDOUT: %struct_type.a.b.cfd: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [concrete]
  46. // CHECK:STDOUT: %struct: %struct_type.a.b.cfd = struct_value (%int_1, %int_2) [concrete]
  47. // CHECK:STDOUT: %IndexWith.type.504: type = generic_interface_type @IndexWith [concrete]
  48. // CHECK:STDOUT: %IndexWith.generic: %IndexWith.type.504 = struct_value () [concrete]
  49. // CHECK:STDOUT: %struct_type.a.b.501: type = struct_type {.a: %i32, .b: %i32} [concrete]
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: imports {
  53. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  54. // CHECK:STDOUT: .Int = %Core.Int
  55. // CHECK:STDOUT: .IndexWith = %Core.IndexWith
  56. // CHECK:STDOUT: import Core//prelude
  57. // CHECK:STDOUT: import Core//prelude/...
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  60. // CHECK:STDOUT: %Core.IndexWith: %IndexWith.type.504 = import_ref Core//prelude/operators/index, IndexWith, loaded [concrete = constants.%IndexWith.generic]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  65. // CHECK:STDOUT: .Core = imports.%Core
  66. // CHECK:STDOUT: .N = %N
  67. // CHECK:STDOUT: .a = %a
  68. // CHECK:STDOUT: .F = %F.decl
  69. // CHECK:STDOUT: .b = %b
  70. // CHECK:STDOUT: .c = %c
  71. // CHECK:STDOUT: .d = %d
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %Core.import = import Core
  74. // CHECK:STDOUT: %N: <namespace> = namespace [concrete] {}
  75. // CHECK:STDOUT: name_binding_decl {
  76. // CHECK:STDOUT: %a.patt: %pattern_type.7ce = binding_pattern a [concrete]
  77. // CHECK:STDOUT: %.loc16_1: %pattern_type.7ce = var_pattern %a.patt [concrete]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %a.var: ref %i32 = var a
  80. // CHECK:STDOUT: %.loc16_8: type = splice_block %i32.loc16 [concrete = constants.%i32] {
  81. // CHECK:STDOUT: %int_32.loc16: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  82. // CHECK:STDOUT: %i32.loc16: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %a: ref %i32 = bind_name a, %a.var
  85. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  86. // CHECK:STDOUT: name_binding_decl {
  87. // CHECK:STDOUT: %b.patt: %pattern_type.7ce = binding_pattern b [concrete]
  88. // CHECK:STDOUT: %.loc23_1: %pattern_type.7ce = var_pattern %b.patt [concrete]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %b.var: ref %i32 = var b
  91. // CHECK:STDOUT: %.loc23_8: type = splice_block %i32.loc23 [concrete = constants.%i32] {
  92. // CHECK:STDOUT: %int_32.loc23: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  93. // CHECK:STDOUT: %i32.loc23: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %b: ref %i32 = bind_name b, %b.var
  96. // CHECK:STDOUT: name_binding_decl {
  97. // CHECK:STDOUT: %c.patt: %pattern_type.7ce = binding_pattern c [concrete]
  98. // CHECK:STDOUT: %.loc29_1: %pattern_type.7ce = var_pattern %c.patt [concrete]
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %c.var: ref %i32 = var c
  101. // CHECK:STDOUT: %.loc29_8: type = splice_block %i32.loc29 [concrete = constants.%i32] {
  102. // CHECK:STDOUT: %int_32.loc29: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  103. // CHECK:STDOUT: %i32.loc29: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %c: ref %i32 = bind_name c, %c.var
  106. // CHECK:STDOUT: name_binding_decl {
  107. // CHECK:STDOUT: %d.patt: %pattern_type.7ce = binding_pattern d [concrete]
  108. // CHECK:STDOUT: %.loc35_1: %pattern_type.7ce = var_pattern %d.patt [concrete]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %d.var: ref %i32 = var d
  111. // CHECK:STDOUT: %.loc35_8: type = splice_block %i32.loc35 [concrete = constants.%i32] {
  112. // CHECK:STDOUT: %int_32.loc35: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  113. // CHECK:STDOUT: %i32.loc35: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %d: ref %i32 = bind_name d, %d.var
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: fn @F();
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: fn @__global_init() {
  121. // CHECK:STDOUT: !entry:
  122. // CHECK:STDOUT: %N.ref: <namespace> = name_ref N, file.%N [concrete = file.%N]
  123. // CHECK:STDOUT: %int_0.loc16: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  124. // CHECK:STDOUT: assign file.%a.var, <error>
  125. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  126. // CHECK:STDOUT: %int_1.loc23: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  127. // CHECK:STDOUT: assign file.%b.var, <error>
  128. // CHECK:STDOUT: %int_1.loc29: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  129. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  130. // CHECK:STDOUT: %.loc29_29.1: %struct_type.a.b.cfd = struct_literal (%int_1.loc29, %int_2)
  131. // CHECK:STDOUT: %int_0.loc29: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  132. // CHECK:STDOUT: %struct: %struct_type.a.b.cfd = struct_value (%int_1.loc29, %int_2) [concrete = constants.%struct]
  133. // CHECK:STDOUT: %.loc29_29.2: %struct_type.a.b.cfd = converted %.loc29_29.1, %struct [concrete = constants.%struct]
  134. // CHECK:STDOUT: assign file.%c.var, <error>
  135. // CHECK:STDOUT: %int_32.loc35_19: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  136. // CHECK:STDOUT: %i32.loc35_19: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  137. // CHECK:STDOUT: %int_32.loc35_28: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  138. // CHECK:STDOUT: %i32.loc35_28: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  139. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [concrete = constants.%struct_type.a.b.501]
  140. // CHECK:STDOUT: %int_0.loc35: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  141. // CHECK:STDOUT: assign file.%d.var, <error>
  142. // CHECK:STDOUT: return
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: