struct.carbon 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/convert.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/lookup/struct.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/struct.carbon
  12. interface Z {
  13. let X:! type;
  14. fn ZZ() -> X;
  15. }
  16. class C { adapt (); }
  17. class D { adapt (); }
  18. impl {.a: (), .b: ()} as Z where .X = C {
  19. fn ZZ() -> C { return () as C; }
  20. }
  21. // A struct with different field names. Structs with different field names are
  22. // different types and can be matched by an impl lookup with matching field
  23. // names.
  24. impl {.aa: (), .bb: ()} as Z where .X = D {
  25. fn ZZ() -> D { return () as D; }
  26. }
  27. // Structs with different field orders are different types too, for impl
  28. // lookup. See https://github.com/carbon-language/carbon-lang/issues/5413.
  29. impl {.b: (), .a: ()} as Z where .X = D {
  30. fn ZZ() -> D { return () as D; }
  31. }
  32. fn F(T:! Z) -> T.X {
  33. return T.ZZ();
  34. }
  35. fn G() {
  36. // Check which impl is selected for struct literals with different field
  37. // names.
  38. let c1: C = F({.a: (), .b: ()});
  39. let d1: D = F({.aa: (), .bb: ()});
  40. let d2: D = F({.b: (), .a: ()});
  41. }