Skip to content

Commit

Permalink
Check that #[pointee] is applied only to generic arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Brezak committed Aug 20, 2024
1 parent 4d5b3b1 commit fcdb5f0
Show file tree
Hide file tree
Showing 5 changed files with 49 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 parameters
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,
}
8 changes: 8 additions & 0 deletions tests/ui/deriving/deriving-smart-pointer-neg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ struct NoMaybeSized<'a, #[pointee] T> {
ptr: &'a T,
}

#[derive(SmartPointer)]
#[repr(transparent)]
struct PointeeOnField<'a, #[pointee] T: ?Sized> {
#[pointee]
//~^ ERROR: the `#[pointee]` attribute may only be used on generic parameters
ptr: &'a T
}

// However, reordering attributes should work nevertheless.
#[repr(transparent)]
#[derive(SmartPointer)]
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/deriving/deriving-smart-pointer-neg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ error: `derive(SmartPointer)` requires T to be marked `?Sized`
LL | struct NoMaybeSized<'a, #[pointee] T> {
| ^

error: the `#[pointee]` attribute may only be used on generic parameters
--> $DIR/deriving-smart-pointer-neg.rs:49:5
|
LL | #[pointee]
| ^^^^^^^^^^

error[E0392]: lifetime parameter `'a` is never used
--> $DIR/deriving-smart-pointer-neg.rs:22:16
|
Expand Down Expand Up @@ -76,6 +82,6 @@ LL | struct NoFieldUnit<'a, #[pointee] T: ?Sized>();
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`

error: aborting due to 10 previous errors
error: aborting due to 11 previous errors

For more information about this error, try `rustc --explain E0392`.

0 comments on commit fcdb5f0

Please sign in to comment.