Skip to content

Commit

Permalink
Auto merge of #15172 - HKalbasi:mir, r=HKalbasi
Browse files Browse the repository at this point in the history
Support #[derive_const(Trait)]

This is a nightly feature used in the standard library.
  • Loading branch information
bors committed Jun 29, 2023
2 parents 70a01fe + f53f923 commit 70ea88e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
38 changes: 38 additions & 0 deletions crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,44 @@ impl < > core::cmp::Eq for Command< > where {}"#]],
);
}

#[test]
fn test_partial_eq_expand_with_derive_const() {
// FIXME: actually expand with const
check(
r#"
//- minicore: derive, eq
#[derive_const(PartialEq, Eq)]
enum Command {
Move { x: i32, y: i32 },
Do(&'static str),
Jump,
}
"#,
expect![[r#"
#[derive_const(PartialEq, Eq)]
enum Command {
Move { x: i32, y: i32 },
Do(&'static str),
Jump,
}
impl < > core::cmp::PartialEq for Command< > where {
fn eq(&self , other: &Self ) -> bool {
match (self , other) {
(Command::Move {
x: x_self, y: y_self,
}
, Command::Move {
x: x_other, y: y_other,
}
)=>x_self.eq(x_other) && y_self.eq(y_other), (Command::Do(f0_self, ), Command::Do(f0_other, ))=>f0_self.eq(f0_other), (Command::Jump, Command::Jump)=>true , _unused=>false
}
}
}
impl < > core::cmp::Eq for Command< > where {}"#]],
);
}

#[test]
fn test_partial_ord_expand() {
check(
Expand Down
4 changes: 3 additions & 1 deletion crates/hir-expand/src/builtin_attr_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! register_builtin {

impl BuiltinAttrExpander {
pub fn is_derive(self) -> bool {
matches!(self, BuiltinAttrExpander::Derive)
matches!(self, BuiltinAttrExpander::Derive | BuiltinAttrExpander::DeriveConst)
}
pub fn is_test(self) -> bool {
matches!(self, BuiltinAttrExpander::Test)
Expand All @@ -50,6 +50,8 @@ register_builtin! {
(cfg_accessible, CfgAccessible) => dummy_attr_expand,
(cfg_eval, CfgEval) => dummy_attr_expand,
(derive, Derive) => derive_attr_expand,
// derive const is equivalent to derive for our proposes.
(derive_const, DeriveConst) => derive_attr_expand,
(global_allocator, GlobalAllocator) => dummy_attr_expand,
(test, Test) => dummy_attr_expand,
(test_case, TestCase) => dummy_attr_expand
Expand Down
1 change: 1 addition & 0 deletions crates/hir-expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ pub mod known {
cfg_eval,
crate_type,
derive,
derive_const,
global_allocator,
no_core,
no_std,
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/tests/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ struct Foo;
at deprecated
at derive macro derive
at derive(…)
at derive_const macro derive_const
at doc = "…"
at doc(alias = "…")
at doc(hidden)
Expand Down
41 changes: 23 additions & 18 deletions crates/test-utils/src/minicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,11 @@ mod macros {
pub macro derive($item:item) {
/* compiler built-in */
}

#[rustc_builtin_macro]
pub macro derive_const($item:item) {
/* compiler built-in */
}
}
// endregion:derive

Expand Down Expand Up @@ -1378,24 +1383,24 @@ pub mod error {
pub mod prelude {
pub mod v1 {
pub use crate::{
clone::Clone, // :clone
cmp::{Eq, PartialEq}, // :eq
cmp::{Ord, PartialOrd}, // :ord
convert::AsRef, // :as_ref
convert::{From, Into}, // :from
default::Default, // :default
iter::{IntoIterator, Iterator}, // :iterator
macros::builtin::derive, // :derive
marker::Copy, // :copy
marker::Send, // :send
marker::Sized, // :sized
marker::Sync, // :sync
mem::drop, // :drop
ops::Drop, // :drop
ops::{Fn, FnMut, FnOnce}, // :fn
option::Option::{self, None, Some}, // :option
panic, // :panic
result::Result::{self, Err, Ok}, // :result
clone::Clone, // :clone
cmp::{Eq, PartialEq}, // :eq
cmp::{Ord, PartialOrd}, // :ord
convert::AsRef, // :as_ref
convert::{From, Into}, // :from
default::Default, // :default
iter::{IntoIterator, Iterator}, // :iterator
macros::builtin::{derive, derive_const}, // :derive
marker::Copy, // :copy
marker::Send, // :send
marker::Sized, // :sized
marker::Sync, // :sync
mem::drop, // :drop
ops::Drop, // :drop
ops::{Fn, FnMut, FnOnce}, // :fn
option::Option::{self, None, Some}, // :option
panic, // :panic
result::Result::{self, Err, Ok}, // :result
};
}

Expand Down

0 comments on commit 70ea88e

Please sign in to comment.