Skip to content

Commit

Permalink
derive(SmartPointer): register helper attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Aug 12, 2024
1 parent 899eb03 commit f254ee6
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 19 deletions.
6 changes: 0 additions & 6 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
EncodeCrossCrate::No, coroutines, experimental!(coroutines)
),

// `#[pointee]` attribute to designate the pointee type in SmartPointer derive-macro
gated!(
pointee, Normal, template!(Word), ErrorFollowing,
EncodeCrossCrate::No, derive_smart_pointer, experimental!(pointee)
),

// RFC 3543
// `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
gated!(
Expand Down
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
32 changes: 32 additions & 0 deletions tests/ui/deriving/auxiliary/another-proc-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//@ force-host
//@ no-prefer-dynamic

#![crate_type = "proc-macro"]
#![feature(proc_macro_quote)]

extern crate proc_macro;

use proc_macro::{quote, TokenStream};

#[proc_macro_derive(AnotherMacro, attributes(pointee))]
pub fn derive(_input: TokenStream) -> TokenStream {
quote! {
const _: () = {
const ANOTHER_MACRO_DERIVED: () = ();
};
}
.into()
}

#[proc_macro_attribute]
pub fn pointee(
_attr: proc_macro::TokenStream,
_item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
quote! {
const _: () = {
const ANOTHER_MACRO_ATTR_DERIVED: () = ();
};
}
.into()
}
25 changes: 25 additions & 0 deletions tests/ui/deriving/built-in-proc-macro-scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ check-pass
//@ aux-build: another-proc-macro.rs
//@ compile-flags: -Zunpretty=expanded

#![feature(derive_smart_pointer)]

#[macro_use]
extern crate another_proc_macro;

use another_proc_macro::{pointee, AnotherMacro};

#[derive(core::marker::SmartPointer)]
#[repr(transparent)]
pub struct Ptr<'a, #[pointee] T: ?Sized> {
data: &'a mut T,
}

#[pointee]
fn f() {}

#[derive(AnotherMacro)]
#[pointee]
struct MyStruct;

fn main() {}
43 changes: 43 additions & 0 deletions tests/ui/deriving/built-in-proc-macro-scope.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![feature(prelude_import)]
#![no_std]
//@ check-pass
//@ aux-build: another-proc-macro.rs
//@ compile-flags: -Zunpretty=expanded

#![feature(derive_smart_pointer)]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;

#[macro_use]
extern crate another_proc_macro;

use another_proc_macro::{pointee, AnotherMacro};

#[repr(transparent)]
pub struct Ptr<'a, #[pointee] T: ?Sized> {
data: &'a mut T,
}
#[automatically_derived]
impl<'a, T: ?Sized + ::core::marker::Unsize<__S>, __S: ?Sized>
::core::ops::DispatchFromDyn<Ptr<'a, __S>> for Ptr<'a, T> {
}
#[automatically_derived]
impl<'a, T: ?Sized + ::core::marker::Unsize<__S>, __S: ?Sized>
::core::ops::CoerceUnsized<Ptr<'a, __S>> for Ptr<'a, T> {
}



const _: () =
{
const ANOTHER_MACRO_ATTR_DERIVED: () = ();
};
#[pointee]
struct MyStruct;
const _: () =
{
const ANOTHER_MACRO_DERIVED: () = ();
};
fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::marker::SmartPointer; //~ ERROR use of unstable library feature 'derive
#[derive(SmartPointer)] //~ ERROR use of unstable library feature 'derive_smart_pointer'
#[repr(transparent)]
struct MyPointer<'a, #[pointee] T: ?Sized> {
//~^ ERROR the `#[pointee]` attribute is an experimental feature
ptr: &'a T,
}

Expand Down
12 changes: 1 addition & 11 deletions tests/ui/feature-gates/feature-gate-derive-smart-pointer.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ LL | #[derive(SmartPointer)]
= help: add `#![feature(derive_smart_pointer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: the `#[pointee]` attribute is an experimental feature
--> $DIR/feature-gate-derive-smart-pointer.rs:5:22
|
LL | struct MyPointer<'a, #[pointee] T: ?Sized> {
| ^^^^^^^^^^
|
= note: see issue #123430 <https://github.com/rust-lang/rust/issues/123430> for more information
= help: add `#![feature(derive_smart_pointer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'derive_smart_pointer'
--> $DIR/feature-gate-derive-smart-pointer.rs:1:5
|
Expand All @@ -28,6 +18,6 @@ LL | use std::marker::SmartPointer;
= help: add `#![feature(derive_smart_pointer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

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

0 comments on commit f254ee6

Please sign in to comment.