fail_duplicate_name.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/struct/fail_duplicate_name.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/struct/fail_duplicate_name.carbon
  10. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:42: error: duplicated field name `abc` in struct type literal [StructNameDuplicate]
  11. // CHECK:STDERR: fn F() -> {.d: i32, .abc: i32, .e: i32, .abc: i32, .f: i32};
  12. // CHECK:STDERR: ^~~
  13. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:22: note: field with the same name here [StructNamePrevious]
  14. // CHECK:STDERR: fn F() -> {.d: i32, .abc: i32, .e: i32, .abc: i32, .f: i32};
  15. // CHECK:STDERR: ^~~
  16. // CHECK:STDERR:
  17. fn F() -> {.d: i32, .abc: i32, .e: i32, .abc: i32, .f: i32};
  18. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:19: error: duplicated field name `a` in struct type literal [StructNameDuplicate]
  19. // CHECK:STDERR: let v: {.a: i32, .a: i32} = {.a = 1};
  20. // CHECK:STDERR: ^
  21. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:10: note: field with the same name here [StructNamePrevious]
  22. // CHECK:STDERR: let v: {.a: i32, .a: i32} = {.a = 1};
  23. // CHECK:STDERR: ^
  24. // CHECK:STDERR:
  25. let v: {.a: i32, .a: i32} = {.a = 1};
  26. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:26: error: duplicated field name `def` in struct literal [StructNameDuplicate]
  27. // CHECK:STDERR: let w: i32 = {.def = 1, .def = 2}.def;
  28. // CHECK:STDERR: ^~~
  29. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:16: note: field with the same name here [StructNamePrevious]
  30. // CHECK:STDERR: let w: i32 = {.def = 1, .def = 2}.def;
  31. // CHECK:STDERR: ^~~
  32. // CHECK:STDERR:
  33. let w: i32 = {.def = 1, .def = 2}.def;
  34. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:30: error: duplicated field name `a` in struct literal [StructNameDuplicate]
  35. // CHECK:STDERR: var x: {.a: i32} = {.a = 1, .a = 2};
  36. // CHECK:STDERR: ^
  37. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:22: note: field with the same name here [StructNamePrevious]
  38. // CHECK:STDERR: var x: {.a: i32} = {.a = 1, .a = 2};
  39. // CHECK:STDERR: ^
  40. // CHECK:STDERR:
  41. var x: {.a: i32} = {.a = 1, .a = 2};
  42. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:39: error: duplicated field name `b` in struct literal [StructNameDuplicate]
  43. // CHECK:STDERR: var y: {.b: i32, .c: i32} = {.b = 3, .b = 4};
  44. // CHECK:STDERR: ^
  45. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:31: note: field with the same name here [StructNamePrevious]
  46. // CHECK:STDERR: var y: {.b: i32, .c: i32} = {.b = 3, .b = 4};
  47. // CHECK:STDERR: ^
  48. // CHECK:STDERR:
  49. var y: {.b: i32, .c: i32} = {.b = 3, .b = 4};
  50. // CHECK:STDOUT: --- fail_duplicate_name.carbon
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: constants {
  53. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  54. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  55. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  56. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  57. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  58. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  59. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  60. // CHECK:STDOUT: %struct_type.a.a6c: type = struct_type {.a: Core.IntLiteral} [concrete]
  61. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  62. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  63. // CHECK:STDOUT: %struct_type.a.ba9: type = struct_type {.a: %i32} [concrete]
  64. // CHECK:STDOUT: %pattern_type.268: type = pattern_type %struct_type.a.ba9 [concrete]
  65. // CHECK:STDOUT: %struct_type.b.c: type = struct_type {.b: %i32, .c: %i32} [concrete]
  66. // CHECK:STDOUT: %pattern_type.366: type = pattern_type %struct_type.b.c [concrete]
  67. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [concrete]
  68. // CHECK:STDOUT: %int_4: Core.IntLiteral = int_value 4 [concrete]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: imports {
  72. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  73. // CHECK:STDOUT: .Int = %Core.Int
  74. // CHECK:STDOUT: import Core//prelude
  75. // CHECK:STDOUT: import Core//prelude/...
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: file {
  81. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  82. // CHECK:STDOUT: .Core = imports.%Core
  83. // CHECK:STDOUT: .F = %F.decl
  84. // CHECK:STDOUT: .v = %v
  85. // CHECK:STDOUT: .w = %w
  86. // CHECK:STDOUT: .x = %x
  87. // CHECK:STDOUT: .y = %y
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %Core.import = import Core
  90. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  91. // CHECK:STDOUT: %return.patt: <error> = return_slot_pattern [concrete]
  92. // CHECK:STDOUT: %return.param_patt: <error> = out_param_pattern %return.patt, call_param0 [concrete]
  93. // CHECK:STDOUT: } {
  94. // CHECK:STDOUT: %int_32.loc18_16: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  95. // CHECK:STDOUT: %i32.loc18_16: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  96. // CHECK:STDOUT: %int_32.loc18_27: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  97. // CHECK:STDOUT: %i32.loc18_27: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  98. // CHECK:STDOUT: %int_32.loc18_36: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  99. // CHECK:STDOUT: %i32.loc18_36: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  100. // CHECK:STDOUT: %int_32.loc18_47: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  101. // CHECK:STDOUT: %i32.loc18_47: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  102. // CHECK:STDOUT: %int_32.loc18_56: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  103. // CHECK:STDOUT: %i32.loc18_56: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  104. // CHECK:STDOUT: %return.param: ref <error> = out_param call_param0
  105. // CHECK:STDOUT: %return: ref <error> = return_slot %return.param
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: name_binding_decl {
  108. // CHECK:STDOUT: %v.patt: <error> = binding_pattern v [concrete]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %.1: <error> = splice_block <error> [concrete = <error>] {
  111. // CHECK:STDOUT: %int_32.loc27_13: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  112. // CHECK:STDOUT: %i32.loc27_13: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  113. // CHECK:STDOUT: %int_32.loc27_22: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  114. // CHECK:STDOUT: %i32.loc27_22: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT: %v: <error> = bind_name v, <error>
  117. // CHECK:STDOUT: name_binding_decl {
  118. // CHECK:STDOUT: %w.patt: %pattern_type.7ce = binding_pattern w [concrete]
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: %.loc36: type = splice_block %i32.loc36 [concrete = constants.%i32] {
  121. // CHECK:STDOUT: %int_32.loc36: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  122. // CHECK:STDOUT: %i32.loc36: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %w: %i32 = bind_name w, <error>
  125. // CHECK:STDOUT: name_binding_decl {
  126. // CHECK:STDOUT: %x.patt: %pattern_type.268 = binding_pattern x [concrete]
  127. // CHECK:STDOUT: %x.var_patt: %pattern_type.268 = var_pattern %x.patt [concrete]
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: %x.var: ref %struct_type.a.ba9 = var %x.var_patt [concrete]
  130. // CHECK:STDOUT: %.loc45: type = splice_block %struct_type.a [concrete = constants.%struct_type.a.ba9] {
  131. // CHECK:STDOUT: %int_32.loc45: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  132. // CHECK:STDOUT: %i32.loc45: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  133. // CHECK:STDOUT: %struct_type.a: type = struct_type {.a: %i32} [concrete = constants.%struct_type.a.ba9]
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT: %x: ref %struct_type.a.ba9 = bind_name x, %x.var
  136. // CHECK:STDOUT: name_binding_decl {
  137. // CHECK:STDOUT: %y.patt: %pattern_type.366 = binding_pattern y [concrete]
  138. // CHECK:STDOUT: %y.var_patt: %pattern_type.366 = var_pattern %y.patt [concrete]
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: %y.var: ref %struct_type.b.c = var %y.var_patt [concrete]
  141. // CHECK:STDOUT: %.loc54: type = splice_block %struct_type.b.c [concrete = constants.%struct_type.b.c] {
  142. // CHECK:STDOUT: %int_32.loc54_13: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  143. // CHECK:STDOUT: %i32.loc54_13: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  144. // CHECK:STDOUT: %int_32.loc54_22: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  145. // CHECK:STDOUT: %i32.loc54_22: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  146. // CHECK:STDOUT: %struct_type.b.c: type = struct_type {.b: %i32, .c: %i32} [concrete = constants.%struct_type.b.c]
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: %y: ref %struct_type.b.c = bind_name y, %y.var
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: fn @F() -> <error>;
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn @__global_init() {
  154. // CHECK:STDOUT: !entry:
  155. // CHECK:STDOUT: %int_1.loc27: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  156. // CHECK:STDOUT: %.loc27: %struct_type.a.a6c = struct_literal (%int_1.loc27)
  157. // CHECK:STDOUT: %int_1.loc36: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  158. // CHECK:STDOUT: %int_2.loc36: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  159. // CHECK:STDOUT: %def.ref: <error> = name_ref def, <error> [concrete = <error>]
  160. // CHECK:STDOUT: %int_1.loc45: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  161. // CHECK:STDOUT: %int_2.loc45: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  162. // CHECK:STDOUT: assign file.%x.var, <error>
  163. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [concrete = constants.%int_3]
  164. // CHECK:STDOUT: %int_4: Core.IntLiteral = int_value 4 [concrete = constants.%int_4]
  165. // CHECK:STDOUT: assign file.%y.var, <error>
  166. // CHECK:STDOUT: return
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: