Prepare for removal of safe_packed_borrows lint#55
Conversation
546dea4 to
2872f1e
Compare
|
In the following code, #[repr(packed)]
pub struct X {
pub inner: u16,
}
#[forbid(unaligned_references)]
fn _f(this: &X) {
let _ = &this.inner;
//~^ERROR reference to packed field is unaligned
}However, if you use a macro that generates similar code, like this: macro_rules! pin_project {
(
$(#[$attrs:meta])*
pub struct $ident:ident {
$(
$(#[$pin:ident])?
$field_vis:vis $field:ident: $field_ty:ty
),+ $(,)?
}
) => {
$(#[$attrs])*
pub struct $ident {
$(
$field_vis $field: $field_ty
),+
}
const _: () = {
// #[forbid(safe_packed_borrows)]
#[forbid(unaligned_references)]
fn __assert_not_repr_packed(this: &$ident) {
$(
let _ = &this.$field;
)+
}
};
};
}No error occurs on packed types (you just get a warning about safe_packed_borrows): pin_project_lite::pin_project! {
//~^WARN borrow of packed field is unsafe and requires unsafe function or block (error E0133)
// The above warning is by safe_packed_borrows and unaligned_references doesn't seem to be working.
#[repr(packed)]
pub struct Y {
pub inner: u16
}
}I guess this is related to the difference in the span between the cc @RalfJung @Aaron1011: Do you know why this doesn't work? Or is there a way to work around this? |
|
@taiki-e so what is the expected behavior here? I am confused by your example. Erroring on I have no idea how the scoping of |
|
There is a flag |
Sorry about that. I updated the example. (Hopefully, it will be easier to understand.)
Yes. The problem is that if the external macro ( |
Using |
Yes it makes sense now, and I am glad that that mysterious flag helps. :) |
Enable report_in_external_macro in unaligned_references Fixes an issue where `unaligned_references` is not triggered in external macros, unlike `safe_packed_borrows`. Also, given that this lint is planned to eventually change to hard error (rust-lang#82525), it would make sense for this lint to be triggered for external macros as well. See taiki-e/pin-project-lite#55 (comment) and taiki-e/pin-project-lite#55 (comment) for more. r? `@RalfJung`
91ee109 to
c4dcbad
Compare
c4dcbad to
3f011ef
Compare
|
bors r+ rust-lang/rust#82587 has been merged and unaligned_references now works as I expected 🎉 |
|
Awesome, and good catch. :) |
|
Build succeeded: |
321: Prepare for removal of safe_packed_borrows lint r=taiki-e a=taiki-e Same as taiki-e/pin-project-lite#55, but, pin-project's safety no longer depends on this, so there is no need to rush release. Co-authored-by: Taiki Endo <te316e89@gmail.com>
pin-project-lite is using
safe_packed_borrowslint for checkingrepr(packed)types. (See taiki-e/pin-project#34 for more)As described in #26, lint-based tricks aren't perfect, but they're much better than nothing.
safe_packed_borrowsis planned to be removed in favor ofunaligned_references(rust-lang/rust#82525), so I would like to switch tounaligned_references. (However, actually, enable both lint asunaligned_referencesdoes not exist in older compilers.)