|  | 
|  | 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: ; Function Attrs: | 
|  | 75 | +    // CHECK-NOT: cold | 
|  | 76 | +    // CHECK: define | 
|  | 77 | +    fn trait_fn_overridden(&self) {} | 
|  | 78 | +} | 
0 commit comments