Disallow unsafe in derive

This commit is contained in:
carbotaniuman 2024-04-23 07:50:53 -05:00
parent 67f5dd1ef1
commit c4de986afa
6 changed files with 37 additions and 2 deletions

View File

@ -110,6 +110,9 @@ builtin_macros_derive_path_args_list = traits in `#[derive(...)]` don't accept a
builtin_macros_derive_path_args_value = traits in `#[derive(...)]` don't accept values builtin_macros_derive_path_args_value = traits in `#[derive(...)]` don't accept values
.suggestion = remove the value .suggestion = remove the value
builtin_macros_derive_unsafe_path = traits in `#[derive(...)]` don't accept `unsafe(...)`
.suggestion = remove the `unsafe(...)`
builtin_macros_env_not_defined = environment variable `{$var}` not defined at compile time builtin_macros_env_not_defined = environment variable `{$var}` not defined at compile time
.cargo = Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead .cargo = Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead
.custom = use `std::env::var({$var_expr})` to read the variable at run time .custom = use `std::env::var({$var_expr})` to read the variable at run time

View File

@ -2,7 +2,7 @@
use crate::errors; use crate::errors;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::{GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind}; use rustc_ast::{GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind, Unsafe};
use rustc_expand::base::{ use rustc_expand::base::{
Annotatable, DeriveResolution, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier, Annotatable, DeriveResolution, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier,
}; };
@ -60,6 +60,7 @@ fn expand(
// Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the // Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the
// paths. // paths.
report_path_args(sess, meta); report_path_args(sess, meta);
report_unsafe_args(sess, meta);
meta.path.clone() meta.path.clone()
}) })
.map(|path| DeriveResolution { .map(|path| DeriveResolution {
@ -159,3 +160,12 @@ fn report_path_args(sess: &Session, meta: &ast::MetaItem) {
} }
} }
} }
fn report_unsafe_args(sess: &Session, meta: &ast::MetaItem) {
match meta.unsafety {
Unsafe::Yes(span) => {
sess.dcx().emit_err(errors::DeriveUnsafePath { span });
}
Unsafe::No => {}
}
}

View File

@ -295,6 +295,13 @@ pub(crate) struct DerivePathArgsValue {
pub(crate) span: Span, pub(crate) span: Span,
} }
#[derive(Diagnostic)]
#[diag(builtin_macros_derive_unsafe_path)]
pub(crate) struct DeriveUnsafePath {
#[primary_span]
pub(crate) span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(builtin_macros_no_default_variant)] #[diag(builtin_macros_no_default_variant)]
#[help] #[help]

View File

@ -778,7 +778,8 @@ fn expand_invoc(
if let SyntaxExtensionKind::Derive(..) = ext { if let SyntaxExtensionKind::Derive(..) = ext {
self.gate_proc_macro_input(&item); self.gate_proc_macro_input(&item);
} }
// FIX THIS LATER // The `MetaItem` representing the trait to derive can't
// have an unsafe around it (as of now).
let meta = ast::MetaItem { let meta = ast::MetaItem {
unsafety: ast::Unsafe::No, unsafety: ast::Unsafe::No,
kind: MetaItemKind::Word, kind: MetaItemKind::Word,

View File

@ -0,0 +1,6 @@
#![feature(unsafe_attributes)]
#[derive(unsafe(Debug))] //~ ERROR: traits in `#[derive(...)]` don't accept `unsafe(...)`
struct Foo;
fn main() {}

View File

@ -0,0 +1,8 @@
error: traits in `#[derive(...)]` don't accept `unsafe(...)`
--> $DIR/derive-unsafe-attributes.rs:3:10
|
LL | #[derive(unsafe(Debug))]
| ^^^^^^
error: aborting due to 1 previous error