]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/ty-outlives/projection-one-region-trait-bound-static-closure.rs
Don't ICE on tuple struct ctor with incorrect arg count
[rust.git] / src / test / ui / nll / ty-outlives / projection-one-region-trait-bound-static-closure.rs
1 // Copyright 2016 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 // Test cases where we constrain `<T as Anything<'b>>::AssocType` to
12 // outlive `'static`. In this case, we don't get any errors, and in fact
13 // we don't even propagate constraints from the closures to the callers.
14
15 // compile-flags:-Zborrowck=mir -Zverbose
16 // compile-pass
17
18 #![allow(warnings)]
19 #![feature(dyn_trait)]
20 #![feature(rustc_attrs)]
21
22 use std::cell::Cell;
23
24 trait Anything<'a> {
25     type AssocType: 'static;
26 }
27
28 fn with_signature<'a, T, F>(cell: Cell<&'a ()>, t: T, op: F)
29 where
30     F: FnOnce(Cell<&'a ()>, T),
31 {
32     op(cell, t)
33 }
34
35 fn require<'a, 'b, T>(_cell: Cell<&'a ()>, _t: T)
36 where
37     T: Anything<'b>,
38     T::AssocType: 'a,
39 {
40 }
41
42 #[rustc_regions]
43 fn no_relationships_late<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
44 where
45     T: Anything<'b>,
46 {
47     with_signature(cell, t, |cell, t| require(cell, t));
48 }
49
50 #[rustc_regions]
51 fn no_relationships_early<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
52 where
53     T: Anything<'b>,
54     'a: 'a,
55 {
56     with_signature(cell, t, |cell, t| require(cell, t));
57 }
58
59 #[rustc_regions]
60 fn projection_outlives<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
61 where
62     T: Anything<'b>,
63     T::AssocType: 'a,
64 {
65     // This error is unfortunate. This code ought to type-check: we
66     // are projecting `<T as Anything<'b>>::AssocType`, and we know
67     // that this outlives `'a` because of the where-clause. However,
68     // the way the region checker works, we don't register this
69     // outlives obligation, and hence we get an error: this is because
70     // what we see is a projection like `<T as
71     // Anything<'?0>>::AssocType`, and we don't yet know if `?0` will
72     // equal `'b` or not, so we ignore the where-clause. Obviously we
73     // can do better here with a more involved verification step.
74
75     with_signature(cell, t, |cell, t| require(cell, t));
76 }
77
78 #[rustc_regions]
79 fn elements_outlive<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
80 where
81     T: Anything<'b>,
82     'b: 'a,
83 {
84     with_signature(cell, t, |cell, t| require(cell, t));
85 }
86
87 #[rustc_regions]
88 fn one_region<'a, T>(cell: Cell<&'a ()>, t: T)
89 where
90     T: Anything<'a>,
91 {
92     // Note that in this case the closure still propagates an external
93     // requirement between two variables in its signature, but the
94     // creator maps both those two region variables to `'a` on its
95     // side.
96     with_signature(cell, t, |cell, t| require(cell, t));
97 }
98
99 fn main() {}