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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/struct/fail_duplicate_name.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/struct/fail_duplicate_name.carbon
  14. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:42: error: duplicated field name `abc` in struct type literal [StructNameDuplicate]
  15. // CHECK:STDERR: fn F() -> {.d: i32, .abc: i32, .e: i32, .abc: i32, .f: i32};
  16. // CHECK:STDERR: ^~~
  17. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:22: note: field with the same name here [StructNamePrevious]
  18. // CHECK:STDERR: fn F() -> {.d: i32, .abc: i32, .e: i32, .abc: i32, .f: i32};
  19. // CHECK:STDERR: ^~~
  20. // CHECK:STDERR:
  21. fn F() -> {.d: i32, .abc: i32, .e: i32, .abc: i32, .f: i32};
  22. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:19: error: duplicated field name `a` in struct type literal [StructNameDuplicate]
  23. // CHECK:STDERR: let v: {.a: i32, .a: i32} = {.a = 1};
  24. // CHECK:STDERR: ^
  25. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:10: note: field with the same name here [StructNamePrevious]
  26. // CHECK:STDERR: let v: {.a: i32, .a: i32} = {.a = 1};
  27. // CHECK:STDERR: ^
  28. // CHECK:STDERR:
  29. let v: {.a: i32, .a: i32} = {.a = 1};
  30. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:26: error: duplicated field name `def` in struct literal [StructNameDuplicate]
  31. // CHECK:STDERR: let w: i32 = {.def = 1, .def = 2}.def;
  32. // CHECK:STDERR: ^~~
  33. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:16: note: field with the same name here [StructNamePrevious]
  34. // CHECK:STDERR: let w: i32 = {.def = 1, .def = 2}.def;
  35. // CHECK:STDERR: ^~~
  36. // CHECK:STDERR:
  37. let w: i32 = {.def = 1, .def = 2}.def;
  38. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:30: error: duplicated field name `a` in struct literal [StructNameDuplicate]
  39. // CHECK:STDERR: var x: {.a: i32} = {.a = 1, .a = 2};
  40. // CHECK:STDERR: ^
  41. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:22: note: field with the same name here [StructNamePrevious]
  42. // CHECK:STDERR: var x: {.a: i32} = {.a = 1, .a = 2};
  43. // CHECK:STDERR: ^
  44. // CHECK:STDERR:
  45. var x: {.a: i32} = {.a = 1, .a = 2};
  46. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+7]]:39: error: duplicated field name `b` in struct literal [StructNameDuplicate]
  47. // CHECK:STDERR: var y: {.b: i32, .c: i32} = {.b = 3, .b = 4};
  48. // CHECK:STDERR: ^
  49. // CHECK:STDERR: fail_duplicate_name.carbon:[[@LINE+4]]:31: note: field with the same name here [StructNamePrevious]
  50. // CHECK:STDERR: var y: {.b: i32, .c: i32} = {.b = 3, .b = 4};
  51. // CHECK:STDERR: ^
  52. // CHECK:STDERR:
  53. var y: {.b: i32, .c: i32} = {.b = 3, .b = 4};
  54. // CHECK:STDOUT: --- fail_duplicate_name.carbon
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: constants {
  57. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  58. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  59. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  60. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  61. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  62. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  63. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  64. // CHECK:STDOUT: %struct_type.a.a6c: type = struct_type {.a: Core.IntLiteral} [concrete]
  65. // CHECK:STDOUT: %struct: %struct_type.a.a6c = struct_value (%int_1) [concrete]
  66. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  67. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  68. // CHECK:STDOUT: %struct_type.a.ba9: type = struct_type {.a: %i32} [concrete]
  69. // CHECK:STDOUT: %pattern_type.268: type = pattern_type %struct_type.a.ba9 [concrete]
  70. // CHECK:STDOUT: %struct_type.b.c: type = struct_type {.b: %i32, .c: %i32} [concrete]
  71. // CHECK:STDOUT: %pattern_type.366: type = pattern_type %struct_type.b.c [concrete]
  72. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [concrete]
  73. // CHECK:STDOUT: %int_4: Core.IntLiteral = int_value 4 [concrete]
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: imports {
  77. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  78. // CHECK:STDOUT: .Int = %Core.Int
  79. // CHECK:STDOUT: import Core//prelude
  80. // CHECK:STDOUT: import Core//prelude/...
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: file {
  86. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  87. // CHECK:STDOUT: .Core = imports.%Core
  88. // CHECK:STDOUT: .F = %F.decl
  89. // CHECK:STDOUT: .v = %v
  90. // CHECK:STDOUT: .w = %w
  91. // CHECK:STDOUT: .x = %x
  92. // CHECK:STDOUT: .y = %y
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %Core.import = import Core
  95. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {
  96. // CHECK:STDOUT: %int_32.loc22_16: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  97. // CHECK:STDOUT: %i32.loc22_16: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  98. // CHECK:STDOUT: %int_32.loc22_27: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  99. // CHECK:STDOUT: %i32.loc22_27: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  100. // CHECK:STDOUT: %int_32.loc22_36: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  101. // CHECK:STDOUT: %i32.loc22_36: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  102. // CHECK:STDOUT: %int_32.loc22_47: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  103. // CHECK:STDOUT: %i32.loc22_47: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  104. // CHECK:STDOUT: %int_32.loc22_56: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  105. // CHECK:STDOUT: %i32.loc22_56: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: name_binding_decl {
  108. // CHECK:STDOUT: %v.patt: <error> = value_binding_pattern v [concrete]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %.1: <error> = splice_block <error> [concrete = <error>] {
  111. // CHECK:STDOUT: %int_32.loc31_13: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  112. // CHECK:STDOUT: %i32.loc31_13: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  113. // CHECK:STDOUT: %int_32.loc31_22: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  114. // CHECK:STDOUT: %i32.loc31_22: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT: %v: <error> = value_binding v, <error> [concrete = <error>]
  117. // CHECK:STDOUT: name_binding_decl {
  118. // CHECK:STDOUT: %w.patt: %pattern_type.7ce = value_binding_pattern w [concrete]
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: %.loc40: type = splice_block %i32.loc40 [concrete = constants.%i32] {
  121. // CHECK:STDOUT: %int_32.loc40: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  122. // CHECK:STDOUT: %i32.loc40: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %w: %i32 = value_binding w, <error> [concrete = <error>]
  125. // CHECK:STDOUT: name_binding_decl {
  126. // CHECK:STDOUT: %x.patt: %pattern_type.268 = ref_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: %.loc49: type = splice_block %struct_type.a [concrete = constants.%struct_type.a.ba9] {
  131. // CHECK:STDOUT: %int_32.loc49: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  132. // CHECK:STDOUT: %i32.loc49: 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 = ref_binding x, %x.var [concrete = %x.var]
  136. // CHECK:STDOUT: name_binding_decl {
  137. // CHECK:STDOUT: %y.patt: %pattern_type.366 = ref_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: %.loc58: type = splice_block %struct_type.b.c [concrete = constants.%struct_type.b.c] {
  142. // CHECK:STDOUT: %int_32.loc58_13: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  143. // CHECK:STDOUT: %i32.loc58_13: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  144. // CHECK:STDOUT: %int_32.loc58_22: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  145. // CHECK:STDOUT: %i32.loc58_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 = ref_binding y, %y.var [concrete = %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.loc31: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  156. // CHECK:STDOUT: %.loc31: %struct_type.a.a6c = struct_literal (%int_1.loc31) [concrete = constants.%struct]
  157. // CHECK:STDOUT: %int_1.loc40: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  158. // CHECK:STDOUT: %int_2.loc40: 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.loc49: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  161. // CHECK:STDOUT: %int_2.loc49: 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: