]> git.lizzy.rs Git - rust.git/commitdiff
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)
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
1  2 
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

Simple merge