summaryrefslogtreecommitdiffstats
path: root/tests/ui-fulldeps/internal-lints/rustc_pass_by_value.rs
blob: 10bab2d889a60a00e6b5d85a73be2c2baa41601a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// compile-flags: -Z unstable-options

#![feature(rustc_attrs)]
#![feature(rustc_private)]
#![deny(rustc::pass_by_value)]
#![allow(unused)]

extern crate rustc_middle;

use rustc_middle::ty::{Ty, TyCtxt};

fn ty_by_ref(
    ty_val: Ty<'_>,
    ty_ref: &Ty<'_>, //~ ERROR passing `Ty<'_>` by reference
    ty_ctxt_val: TyCtxt<'_>,
    ty_ctxt_ref: &TyCtxt<'_>, //~ ERROR passing `TyCtxt<'_>` by reference
) {
}

fn ty_multi_ref(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>) {}
//~^ ERROR passing `Ty<'_>` by reference
//~^^ ERROR passing `TyCtxt<'_>` by reference

trait T {
    fn ty_by_ref_in_trait(
        ty_val: Ty<'_>,
        ty_ref: &Ty<'_>, //~ ERROR passing `Ty<'_>` by reference
        ty_ctxt_val: TyCtxt<'_>,
        ty_ctxt_ref: &TyCtxt<'_>, //~ ERROR passing `TyCtxt<'_>` by reference
    );

    fn ty_multi_ref_in_trait(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>);
    //~^ ERROR passing `Ty<'_>` by reference
    //~^^ ERROR passing `TyCtxt<'_>` by reference
}

struct Foo;

impl T for Foo {
    fn ty_by_ref_in_trait(
        ty_val: Ty<'_>,
        ty_ref: &Ty<'_>,
        ty_ctxt_val: TyCtxt<'_>,
        ty_ctxt_ref: &TyCtxt<'_>,
    ) {
    }

    fn ty_multi_ref_in_trait(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>) {}
}

impl Foo {
    fn ty_by_ref_assoc(
        ty_val: Ty<'_>,
        ty_ref: &Ty<'_>, //~ ERROR passing `Ty<'_>` by reference
        ty_ctxt_val: TyCtxt<'_>,
        ty_ctxt_ref: &TyCtxt<'_>, //~ ERROR passing `TyCtxt<'_>` by reference
    ) {
    }

    fn ty_multi_ref_assoc(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>) {}
    //~^ ERROR passing `Ty<'_>` by reference
    //~^^ ERROR passing `TyCtxt<'_>` by reference
}

#[rustc_pass_by_value]
enum CustomEnum {
    A,
    B,
}

impl CustomEnum {
    fn test(
        value: CustomEnum,
        reference: &CustomEnum, //~ ERROR passing `CustomEnum` by reference
    ) {
    }
}

#[rustc_pass_by_value]
struct CustomStruct {
    s: u8,
}

#[rustc_pass_by_value]
type CustomAlias<'a> = &'a CustomStruct; //~ ERROR passing `CustomStruct` by reference

impl CustomStruct {
    fn test(
        value: CustomStruct,
        reference: &CustomStruct, //~ ERROR passing `CustomStruct` by reference
    ) {
    }

    fn test_alias(
        value: CustomAlias,
        reference: &CustomAlias, //~ ERROR passing `CustomAlias<'_>` by reference
    ) {
    }
}

#[rustc_pass_by_value]
struct WithParameters<T, const N: usize, M = u32> {
    slice: [T; N],
    m: M,
}

impl<T> WithParameters<T, 1> {
    fn test<'a>(
        value: WithParameters<T, 1>,
        reference: &'a WithParameters<T, 1>, //~ ERROR passing `WithParameters<T, 1>` by reference
        reference_with_m: &WithParameters<T, 1, u32>, //~ ERROR passing `WithParameters<T, 1, u32>` by reference
    ) -> &'a WithParameters<T, 1> {
        //~^ ERROR passing `WithParameters<T, 1>` by reference
        reference as &WithParameters<_, 1> //~ ERROR passing `WithParameters<_, 1>` by reference
    }
}

fn main() {}