Skip to content

Commit e9d8b3f

Browse files
authored
Rollup merge of rust-lang#147476 - ehuss:cold-attribute-test, r=chenyukang
Add a test for the cold attribute This adds a test for the cold attribute to verify that it actually does something, and that it applies correctly in all the positions it is expected to work.
2 parents 2654b69 + 849feea commit e9d8b3f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Checks that the cold attribute adds the llvm cold attribute.
2+
//
3+
//@ reference: attributes.codegen.cold.intro
4+
//@ reference: attributes.codegen.cold.trait
5+
//@ edition:2024
6+
//@ compile-flags: -Copt-level=0
7+
8+
#![crate_type = "lib"]
9+
10+
// CHECK-LABEL: ; cold_attribute::free_function
11+
// CHECK-NEXT: Function Attrs: cold {{.*}}
12+
#[cold]
13+
pub fn free_function() {}
14+
15+
// CHECK-LABEL: ; cold_attribute::async_block
16+
// CHECK-NEXT: Function Attrs: cold {{.*}}
17+
#[cold]
18+
pub async fn async_block() {
19+
async fn x(f: impl Future<Output = ()>) {
20+
f.await;
21+
}
22+
x(
23+
// CHECK-LABEL: ; cold_attribute::async_block::{{{{closure}}}}::{{{{closure}}}}
24+
// CHECK-NEXT: Function Attrs: cold {{.*}}
25+
#[cold]
26+
async {},
27+
)
28+
.await;
29+
}
30+
31+
pub fn closure() {
32+
fn x(f: impl Fn()) {
33+
f()
34+
}
35+
x(
36+
// CHECK-LABEL: ; cold_attribute::closure::{{{{closure}}}}
37+
// CHECK-NEXT: Function Attrs: cold {{.*}}
38+
#[cold]
39+
|| {},
40+
);
41+
}
42+
43+
pub struct S;
44+
45+
impl S {
46+
// CHECK-LABEL: ; cold_attribute::S::method
47+
// CHECK-NEXT: Function Attrs: cold {{.*}}
48+
#[cold]
49+
pub fn method(&self) {}
50+
}
51+
52+
pub trait Trait {
53+
// CHECK-LABEL: ; cold_attribute::Trait::trait_fn
54+
// CHECK-NEXT: Function Attrs: cold {{.*}}
55+
#[cold]
56+
fn trait_fn(&self) {}
57+
58+
#[cold]
59+
fn trait_fn_overridden(&self) {}
60+
61+
fn impl_fn(&self);
62+
}
63+
64+
impl Trait for S {
65+
// CHECK-LABEL: ; <cold_attribute::S as cold_attribute::Trait>::impl_fn
66+
// CHECK-NEXT: Function Attrs: cold {{.*}}
67+
#[cold]
68+
fn impl_fn(&self) {
69+
self.trait_fn();
70+
}
71+
72+
// This does not have #[cold], and does not inherit the cold attribute from the trait.
73+
// CHECK-LABEL: ; <cold_attribute::S as cold_attribute::Trait>::trait_fn_overridden
74+
// CHECK-NEXT: ; Function Attrs: uwtable
75+
fn trait_fn_overridden(&self) {}
76+
}

0 commit comments

Comments
 (0)