]> git.lizzy.rs Git - rust.git/commit
Auto merge of #107257 - inquisitivecrystal:ffi-attr, r=davidtwco
authorbors <bors@rust-lang.org>
Wed, 1 Feb 2023 11:37:24 +0000 (11:37 +0000)
committerbors <bors@rust-lang.org>
Wed, 1 Feb 2023 11:37:24 +0000 (11:37 +0000)
commit11d96b59307b1702fffe871bfc2d0145d070881e
treeaf26139253d2807f229208f8301714afc803c306
parent3b639486c17e9144a9176382ecb2a0b801263935
parentbc23e9aa4c78066043be246a47627746341480dd
Auto merge of #107257 - inquisitivecrystal:ffi-attr, r=davidtwco

Strengthen validation of FFI attributes

Previously, `codegen_attrs` validated the attributes `#[ffi_pure]`, `#[ffi_const]`, and `#[ffi_returns_twice]` to make sure that they were only used on foreign functions. However, this validation was insufficient in two ways:

1. `codegen_attrs` only sees items for which code must be generated, so it was unable to raise errors when the attribute was incorrectly applied to macros and the like.
2. the validation code only checked that the item with the attr was foreign, but not that it was a foreign function, allowing these attributes to be applied to foreign statics as well.

This PR moves the validation to `check_attr`, which sees all items. It additionally changes the validation to ensure that the attribute's target is `Target::ForeignFunction`, only allowing the attributes on foreign functions and not foreign statics. Because these attributes are unstable, there is no risk for backwards compatibility. The changes also ending up making the code much easier to read.

This PR is best reviewed commit by commit. Additionally, I was considering moving the tests to the `attribute` subdirectory, to get them out of the general UI directory. I could do that as part of this PR or a follow-up, as the reviewer prefers.

CC: #58328, #58329
compiler/rustc_codegen_ssa/src/codegen_attrs.rs
compiler/rustc_error_messages/locales/en-US/passes.ftl
compiler/rustc_passes/src/check_attr.rs
compiler/rustc_passes/src/errors.rs