Skip to content

Commit 15646bf

Browse files
committed
Slightly refactor a bit of macro error handling
1 parent 0ff317b commit 15646bf

File tree

4 files changed

+61
-98
lines changed

4 files changed

+61
-98
lines changed

crates/objc2/src/macros/declare_class.rs

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,33 +1180,6 @@ macro_rules! __convert_result {
11801180
#[doc(hidden)]
11811181
#[macro_export]
11821182
macro_rules! __declare_class_register_out {
1183-
// #[method(dealloc)]
1184-
{
1185-
($builder:ident)
1186-
($($qualifiers:tt)*)
1187-
($name:ident)
1188-
($($__ret:ty)?)
1189-
($__body:block)
1190-
1191-
($builder_method:ident)
1192-
($__receiver:expr)
1193-
($__receiver_ty:ty)
1194-
($($__params_prefix:tt)*)
1195-
($($params_rest:tt)*)
1196-
1197-
(#[method(dealloc)])
1198-
()
1199-
() // No optional
1200-
($($m_checked:tt)*)
1201-
} => {
1202-
$crate::__extract_and_apply_cfg_attributes! {
1203-
@($($m_checked)*)
1204-
@($crate::__macro_helpers::compile_error!(
1205-
"`#[method(dealloc)]` is not supported. Implement `Drop` for the type instead"
1206-
))
1207-
}
1208-
};
1209-
12101183
// #[method(...)]
12111184
{
12121185
($builder:ident)
@@ -1223,12 +1196,15 @@ macro_rules! __declare_class_register_out {
12231196

12241197
(#[method($($sel:tt)*)])
12251198
()
1226-
() // No optional
1199+
($($m_optional:tt)*)
12271200
($($m_checked:tt)*)
12281201
} => {
12291202
$crate::__extract_and_apply_cfg_attributes! {
12301203
@($($m_checked)*)
12311204
@(
1205+
$crate::__declare_class_invalid_selectors!(#[method($($sel)*)]);
1206+
$crate::__extern_methods_no_optional!($($m_optional)*);
1207+
12321208
$builder.$builder_method(
12331209
$crate::sel!($($sel)*),
12341210
Self::$name as $crate::__fn_ptr! {
@@ -1257,14 +1233,17 @@ macro_rules! __declare_class_register_out {
12571233

12581234
(#[method_id($($sel:tt)*)])
12591235
() // Retain semantics unsupported in declare_class!
1260-
() // No optional
1236+
($($m_optional:tt)*)
12611237
($($m_checked:tt)*)
12621238
} => {
12631239
$crate::__extract_and_apply_cfg_attributes! {
12641240
@($($m_checked)*)
12651241
@(
1242+
$crate::__declare_class_invalid_selectors!(#[method_id($($sel)*)]);
1243+
$crate::__extern_methods_no_optional!($($m_optional)*);
1244+
12661245
$builder.$builder_method(
1267-
$crate::__get_method_id_sel!($($sel)*),
1246+
$crate::sel!($($sel)*),
12681247
Self::$name as $crate::__fn_ptr! {
12691248
($($qualifiers)*)
12701249
(_, _,)
@@ -1274,65 +1253,56 @@ macro_rules! __declare_class_register_out {
12741253
)
12751254
}
12761255
};
1277-
1278-
// #[optional]
1279-
{
1280-
($builder:ident)
1281-
($($qualifiers:tt)*)
1282-
($name:ident)
1283-
($($__ret:ty)?)
1284-
($__body:block)
1285-
1286-
($builder_method:ident)
1287-
($__receiver:expr)
1288-
($__receiver_ty:ty)
1289-
($($__params_prefix:tt)*)
1290-
($($params_rest:tt)*)
1291-
1292-
($($m_method:tt)*)
1293-
($($retain_semantics:tt)*)
1294-
($($m_optional:tt)*)
1295-
($($m_checked:tt)*)
1296-
} => {
1297-
$crate::__macro_helpers::compile_error!("`#[optional]` is only supported in `extern_protocol!`")
1298-
};
12991256
}
13001257

13011258
#[doc(hidden)]
13021259
#[macro_export]
1303-
macro_rules! __get_method_id_sel {
1304-
(alloc) => {
1260+
macro_rules! __declare_class_invalid_selectors {
1261+
(#[method(dealloc)]) => {
1262+
$crate::__macro_helpers::compile_error!(
1263+
"`#[method(dealloc)]` is not supported. Implement `Drop` for the type instead"
1264+
)
1265+
};
1266+
(#[method_id(dealloc)]) => {
1267+
$crate::__macro_helpers::compile_error!(
1268+
"`#[method_id(dealloc)]` is not supported. Implement `Drop` for the type instead"
1269+
)
1270+
};
1271+
(#[method_id(alloc)]) => {
13051272
$crate::__macro_helpers::compile_error!($crate::__macro_helpers::concat!(
13061273
"`#[method_id(alloc)]` is not supported. ",
13071274
"Use `#[method(alloc)]` and do the memory management yourself",
13081275
))
13091276
};
1310-
(retain) => {
1277+
(#[method_id(retain)]) => {
13111278
$crate::__macro_helpers::compile_error!($crate::__macro_helpers::concat!(
13121279
"`#[method_id(retain)]` is not supported. ",
13131280
"Use `#[method(retain)]` and do the memory management yourself",
13141281
))
13151282
};
1316-
(release) => {
1283+
(#[method_id(release)]) => {
13171284
$crate::__macro_helpers::compile_error!($crate::__macro_helpers::concat!(
13181285
"`#[method_id(release)]` is not supported. ",
13191286
"Use `#[method(release)]` and do the memory management yourself",
13201287
))
13211288
};
1322-
(autorelease) => {
1289+
(#[method_id(autorelease)]) => {
13231290
$crate::__macro_helpers::compile_error!($crate::__macro_helpers::concat!(
13241291
"`#[method_id(autorelease)]` is not supported. ",
13251292
"Use `#[method(autorelease)]` and do the memory management yourself",
13261293
))
13271294
};
1328-
(dealloc) => {
1329-
$crate::__macro_helpers::compile_error!($crate::__macro_helpers::concat!(
1330-
"`#[method_id(dealloc)]` is not supported. ",
1331-
"Add an instance variable with a `Drop` impl to the class instead",
1332-
))
1333-
};
1334-
($($t:tt)*) => {
1335-
$crate::sel!($($t)*)
1295+
(#[$method_or_method_id:ident($($sel:tt)*)]) => {};
1296+
}
1297+
1298+
#[doc(hidden)]
1299+
#[macro_export]
1300+
macro_rules! __declare_class_no_optional {
1301+
() => {};
1302+
(#[optional]) => {
1303+
$crate::__macro_helpers::compile_error!(
1304+
"`#[optional]` is only supported in `extern_protocol!`"
1305+
)
13361306
};
13371307
}
13381308

crates/objc2/src/macros/extern_methods.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,16 @@ macro_rules! __extern_methods_method_out {
302302

303303
(#[method($($sel:tt)*)])
304304
()
305-
() // No `optional`
305+
($($m_optional:tt)*)
306306
($($m_checked:tt)*)
307307
} => {
308308
$($m_checked)*
309309
$($function_start)*
310310
where
311311
$($where : $bound,)*
312312
{
313+
$crate::__extern_methods_no_optional!($($m_optional)*);
314+
313315
#[allow(unused_unsafe)]
314316
unsafe {
315317
$crate::__method_msg_send! {
@@ -337,14 +339,16 @@ macro_rules! __extern_methods_method_out {
337339

338340
(#[method_id($($sel:tt)*)])
339341
($($retain_semantics:tt)*)
340-
() // No `optional`
342+
($($m_optional:tt)*)
341343
($($m_checked:tt)*)
342344
} => {
343345
$($m_checked)*
344346
$($function_start)*
345347
where
346348
$($where : $bound,)*
347349
{
350+
$crate::__extern_methods_no_optional!($($m_optional)*);
351+
348352
#[allow(unused_unsafe)]
349353
unsafe {
350354
$crate::__method_msg_send_id! {
@@ -359,26 +363,15 @@ macro_rules! __extern_methods_method_out {
359363
}
360364
}
361365
};
366+
}
362367

363-
// #[optional]
364-
{
365-
($($function_start:tt)*)
366-
($($where:ty : $bound:path ,)*)
367-
368-
($__builder_method:ident)
369-
($receiver:expr)
370-
($__receiver_ty:ty)
371-
($($__params_prefix:tt)*)
372-
($($params_rest:tt)*)
373-
374-
($($m_method:tt)*)
375-
($($retain_semantics:tt)*)
376-
($($m_optional:tt)*)
377-
($($m_checked:tt)*)
378-
} => {
379-
$($m_checked)*
380-
$($function_start)* {
381-
$crate::__macro_helpers::compile_error!("`#[optional]` is only supported in `extern_protocol!`")
382-
}
368+
#[doc(hidden)]
369+
#[macro_export]
370+
macro_rules! __extern_methods_no_optional {
371+
() => {};
372+
(#[optional]) => {
373+
$crate::__macro_helpers::compile_error!(
374+
"`#[optional]` is only supported in `extern_protocol!`"
375+
)
383376
};
384377
}

crates/test-ui/ui/declare_class_invalid_syntax.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ error: `#[method_id(alloc)]` is not supported. Use `#[method(alloc)]` and do the
136136
| | );
137137
| |_^
138138
|
139-
= note: this error originates in the macro `$crate::__get_method_id_sel` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
139+
= note: this error originates in the macro `$crate::__declare_class_invalid_selectors` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
140140

141141
error: `#[method_id(retain)]` is not supported. Use `#[method(retain)]` and do the memory management yourself
142142
--> ui/declare_class_invalid_syntax.rs
@@ -150,7 +150,7 @@ error: `#[method_id(retain)]` is not supported. Use `#[method(retain)]` and do t
150150
| | );
151151
| |_^
152152
|
153-
= note: this error originates in the macro `$crate::__get_method_id_sel` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
153+
= note: this error originates in the macro `$crate::__declare_class_invalid_selectors` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
154154

155155
error: `#[method_id(release)]` is not supported. Use `#[method(release)]` and do the memory management yourself
156156
--> ui/declare_class_invalid_syntax.rs
@@ -164,7 +164,7 @@ error: `#[method_id(release)]` is not supported. Use `#[method(release)]` and do
164164
| | );
165165
| |_^
166166
|
167-
= note: this error originates in the macro `$crate::__get_method_id_sel` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
167+
= note: this error originates in the macro `$crate::__declare_class_invalid_selectors` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
168168

169169
error: `#[method_id(autorelease)]` is not supported. Use `#[method(autorelease)]` and do the memory management yourself
170170
--> ui/declare_class_invalid_syntax.rs
@@ -178,9 +178,9 @@ error: `#[method_id(autorelease)]` is not supported. Use `#[method(autorelease)]
178178
| | );
179179
| |_^
180180
|
181-
= note: this error originates in the macro `$crate::__get_method_id_sel` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
181+
= note: this error originates in the macro `$crate::__declare_class_invalid_selectors` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
182182

183-
error: `#[method_id(dealloc)]` is not supported. Add an instance variable with a `Drop` impl to the class instead
183+
error: `#[method_id(dealloc)]` is not supported. Implement `Drop` for the type instead
184184
--> ui/declare_class_invalid_syntax.rs
185185
|
186186
| / declare_class!(
@@ -192,7 +192,7 @@ error: `#[method_id(dealloc)]` is not supported. Add an instance variable with a
192192
| | );
193193
| |_^
194194
|
195-
= note: this error originates in the macro `$crate::__get_method_id_sel` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
195+
= note: this error originates in the macro `$crate::__declare_class_invalid_selectors` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
196196

197197
error: `#[method(dealloc)]` is not supported. Implement `Drop` for the type instead
198198
--> ui/declare_class_invalid_syntax.rs
@@ -206,7 +206,7 @@ error: `#[method(dealloc)]` is not supported. Implement `Drop` for the type inst
206206
| | );
207207
| |_^
208208
|
209-
= note: this error originates in the macro `$crate::__declare_class_register_out` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
209+
= note: this error originates in the macro `$crate::__declare_class_invalid_selectors` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
210210

211211
error: an inner attribute is not permitted in this context
212212
--> ui/declare_class_invalid_syntax.rs

crates/test-ui/ui/wrong_optional.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: `#[optional]` is only supported in `extern_protocol!`
1010
| | );
1111
| |_^
1212
|
13-
= note: this error originates in the macro `$crate::__extern_methods_method_out` which comes from the expansion of the macro `extern_methods` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
= note: this error originates in the macro `$crate::__extern_methods_no_optional` which comes from the expansion of the macro `extern_methods` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

1515
error: `#[optional]` is only supported in `extern_protocol!`
1616
--> ui/wrong_optional.rs
@@ -24,7 +24,7 @@ error: `#[optional]` is only supported in `extern_protocol!`
2424
| | );
2525
| |_^
2626
|
27-
= note: this error originates in the macro `$crate::__extern_methods_method_out` which comes from the expansion of the macro `extern_methods` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
= note: this error originates in the macro `$crate::__extern_methods_no_optional` which comes from the expansion of the macro `extern_methods` (in Nightly builds, run with -Z macro-backtrace for more info)
2828

2929
error: `#[optional]` is only supported in `extern_protocol!`
3030
--> ui/wrong_optional.rs
@@ -38,7 +38,7 @@ error: `#[optional]` is only supported in `extern_protocol!`
3838
| | );
3939
| |_^
4040
|
41-
= note: this error originates in the macro `$crate::__declare_class_register_out` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
41+
= note: this error originates in the macro `$crate::__extern_methods_no_optional` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
4242

4343
error: `#[optional]` is only supported in `extern_protocol!`
4444
--> ui/wrong_optional.rs
@@ -52,4 +52,4 @@ error: `#[optional]` is only supported in `extern_protocol!`
5252
| | );
5353
| |_^
5454
|
55-
= note: this error originates in the macro `$crate::__declare_class_register_out` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)
55+
= note: this error originates in the macro `$crate::__extern_methods_no_optional` which comes from the expansion of the macro `declare_class` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)