]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/coerce-unsize-subtype.rs
Rollup merge of #45171 - rust-lang:petrochenkov-patch-2, r=steveklabnik
[rust.git] / src / test / run-pass / coerce-unsize-subtype.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // pretty-expanded FIXME #23616
12
13 use std::rc::Rc;
14
15 fn lub_short<'a, T>(_: &[&'a T], _: &[&'a T]) {}
16
17 // The two arguments are a subtype of their LUB, after coercion.
18 fn long_and_short<'a, T>(xs: &[&'static T; 1], ys: &[&'a T; 1]) {
19     lub_short(xs, ys);
20 }
21
22 // The argument coerces to a subtype of the return type.
23 fn long_to_short<'a, 'b, T>(xs: &'b [&'static T; 1]) -> &'b [&'a T] {
24     xs
25 }
26
27 // Rc<T> is covariant over T just like &T.
28 fn long_to_short_rc<'a, T>(xs: Rc<[&'static T; 1]>) -> Rc<[&'a T]> {
29     xs
30 }
31
32 // LUB-coercion (if-else/match/array) coerces `xs: &'b [&'static T: N]`
33 // to a subtype of the LUB of `xs` and `ys` (i.e. `&'b [&'a T]`),
34 // regardless of the order they appear (in if-else/match/array).
35 fn long_and_short_lub1<'a, 'b, T>(xs: &'b [&'static T; 1], ys: &'b [&'a T]) {
36     let _order1 = [xs, ys];
37     let _order2 = [ys, xs];
38 }
39
40 // LUB-coercion should also have the exact same effect when `&'b [&'a T; N]`
41 // needs to be coerced, i.e. the resulting type is not &'b [&'static T], but
42 // rather the `&'b [&'a T]` LUB.
43 fn long_and_short_lub2<'a, 'b, T>(xs: &'b [&'static T], ys: &'b [&'a T; 1]) {
44     let _order1 = [xs, ys];
45     let _order2 = [ys, xs];
46 }
47
48 fn main() {}