struct.carbon 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. //
  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. // TODO: Are structs with different field orders different types too, for impl
  28. // lookup? Or should this impl be diagnosed as overlapping with the impl on
  29. // `{.a, .b}`? Raised in:
  30. // https://github.com/carbon-language/carbon-lang/issues/5413
  31. impl {.b: (), .a: ()} as Z where .X = D {
  32. fn ZZ() -> D { return () as D; }
  33. }
  34. fn F(T:! Z) -> T.X {
  35. return T.ZZ();
  36. }
  37. fn G() {
  38. // Check which impl is selected for struct literals with different field
  39. // names.
  40. let c1: C = F({.a: (), .b: ()});
  41. let d1: D = F({.aa: (), .bb: ()});
  42. // TODO: It is unclear if `F` should return `C` or `D`, since it has the same
  43. // field names but different order as the impl that would give us `C` here.
  44. let d2: D = F({.b: (), .a: ()});
  45. }