Rust: #![inline(never)] vs #[inline(never)]

Created on 22 Feb 2020  路  4Comments  路  Source: rust-lang/rust

Consider this code:

#![inline(never)]

pub fn f(x: u64) -> u64 {
    x + 1
}

pub fn g(x: u64) -> u64 {
    f(x) + 1
}

and the resulting assembly:

playground::f:
    leaq    1(%rdi), %rax
    retq

playground::g:
    leaq    2(%rdi), %rax
    retq

I know I was hoping for a lot there, and I know that #[inline(never)] is just a suggestion. That said, given that I didn't get a warning here I would have expected removing the ! to produce the same output. But

#[inline(never)]
pub fn f(x: u64) -> u64 {
    x + 1
}

pub fn g(x: u64) -> u64 {
    f(x) + 1
}

yields

playground::f:
    leaq    1(%rdi), %rax
    retq

playground::g:
    pushq   %rax
    callq   *playground::f@GOTPCREL(%rip)
    addq    $1, %rax
    popq    %rcx
    retq

If #![inline(never)] is supposed to work, it would be nice to have it work here. If it is not, it would be nice to get a warning lint.

(It would also be nice if inline(never) was a requirement, not just a suggestion. But that's a separate issue, I think.)

Meta

Built on the playground in release mode. Same behavior with rustc stable 1.41.0 and 1.43.0-nightly (2020-02-20 2c462a2f776b899d4674)

A-attributes C-bug

All 4 comments

Many built-in attributes are not validated that they appear in correct locations, particularly for the codegen attributes. I don't think there is a single issue tracking this, but I think #55112 is probably the closest?

There are a number of other issues reporting similar confusion about attributes being ignored: #54044, #47725, #54584, #53901, #64734, #65128. The lint control attributes (allow/warn/deny) are particularly gnarly since they can go anywhere, but don't always do what people expect (#61552, #59144 among others).

64734 is especially relevant because, as discussion there immediately identified, #[inline] is in the same boat as #[repr]: we already have a pretty comprehensive lint for these specific attributes being misapplied, it only forgets to check attribute of the root module specifically. As @varkor already said in that thread, we should really fix that.

Thanks for the insights!

It would occasionally be convenient to be able to say "inline nothing in this module", if that was easy to provide. I wrote #![inline(never)] on purpose in some code we were trying to debug / analyze.

Such a feature might not be hard to provide, but we need fix the bug in attribute checking anyway (since it affects other attributes too), and IMO we should do that first. I'd like at least one release where rustc highlights potential pre-existing typos such as

///! This is a simple crate with a few functions, one of which is `#[inline]`.
#![some_crate_attribute]

#![inline] // typo: a ! snuck in
fn foo() {
    // ...
}

// ... other functions, which aren't intended to be #[inline]
Was this page helpful?
0 / 5 - 0 ratings