File tree Expand file tree Collapse file tree 6 files changed +115
-0
lines changed 
src/test/ui/traits/object Expand file tree Collapse file tree 6 files changed +115
-0
lines changed Original file line number Diff line number Diff line change 1+ // Taken from https://github.com/rust-lang/rust/issues/44454#issue-256435333 
2+ 
3+ trait  Animal < X > :  ' static  { } 
4+ 
5+ fn  foo < Y ,  X > ( ) 
6+ where 
7+     Y :  Animal < X >  + ?Sized , 
8+ { 
9+     // `Y` implements `Animal<X>` so `Y` is 'static. 
10+     baz :: < Y > ( ) 
11+ } 
12+ 
13+ fn  bar < ' a > ( _arg :  & ' a  i32 )  { 
14+     foo :: < dyn  Animal < & ' a  i32 > ,  & ' a  i32 > ( )  //~ ERROR: lifetime may not live long enough 
15+ } 
16+ 
17+ fn  baz < T :  ' static  + ?Sized > ( )  { } 
18+ 
19+ fn  main ( )  { 
20+     let  a = 5 ; 
21+     bar ( & a) ; 
22+ } 
Original file line number Diff line number Diff line change 1+ error: lifetime may not live long enough
2+   --> $DIR/issue-44454-1.rs:14:5
3+    |
4+ LL | fn bar<'a>(_arg: &'a i32) {
5+    |        -- lifetime `'a` defined here
6+ LL |     foo::<dyn Animal<&'a i32>, &'a i32>()
7+    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
8+ 
9+ error: aborting due to previous error
10+ 
Original file line number Diff line number Diff line change 1+ // Taken from https://github.com/rust-lang/rust/issues/44454#issuecomment-1175925928 
2+ 
3+ trait  Trait < ARG :  ' static > :  ' static  { 
4+     type  Assoc :  AsRef < str > ; 
5+ } 
6+ 
7+ fn  hr < T :  ?Sized ,  ARG > ( x :  T :: Assoc )  -> Box < dyn  AsRef < str >  + ' static > 
8+ where 
9+     T :  Trait < ARG > 
10+ { 
11+     Box :: new ( x) 
12+ } 
13+ 
14+ fn  extend_lt < ' a > ( x :  & ' a  str )  -> Box < dyn  AsRef < str >  + ' static >  { 
15+     type  DynTrait  = dyn  for < ' a >  Trait < & ' a  str ,  Assoc  = & ' a  str > ; 
16+     hr :: < DynTrait ,  _ > ( x)  //~ ERROR: borrowed data escapes outside of function 
17+ } 
18+ 
19+ fn  main ( )  { 
20+     let  extended = extend_lt ( & String :: from ( "hello" ) ) ; 
21+     println ! ( "{}" ,  extended. as_ref( ) . as_ref( ) ) ; 
22+ } 
Original file line number Diff line number Diff line change 1+ error[E0521]: borrowed data escapes outside of function
2+   --> $DIR/issue-44454-2.rs:16:5
3+    |
4+ LL | fn extend_lt<'a>(x: &'a str) -> Box<dyn AsRef<str> + 'static> {
5+    |              --  - `x` is a reference that is only valid in the function body
6+    |              |
7+    |              lifetime `'a` defined here
8+ LL |     type DynTrait = dyn for<'a> Trait<&'a str, Assoc = &'a str>;
9+ LL |     hr::<DynTrait, _>(x)
10+    |     ^^^^^^^^^^^^^^^^^^^^
11+    |     |
12+    |     `x` escapes the function body here
13+    |     argument requires that `'a` must outlive `'static`
14+ 
15+ error: aborting due to previous error
16+ 
17+ For more information about this error, try `rustc --explain E0521`.
Original file line number Diff line number Diff line change 1+ // Taken from https://github.com/rust-lang/rust/issues/44454#issuecomment-1332781290 
2+ 
3+ use  std:: any:: Any ; 
4+ 
5+ trait  Animal < X > :  ' static  { } 
6+ 
7+ trait  Projector  { 
8+     type  Foo ; 
9+ } 
10+ 
11+ impl < X >  Projector  for  dyn  Animal < X >  { 
12+     type  Foo  = X ; 
13+ } 
14+ 
15+ fn  make_static < ' a ,  T > ( t :  & ' a  T )  -> & ' static  T  { 
16+     let  x:  <dyn  Animal < & ' a  T >  as  Projector >:: Foo  = t; 
17+     let  any = generic :: < dyn  Animal < & ' a  T > ,  & ' a  T > ( x) ; 
18+     //~^ ERROR: lifetime may not live long enough 
19+     any. downcast_ref :: < & ' static  T > ( ) . unwrap ( ) 
20+ } 
21+ 
22+ fn  generic < T :  Projector  + Animal < U >  + ?Sized ,  U > ( x :  <T  as  Projector >:: Foo )  -> Box < dyn  Any >  { 
23+     make_static_any ( x) 
24+ } 
25+ 
26+ fn  make_static_any < U :  ' static > ( u :  U )  -> Box < dyn  Any >  { 
27+     Box :: new ( u) 
28+ } 
29+ 
30+ fn  main ( )  { 
31+     let  a = make_static ( & "salut" . to_string ( ) ) ; 
32+     println ! ( "{}" ,  * a) ; 
33+ } 
Original file line number Diff line number Diff line change 1+ error: lifetime may not live long enough
2+   --> $DIR/issue-44454-3.rs:17:15
3+    |
4+ LL | fn make_static<'a, T>(t: &'a T) -> &'static T {
5+    |                -- lifetime `'a` defined here
6+ LL |     let x: <dyn Animal<&'a T> as Projector>::Foo = t;
7+ LL |     let any = generic::<dyn Animal<&'a T>, &'a T>(x);
8+    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
9+ 
10+ error: aborting due to previous error
11+ 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments