mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-12 16:34:05 -04:00
Rewrite the two derive macros for `Zeroable` using `syn`. One positive
side effect of this change is that tuple structs are now supported by
them. Additionally, syntax errors and the error emitted when trying to
use one of the derive macros on an `enum` are improved. Otherwise no
functional changes intended.
For example:
#[derive(Zeroable)]
enum Num {
A(u32),
B(i32),
}
Produced this error before this commit:
error: no rules expected keyword `enum`
--> tests/ui/compile-fail/zeroable/enum.rs:5:1
|
5 | enum Num {
| ^^^^ no rules expected this token in macro call
|
note: while trying to match keyword `struct`
--> src/macros.rs
|
| $vis:vis struct $name:ident
| ^^^^^^
Now the error is:
error: cannot derive `Zeroable` for an enum
--> tests/ui/compile-fail/zeroable/enum.rs:5:1
|
5 | enum Num {
| ^^^^
error: cannot derive `Zeroable` for an enum
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Benno Lossin <lossin@kernel.org>
31 lines
844 B
Rust
31 lines
844 B
Rust
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
|
|
use std::fmt::Display;
|
|
|
|
use proc_macro2::TokenStream;
|
|
use syn::{spanned::Spanned, Error};
|
|
|
|
pub(crate) struct DiagCtxt(TokenStream);
|
|
pub(crate) struct ErrorGuaranteed(());
|
|
|
|
impl DiagCtxt {
|
|
pub(crate) fn error(&mut self, span: impl Spanned, msg: impl Display) -> ErrorGuaranteed {
|
|
let error = Error::new(span.span(), msg);
|
|
self.0.extend(error.into_compile_error());
|
|
ErrorGuaranteed(())
|
|
}
|
|
|
|
pub(crate) fn with(
|
|
fun: impl FnOnce(&mut DiagCtxt) -> Result<TokenStream, ErrorGuaranteed>,
|
|
) -> TokenStream {
|
|
let mut dcx = Self(TokenStream::new());
|
|
match fun(&mut dcx) {
|
|
Ok(mut stream) => {
|
|
stream.extend(dcx.0);
|
|
stream
|
|
}
|
|
Err(ErrorGuaranteed(())) => dcx.0,
|
|
}
|
|
}
|
|
}
|