Skip to content

Commit

Permalink
Treat pointee as a macro attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Brezak committed Aug 6, 2024
1 parent 2ba4d07 commit d05c2fc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ builtin_macros_non_exhaustive_default = default variant must be exhaustive
.label = declared `#[non_exhaustive]` here
.help = consider a manual implementation of `Default`
builtin_macros_non_generic_pointee = the `#[pointee]` attribute may only be used on generic params
builtin_macros_non_unit_default = the `#[default]` attribute may only be used on unit enum variants
.help = consider a manual implementation of `Default`
Expand Down
25 changes: 25 additions & 0 deletions compiler/rustc_builtin_macros/src/deriving/smart_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use rustc_span::{Span, Symbol};
use smallvec::{smallvec, SmallVec};
use thin_vec::{thin_vec, ThinVec};

use crate::errors;

macro_rules! path {
($span:expr, $($part:ident)::*) => { vec![$(Ident::new(sym::$part, $span),)*] }
}
Expand All @@ -26,6 +28,8 @@ pub fn expand_deriving_smart_ptr(
push: &mut dyn FnMut(Annotatable),
_is_const: bool,
) {
item.visit_with(&mut DetectNonGenericPointeeAttr { cx });

let (name_ident, generics) = if let Annotatable::Item(aitem) = item
&& let ItemKind::Struct(struct_data, g) = &aitem.kind
{
Expand Down Expand Up @@ -377,3 +381,24 @@ impl<'a> ast::mut_visit::MutVisitor for TypeSubstitution<'a> {
}
}
}

struct DetectNonGenericPointeeAttr<'a, 'b> {
cx: &'a ExtCtxt<'b>,
}

impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonGenericPointeeAttr<'a, 'b> {
fn visit_attribute(&mut self, attr: &'a rustc_ast::Attribute) -> Self::Result {
if attr.has_name(sym::pointee) {
self.cx.dcx().emit_err(errors::NonGenericPointee { span: attr.span });
}
}

fn visit_generic_param(&mut self, param: &'a rustc_ast::GenericParam) -> Self::Result {
match param.kind {
GenericParamKind::Type { .. } => return,
GenericParamKind::Lifetime | GenericParamKind::Const { .. } => {
rustc_ast::visit::walk_generic_param(self, param)
}
}
}
}
7 changes: 7 additions & 0 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,3 +937,10 @@ pub struct NakedFunctionTestingAttribute {
#[label]
pub testing_span: Span,
}

#[derive(Diagnostic)]
#[diag(builtin_macros_non_generic_pointee)]
pub struct NonGenericPointee {
#[primary_span]
pub span: Span,
}
2 changes: 1 addition & 1 deletion library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ pub trait FnPtr: Copy + Clone {
}

/// Derive macro generating impls of traits related to smart pointers.
#[rustc_builtin_macro]
#[rustc_builtin_macro(SmartPointer, attributes(pointee))]
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize)]
#[unstable(feature = "derive_smart_pointer", issue = "123430")]
pub macro SmartPointer($item:item) {
Expand Down

0 comments on commit d05c2fc

Please sign in to comment.