fail_duplicate_name.carbon 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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+6]]: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+3]]: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. var y: {.b: i32, .c: i32} = {.b = 3, .b = 4};
  49. // CHECK:STDOUT: --- fail_duplicate_name.carbon
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: constants {
  52. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  53. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  54. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  55. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  56. // CHECK:STDOUT: %.1: i32 = int_value 1 [template]
  57. // CHECK:STDOUT: %.2: type = struct_type {.a: i32} [template]
  58. // CHECK:STDOUT: %.3: i32 = int_value 2 [template]
  59. // CHECK:STDOUT: %.4: type = struct_type {.b: i32, .c: i32} [template]
  60. // CHECK:STDOUT: %.6: i32 = int_value 3 [template]
  61. // CHECK:STDOUT: %.7: i32 = int_value 4 [template]
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: imports {
  65. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  66. // CHECK:STDOUT: .Int32 = %import_ref
  67. // CHECK:STDOUT: import Core//prelude
  68. // CHECK:STDOUT: import Core//prelude/...
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: file {
  73. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  74. // CHECK:STDOUT: .Core = imports.%Core
  75. // CHECK:STDOUT: .F = %F.decl
  76. // CHECK:STDOUT: .v = @__global_init.%v
  77. // CHECK:STDOUT: .w = @__global_init.%w
  78. // CHECK:STDOUT: .x = %x
  79. // CHECK:STDOUT: .y = %y
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %Core.import = import Core
  82. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  83. // CHECK:STDOUT: %return.patt: <error> = return_slot_pattern
  84. // CHECK:STDOUT: %return.param_patt: <error> = out_param_pattern %return.patt, runtime_param0
  85. // CHECK:STDOUT: } {
  86. // CHECK:STDOUT: %int.make_type_32.loc18_16: init type = call constants.%Int32() [template = i32]
  87. // CHECK:STDOUT: %.loc18_16.1: type = value_of_initializer %int.make_type_32.loc18_16 [template = i32]
  88. // CHECK:STDOUT: %.loc18_16.2: type = converted %int.make_type_32.loc18_16, %.loc18_16.1 [template = i32]
  89. // CHECK:STDOUT: %int.make_type_32.loc18_27: init type = call constants.%Int32() [template = i32]
  90. // CHECK:STDOUT: %.loc18_27.1: type = value_of_initializer %int.make_type_32.loc18_27 [template = i32]
  91. // CHECK:STDOUT: %.loc18_27.2: type = converted %int.make_type_32.loc18_27, %.loc18_27.1 [template = i32]
  92. // CHECK:STDOUT: %int.make_type_32.loc18_36: init type = call constants.%Int32() [template = i32]
  93. // CHECK:STDOUT: %.loc18_36.1: type = value_of_initializer %int.make_type_32.loc18_36 [template = i32]
  94. // CHECK:STDOUT: %.loc18_36.2: type = converted %int.make_type_32.loc18_36, %.loc18_36.1 [template = i32]
  95. // CHECK:STDOUT: %int.make_type_32.loc18_47: init type = call constants.%Int32() [template = i32]
  96. // CHECK:STDOUT: %.loc18_47.1: type = value_of_initializer %int.make_type_32.loc18_47 [template = i32]
  97. // CHECK:STDOUT: %.loc18_47.2: type = converted %int.make_type_32.loc18_47, %.loc18_47.1 [template = i32]
  98. // CHECK:STDOUT: %int.make_type_32.loc18_56: init type = call constants.%Int32() [template = i32]
  99. // CHECK:STDOUT: %.loc18_56.1: type = value_of_initializer %int.make_type_32.loc18_56 [template = i32]
  100. // CHECK:STDOUT: %.loc18_56.2: type = converted %int.make_type_32.loc18_56, %.loc18_56.1 [template = i32]
  101. // CHECK:STDOUT: %return.param: ref <error> = out_param runtime_param0
  102. // CHECK:STDOUT: %return: ref <error> = return_slot %return.param
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %int.make_type_32.loc27_13: init type = call constants.%Int32() [template = i32]
  105. // CHECK:STDOUT: %.loc27_13.1: type = value_of_initializer %int.make_type_32.loc27_13 [template = i32]
  106. // CHECK:STDOUT: %.loc27_13.2: type = converted %int.make_type_32.loc27_13, %.loc27_13.1 [template = i32]
  107. // CHECK:STDOUT: %int.make_type_32.loc27_22: init type = call constants.%Int32() [template = i32]
  108. // CHECK:STDOUT: %.loc27_22.1: type = value_of_initializer %int.make_type_32.loc27_22 [template = i32]
  109. // CHECK:STDOUT: %.loc27_22.2: type = converted %int.make_type_32.loc27_22, %.loc27_22.1 [template = i32]
  110. // CHECK:STDOUT: %int.make_type_32.loc36: init type = call constants.%Int32() [template = i32]
  111. // CHECK:STDOUT: %.loc36_8.1: type = value_of_initializer %int.make_type_32.loc36 [template = i32]
  112. // CHECK:STDOUT: %.loc36_8.2: type = converted %int.make_type_32.loc36, %.loc36_8.1 [template = i32]
  113. // CHECK:STDOUT: %int.make_type_32.loc45: init type = call constants.%Int32() [template = i32]
  114. // CHECK:STDOUT: %.loc45_13.1: type = value_of_initializer %int.make_type_32.loc45 [template = i32]
  115. // CHECK:STDOUT: %.loc45_13.2: type = converted %int.make_type_32.loc45, %.loc45_13.1 [template = i32]
  116. // CHECK:STDOUT: %.loc45_16: type = struct_type {.a: i32} [template = constants.%.2]
  117. // CHECK:STDOUT: %x.var: ref %.2 = var x
  118. // CHECK:STDOUT: %x: ref %.2 = bind_name x, %x.var
  119. // CHECK:STDOUT: %int.make_type_32.loc53_13: init type = call constants.%Int32() [template = i32]
  120. // CHECK:STDOUT: %.loc53_13.1: type = value_of_initializer %int.make_type_32.loc53_13 [template = i32]
  121. // CHECK:STDOUT: %.loc53_13.2: type = converted %int.make_type_32.loc53_13, %.loc53_13.1 [template = i32]
  122. // CHECK:STDOUT: %int.make_type_32.loc53_22: init type = call constants.%Int32() [template = i32]
  123. // CHECK:STDOUT: %.loc53_22.1: type = value_of_initializer %int.make_type_32.loc53_22 [template = i32]
  124. // CHECK:STDOUT: %.loc53_22.2: type = converted %int.make_type_32.loc53_22, %.loc53_22.1 [template = i32]
  125. // CHECK:STDOUT: %.loc53_25: type = struct_type {.b: i32, .c: i32} [template = constants.%.4]
  126. // CHECK:STDOUT: %y.var: ref %.4 = var y
  127. // CHECK:STDOUT: %y: ref %.4 = bind_name y, %y.var
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @F() -> <error>;
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: fn @__global_init() {
  133. // CHECK:STDOUT: !entry:
  134. // CHECK:STDOUT: %.loc27_35: i32 = int_value 1 [template = constants.%.1]
  135. // CHECK:STDOUT: %.loc27_36: %.2 = struct_literal (%.loc27_35)
  136. // CHECK:STDOUT: %v: <error> = bind_name v, <error>
  137. // CHECK:STDOUT: %.loc36_22: i32 = int_value 1 [template = constants.%.1]
  138. // CHECK:STDOUT: %.loc36_32: i32 = int_value 2 [template = constants.%.3]
  139. // CHECK:STDOUT: %w: i32 = bind_name w, <error>
  140. // CHECK:STDOUT: %.loc45_26: i32 = int_value 1 [template = constants.%.1]
  141. // CHECK:STDOUT: %.loc45_34: i32 = int_value 2 [template = constants.%.3]
  142. // CHECK:STDOUT: assign file.%x.var, <error>
  143. // CHECK:STDOUT: %.loc53_35: i32 = int_value 3 [template = constants.%.6]
  144. // CHECK:STDOUT: %.loc53_43: i32 = int_value 4 [template = constants.%.7]
  145. // CHECK:STDOUT: assign file.%y.var, <error>
  146. // CHECK:STDOUT: return
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: