fail_invalid_base.carbon 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  6. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  7. //
  8. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
  9. //
  10. // AUTOUPDATE
  11. // TIP: To test this file alone, run:
  12. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/index/fail_invalid_base.carbon
  13. // TIP: To dump output, run:
  14. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_invalid_base.carbon
  15. namespace N;
  16. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: expression cannot be used as a value [UseOfNonExprAsValue]
  17. // CHECK:STDERR: var a: i32 = N[0];
  18. // CHECK:STDERR: ^
  19. // CHECK:STDERR:
  20. var a: i32 = N[0];
  21. fn F();
  22. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: expression cannot be used as a value [UseOfNonExprAsValue]
  23. // CHECK:STDERR: var b: i32 = F[1];
  24. // CHECK:STDERR: ^
  25. // CHECK:STDERR:
  26. var b: i32 = F[1];
  27. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: cannot access member of interface `Core.IndexWith(Core.IntLiteral)` in type `{.a: Core.IntLiteral, .b: Core.IntLiteral}` that does not implement that interface [MissingImplInMemberAccess]
  28. // CHECK:STDERR: var c: i32 = {.a = 1, .b = 2}[0];
  29. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  30. // CHECK:STDERR:
  31. var c: i32 = {.a = 1, .b = 2}[0];
  32. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: cannot access member of interface `Core.IndexWith(Core.IntLiteral)` in type `type` that does not implement that interface [MissingImplInMemberAccess]
  33. // CHECK:STDERR: var d: i32 = {.a: i32, .b: i32}[0];
  34. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  35. // CHECK:STDERR:
  36. var d: i32 = {.a: i32, .b: i32}[0];
  37. // CHECK:STDOUT: --- fail_invalid_base.carbon
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: constants {
  40. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  41. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  42. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  43. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  44. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  45. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  46. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  47. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  48. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  49. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  50. // CHECK:STDOUT: %struct_type.a.b.cfd: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [concrete]
  51. // CHECK:STDOUT: %struct: %struct_type.a.b.cfd = struct_value (%int_1, %int_2) [concrete]
  52. // CHECK:STDOUT: %IndexWith.type.504: type = generic_interface_type @IndexWith [concrete]
  53. // CHECK:STDOUT: %IndexWith.generic: %IndexWith.type.504 = struct_value () [concrete]
  54. // CHECK:STDOUT: %struct_type.a.b.501: type = struct_type {.a: %i32, .b: %i32} [concrete]
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: imports {
  58. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  59. // CHECK:STDOUT: .Int = %Core.Int
  60. // CHECK:STDOUT: .IndexWith = %Core.IndexWith
  61. // CHECK:STDOUT: import Core//prelude
  62. // CHECK:STDOUT: import Core//prelude/...
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  65. // CHECK:STDOUT: %Core.IndexWith: %IndexWith.type.504 = import_ref Core//prelude/operators/index, IndexWith, loaded [concrete = constants.%IndexWith.generic]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: file {
  69. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  70. // CHECK:STDOUT: .Core = imports.%Core
  71. // CHECK:STDOUT: .N = %N
  72. // CHECK:STDOUT: .a = %a
  73. // CHECK:STDOUT: .F = %F.decl
  74. // CHECK:STDOUT: .b = %b
  75. // CHECK:STDOUT: .c = %c
  76. // CHECK:STDOUT: .d = %d
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core.import = import Core
  79. // CHECK:STDOUT: %N: <namespace> = namespace [concrete] {}
  80. // CHECK:STDOUT: name_binding_decl {
  81. // CHECK:STDOUT: %a.patt: %pattern_type.7ce = ref_binding_pattern a [concrete]
  82. // CHECK:STDOUT: %a.var_patt: %pattern_type.7ce = var_pattern %a.patt [concrete]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %a.var: ref %i32 = var %a.var_patt [concrete]
  85. // CHECK:STDOUT: %i32.loc21: type = type_literal constants.%i32 [concrete = constants.%i32]
  86. // CHECK:STDOUT: %a: ref %i32 = ref_binding a, %a.var [concrete = %a.var]
  87. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  88. // CHECK:STDOUT: name_binding_decl {
  89. // CHECK:STDOUT: %b.patt: %pattern_type.7ce = ref_binding_pattern b [concrete]
  90. // CHECK:STDOUT: %b.var_patt: %pattern_type.7ce = var_pattern %b.patt [concrete]
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %b.var: ref %i32 = var %b.var_patt [concrete]
  93. // CHECK:STDOUT: %i32.loc28: type = type_literal constants.%i32 [concrete = constants.%i32]
  94. // CHECK:STDOUT: %b: ref %i32 = ref_binding b, %b.var [concrete = %b.var]
  95. // CHECK:STDOUT: name_binding_decl {
  96. // CHECK:STDOUT: %c.patt: %pattern_type.7ce = ref_binding_pattern c [concrete]
  97. // CHECK:STDOUT: %c.var_patt: %pattern_type.7ce = var_pattern %c.patt [concrete]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %c.var: ref %i32 = var %c.var_patt [concrete]
  100. // CHECK:STDOUT: %i32.loc34: type = type_literal constants.%i32 [concrete = constants.%i32]
  101. // CHECK:STDOUT: %c: ref %i32 = ref_binding c, %c.var [concrete = %c.var]
  102. // CHECK:STDOUT: name_binding_decl {
  103. // CHECK:STDOUT: %d.patt: %pattern_type.7ce = ref_binding_pattern d [concrete]
  104. // CHECK:STDOUT: %d.var_patt: %pattern_type.7ce = var_pattern %d.patt [concrete]
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %d.var: ref %i32 = var %d.var_patt [concrete]
  107. // CHECK:STDOUT: %i32.loc40: type = type_literal constants.%i32 [concrete = constants.%i32]
  108. // CHECK:STDOUT: %d: ref %i32 = ref_binding d, %d.var [concrete = %d.var]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: fn @F();
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @__global_init() {
  114. // CHECK:STDOUT: !entry:
  115. // CHECK:STDOUT: %N.ref: <namespace> = name_ref N, file.%N [concrete = file.%N]
  116. // CHECK:STDOUT: %int_0.loc21: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  117. // CHECK:STDOUT: assign file.%a.var, <error>
  118. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  119. // CHECK:STDOUT: %int_1.loc28: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  120. // CHECK:STDOUT: assign file.%b.var, <error>
  121. // CHECK:STDOUT: %int_1.loc34: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  122. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  123. // CHECK:STDOUT: %.loc34_29.1: %struct_type.a.b.cfd = struct_literal (%int_1.loc34, %int_2) [concrete = constants.%struct]
  124. // CHECK:STDOUT: %int_0.loc34: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  125. // CHECK:STDOUT: %struct: %struct_type.a.b.cfd = struct_value (%int_1.loc34, %int_2) [concrete = constants.%struct]
  126. // CHECK:STDOUT: %.loc34_29.2: %struct_type.a.b.cfd = converted %.loc34_29.1, %struct [concrete = constants.%struct]
  127. // CHECK:STDOUT: assign file.%c.var, <error>
  128. // CHECK:STDOUT: %i32.loc40_19: type = type_literal constants.%i32 [concrete = constants.%i32]
  129. // CHECK:STDOUT: %i32.loc40_28: type = type_literal constants.%i32 [concrete = constants.%i32]
  130. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: %i32, .b: %i32} [concrete = constants.%struct_type.a.b.501]
  131. // CHECK:STDOUT: %int_0.loc40: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  132. // CHECK:STDOUT: assign file.%d.var, <error>
  133. // CHECK:STDOUT: return
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT: