如何抑制" drop_with_repr_extern"的警告细粒度?

时间:2016-03-19 17:01:33

标签: rust suppress-warnings

我目前正在尝试使用多线程代码,其性能受两个数据成员是否共享同一缓存行的影响。

为了避免错误共享,我需要在没有Rust编译器干扰的情况下指定struct的布局,因此我使用repr(C)。但是,同样的struct也实现了Drop,因此编译器会警告"不兼容性" repr(C)Drop,我不在乎。

然而,试图平息这种徒劳的警告已经证明了我的能力。

这是reduced example

#[repr(C)]
#[derive(Default, Debug)]
struct Simple<T> {
    item: T,
}

impl<T> Drop for Simple<T> {
    fn drop(&mut self) {}
}

fn main() {
    println!("{:?}", Simple::<u32>::default());
}

发出#[warn(drop_with_repr_extern)]

我已尝试指定#[allow(drop_with_repr_extern)]

  • at struct
  • at impl Drop
  • at mod

并且都没有奏效。只有板条箱级别的抑制工作才有效,而且非常严厉。

这导致我们:是否有更精细的方法来抑制此警告?

注意:欢迎使用更好的方法来确保两个数据成员分布在不同的缓存行上。但是它们不会自己构成答案。

1 个答案:

答案 0 :(得分:2)

原因是near the end of rustc_lint/builtin.rs

lint不会行走箱子,而是使用ctx.tcx.lang_items.drop_trait()查找箱子内的所有Drop特征实施。只有在走板条箱时才会拾取注释。我在this question中偶然发现了同样的问题。因此,除非有人更改lint以实际行走箱子并随身携带Drop impl,否则您需要注释整个箱子。