]> git.lizzy.rs Git - rust.git/blob - tests/ui/wf/wf-misc-methods-issue-28609.rs
Rollup merge of #106470 - ehuss:tidy-no-wasm, r=Mark-Simulacrum
[rust.git] / tests / ui / wf / wf-misc-methods-issue-28609.rs
1 // check that misc. method calls are well-formed
2
3 use std::marker::PhantomData;
4 use std::ops::{Deref, Shl};
5
6 #[derive(Copy, Clone)]
7 struct S<'a, 'b: 'a> {
8     marker: PhantomData<&'a &'b ()>,
9     bomb: Option<&'b u32>
10 }
11
12 type S2<'a> = S<'a, 'a>;
13
14 impl<'a, 'b> S<'a, 'b> {
15     fn transmute_inherent(&self, a: &'b u32) -> &'a u32 {
16         a
17     }
18 }
19
20 fn return_dangling_pointer_inherent(s: S2) -> &u32 {
21     let s = s;
22     s.transmute_inherent(&mut 42) //~ ERROR cannot return value referencing temporary value
23 }
24
25 impl<'a, 'b> Deref for S<'a, 'b> {
26     type Target = &'a u32;
27     fn deref(&self) -> &&'a u32 {
28         self.bomb.as_ref().unwrap()
29     }
30 }
31
32 fn return_dangling_pointer_coerce(s: S2) -> &u32 {
33     let four = 4;
34     let mut s = s;
35     s.bomb = Some(&four);
36     &s //~ ERROR cannot return value referencing local variable `four`
37 }
38
39 fn return_dangling_pointer_unary_op(s: S2) -> &u32 {
40     let four = 4;
41     let mut s = s;
42     s.bomb = Some(&four);
43     &*s //~ ERROR cannot return value referencing local variable `four`
44 }
45
46 impl<'a, 'b> Shl<&'b u32> for S<'a, 'b> {
47     type Output = &'a u32;
48     fn shl(self, t: &'b u32) -> &'a u32 { t }
49 }
50
51 fn return_dangling_pointer_binary_op(s: S2) -> &u32 {
52     let s = s;
53     s << &mut 3 //~ ERROR cannot return value referencing temporary value
54 }
55
56 fn return_dangling_pointer_method(s: S2) -> &u32 {
57     let s = s;
58     s.shl(&mut 3) //~ ERROR cannot return value referencing temporary value
59 }
60
61 fn return_dangling_pointer_ufcs(s: S2) -> &u32 {
62     let s = s;
63     S2::shl(s, &mut 3) //~ ERROR cannot return value referencing temporary value
64 }
65
66 fn main() {
67     let s = S { marker: PhantomData, bomb: None };
68     let _inherent_dp = return_dangling_pointer_inherent(s);
69     let _coerce_dp = return_dangling_pointer_coerce(s);
70     let _unary_dp = return_dangling_pointer_unary_op(s);
71     let _binary_dp = return_dangling_pointer_binary_op(s);
72     let _method_dp = return_dangling_pointer_method(s);
73     let _ufcs_dp = return_dangling_pointer_ufcs(s);
74 }