Skip to content

Commit 91ed689

Browse files
committed
rustc_pass_by_value lint: add test on custom types
1 parent 4c3e330 commit 91ed689

File tree

4 files changed

+82
-22
lines changed

4 files changed

+82
-22
lines changed

compiler/rustc_lint/src/pass_by_value.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare_tool_lint! {
1111
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to always be passed by value.
1212
/// This is usually used for types that are thin wrappers around references, so there is no benefit to an extra
1313
/// layer of indirection. (Example: `Ty` which is a reference to a `TyS`)
14+
/// This lint relies on `#[rustc_diagnostic_item]` being available for the target.
1415
pub rustc::PASS_BY_VALUE,
1516
Warn,
1617
"pass by reference of a type flagged as `#[rustc_pass_by_value]`",

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,20 +1070,15 @@ impl CheckAttrVisitor<'_> {
10701070
/// Warns against some misuses of `#[pass_by_value]`
10711071
fn check_pass_by_value(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
10721072
match target {
1073-
Target::Struct
1074-
| Target::Enum
1075-
| Target::Union
1076-
| Target::Trait
1077-
| Target::TraitAlias
1078-
| Target::TyAlias => true,
1073+
Target::Struct | Target::Enum | Target::TyAlias => true,
10791074
_ => {
10801075
self.tcx
10811076
.sess
10821077
.struct_span_err(
10831078
attr.span,
1084-
"`pass_by_value` attribute should be applied to a struct, enum, trait or type alias.",
1079+
"`pass_by_value` attribute should be applied to a struct, enum or type alias.",
10851080
)
1086-
.span_label(*span, "is not a struct, enum, trait or type alias")
1081+
.span_label(*span, "is not a struct, enum or type alias")
10871082
.emit();
10881083
false
10891084
}

src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// compile-flags: -Z unstable-options
22

3+
#![feature(rustc_attrs)]
34
#![feature(rustc_private)]
45
#![deny(rustc::pass_by_value)]
56
#![allow(unused)]
@@ -61,4 +62,43 @@ impl Foo {
6162
//~^^ ERROR passing `TyCtxt<'_>` by reference
6263
}
6364

65+
#[rustc_diagnostic_item = "CustomEnum"]
66+
#[rustc_pass_by_value]
67+
enum CustomEnum {
68+
A,
69+
B,
70+
}
71+
72+
impl CustomEnum {
73+
fn test(
74+
value: CustomEnum,
75+
reference: &CustomEnum, //~ ERROR passing `CustomEnum` by reference
76+
) {
77+
}
78+
}
79+
80+
#[rustc_diagnostic_item = "CustomStruct"]
81+
#[rustc_pass_by_value]
82+
struct CustomStruct {
83+
s: u8,
84+
}
85+
86+
#[rustc_diagnostic_item = "CustomAlias"]
87+
#[rustc_pass_by_value]
88+
type CustomAlias<'a> = &'a CustomStruct; //~ ERROR passing `CustomStruct` by reference
89+
90+
impl CustomStruct {
91+
fn test(
92+
value: CustomStruct,
93+
reference: &CustomStruct, //~ ERROR passing `CustomStruct` by reference
94+
) {
95+
}
96+
97+
fn test_alias(
98+
value: CustomAlias,
99+
reference: &CustomAlias, //~ ERROR passing `CustomAlias<>` by reference
100+
) {
101+
}
102+
}
103+
64104
fn main() {}
Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,104 @@
11
error: passing `Ty<'_>` by reference
2-
--> $DIR/rustc_pass_by_value.rs:13:13
2+
--> $DIR/rustc_pass_by_value.rs:14:13
33
|
44
LL | ty_ref: &Ty<'_>,
55
| ^^^^^^^ help: try passing by value: `Ty<'_>`
66
|
77
note: the lint level is defined here
8-
--> $DIR/rustc_pass_by_value.rs:4:9
8+
--> $DIR/rustc_pass_by_value.rs:5:9
99
|
1010
LL | #![deny(rustc::pass_by_value)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

1313
error: passing `TyCtxt<'_>` by reference
14-
--> $DIR/rustc_pass_by_value.rs:15:18
14+
--> $DIR/rustc_pass_by_value.rs:16:18
1515
|
1616
LL | ty_ctxt_ref: &TyCtxt<'_>,
1717
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`
1818

1919
error: passing `Ty<'_>` by reference
20-
--> $DIR/rustc_pass_by_value.rs:19:28
20+
--> $DIR/rustc_pass_by_value.rs:20:28
2121
|
2222
LL | fn ty_multi_ref(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>) {}
2323
| ^^^^^^^ help: try passing by value: `Ty<'_>`
2424

2525
error: passing `TyCtxt<'_>` by reference
26-
--> $DIR/rustc_pass_by_value.rs:19:55
26+
--> $DIR/rustc_pass_by_value.rs:20:55
2727
|
2828
LL | fn ty_multi_ref(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>) {}
2929
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`
3030

3131
error: passing `Ty<'_>` by reference
32-
--> $DIR/rustc_pass_by_value.rs:26:17
32+
--> $DIR/rustc_pass_by_value.rs:27:17
3333
|
3434
LL | ty_ref: &Ty<'_>,
3535
| ^^^^^^^ help: try passing by value: `Ty<'_>`
3636

3737
error: passing `TyCtxt<'_>` by reference
38-
--> $DIR/rustc_pass_by_value.rs:28:22
38+
--> $DIR/rustc_pass_by_value.rs:29:22
3939
|
4040
LL | ty_ctxt_ref: &TyCtxt<'_>,
4141
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`
4242

4343
error: passing `Ty<'_>` by reference
44-
--> $DIR/rustc_pass_by_value.rs:31:41
44+
--> $DIR/rustc_pass_by_value.rs:32:41
4545
|
4646
LL | fn ty_multi_ref_in_trait(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>);
4747
| ^^^^^^^ help: try passing by value: `Ty<'_>`
4848

4949
error: passing `TyCtxt<'_>` by reference
50-
--> $DIR/rustc_pass_by_value.rs:31:68
50+
--> $DIR/rustc_pass_by_value.rs:32:68
5151
|
5252
LL | fn ty_multi_ref_in_trait(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>);
5353
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`
5454

5555
error: passing `Ty<'_>` by reference
56-
--> $DIR/rustc_pass_by_value.rs:53:17
56+
--> $DIR/rustc_pass_by_value.rs:54:17
5757
|
5858
LL | ty_ref: &Ty<'_>,
5959
| ^^^^^^^ help: try passing by value: `Ty<'_>`
6060

6161
error: passing `TyCtxt<'_>` by reference
62-
--> $DIR/rustc_pass_by_value.rs:55:22
62+
--> $DIR/rustc_pass_by_value.rs:56:22
6363
|
6464
LL | ty_ctxt_ref: &TyCtxt<'_>,
6565
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`
6666

6767
error: passing `Ty<'_>` by reference
68-
--> $DIR/rustc_pass_by_value.rs:59:38
68+
--> $DIR/rustc_pass_by_value.rs:60:38
6969
|
7070
LL | fn ty_multi_ref_assoc(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>) {}
7171
| ^^^^^^^ help: try passing by value: `Ty<'_>`
7272

7373
error: passing `TyCtxt<'_>` by reference
74-
--> $DIR/rustc_pass_by_value.rs:59:65
74+
--> $DIR/rustc_pass_by_value.rs:60:65
7575
|
7676
LL | fn ty_multi_ref_assoc(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>) {}
7777
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`
7878

79-
error: aborting due to 12 previous errors
79+
error: passing `CustomEnum` by reference
80+
--> $DIR/rustc_pass_by_value.rs:75:20
81+
|
82+
LL | reference: &CustomEnum,
83+
| ^^^^^^^^^^^ help: try passing by value: `CustomEnum`
84+
85+
error: passing `CustomStruct` by reference
86+
--> $DIR/rustc_pass_by_value.rs:88:24
87+
|
88+
LL | type CustomAlias<'a> = &'a CustomStruct;
89+
| ^^^^^^^^^^^^^^^^ help: try passing by value: `CustomStruct`
90+
91+
error: passing `CustomStruct` by reference
92+
--> $DIR/rustc_pass_by_value.rs:93:20
93+
|
94+
LL | reference: &CustomStruct,
95+
| ^^^^^^^^^^^^^ help: try passing by value: `CustomStruct`
96+
97+
error: passing `CustomAlias<>` by reference
98+
--> $DIR/rustc_pass_by_value.rs:99:20
99+
|
100+
LL | reference: &CustomAlias,
101+
| ^^^^^^^^^^^^ help: try passing by value: `CustomAlias<>`
102+
103+
error: aborting due to 16 previous errors
80104

0 commit comments

Comments
 (0)