struct.carbon 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/min_prelude/facet_types.carbon
  6. // EXTRA-ARGS: --dump-sem-ir-ranges=only
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/lookup/min_prelude/struct.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/min_prelude/struct.carbon
  13. interface Z {
  14. let X:! type;
  15. fn ZZ() -> X;
  16. }
  17. class C { adapt (); }
  18. class D { adapt (); }
  19. impl {.a: (), .b: ()} as Z where .X = C {
  20. fn ZZ() -> C { return () as C; }
  21. }
  22. // A struct with different field names. Structs with different field names are
  23. // different types and can be matched by an impl lookup with matching field
  24. // names.
  25. impl {.aa: (), .bb: ()} as Z where .X = D {
  26. fn ZZ() -> D { return () as D; }
  27. }
  28. // TODO: Are structs with different field orders different types too, for impl
  29. // lookup? Or should this impl be diagnosed as overlapping with the impl on
  30. // `{.a, .b}`? Raised in:
  31. // https://github.com/carbon-language/carbon-lang/issues/5413
  32. impl {.b: (), .a: ()} as Z where .X = D {
  33. fn ZZ() -> D { return () as D; }
  34. }
  35. fn F(T:! Z) -> T.X {
  36. return T.ZZ();
  37. }
  38. fn G() {
  39. // Check which impl is selected for struct literals with different field
  40. // names.
  41. let c1: C = F({.a: (), .b: ()});
  42. let d1: D = F({.aa: (), .bb: ()});
  43. // TODO: It is unclear if `F` should return `C` or `D`, since it has the same
  44. // field names but different order as the impl that would give us `C` here.
  45. let d2: D = F({.b: (), .a: ()});
  46. }