/src/test/ui/traits/object/with-self-in-projection-output-repeated-supertrait.rs

https://gitlab.com/rust-lang/rust · Rust · 51 lines · 25 code · 9 blank · 17 comment · 3 complexity · 0502eea64083bbfcd6ba0a11a72e7adc MD5 · raw file

  1. // build-pass (FIXME(62277): could be check-pass?)
  2. // FIXME(eddyb) shorten the name so windows doesn't choke on it.
  3. #![crate_name = "trait_test"]
  4. // Regression test related to #56288. Check that a supertrait projection (of
  5. // `Output`) that references `Self` is ok if there is another occurrence of
  6. // the same supertrait that specifies the projection explicitly, even if
  7. // the projection's associated type is not explicitly specified in the object type.
  8. //
  9. // Note that in order for this to compile, we need the `Self`-referencing projection
  10. // to normalize fairly directly to a concrete type, otherwise the trait resolver
  11. // will hate us.
  12. //
  13. // There is a test in `trait-object-with-self-in-projection-output-bad.rs` that
  14. // having a normalizing, but `Self`-containing projection does not *by itself*
  15. // allow you to avoid writing the projected type (`Output`, in this example)
  16. // explicitly.
  17. trait ConstI32 {
  18. type Out;
  19. }
  20. impl<T: ?Sized> ConstI32 for T {
  21. type Out = i32;
  22. }
  23. trait Base {
  24. type Output;
  25. }
  26. trait NormalizingHelper: Base<Output=<Self as ConstI32>::Out> + Base<Output=i32> {
  27. type Target;
  28. }
  29. impl Base for u32
  30. {
  31. type Output = i32;
  32. }
  33. impl NormalizingHelper for u32
  34. {
  35. type Target = i32;
  36. }
  37. fn main() {
  38. // Make sure this works both with and without the associated type
  39. // being specified.
  40. let _x: Box<dyn NormalizingHelper<Target=i32>> = Box::new(2u32);
  41. let _y: Box<dyn NormalizingHelper<Target=i32, Output=i32>> = Box::new(2u32);
  42. }