Skip to content

Commit 0b350c4

Browse files
Back to tuple_tree. Not cleaned up yet.
1 parent 7920a8f commit 0b350c4

File tree

1 file changed

+123
-22
lines changed

1 file changed

+123
-22
lines changed

src/lib.rs

Lines changed: 123 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ macro_rules! unsafe_method {
269269
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator:tt } )? )?
270270
$self:expr, $fn:ident $(, $arg:expr )*
271271
) => {
272-
$crate::unsafe_method_internal!(
272+
$crate::unsafe_method_internal_check_self_etc!(
273273
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator } )? )?
274274
$self, $fn $(, $arg )*
275275
)
@@ -278,28 +278,40 @@ macro_rules! unsafe_method {
278278
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator:tt } )? )?
279279
$self:expr, $fn:ident $(, $arg:expr )*
280280
) => {
281-
$crate::unsafe_method_internal!(
281+
$crate::unsafe_method_internal_check_self_etc!(
282282
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator } )? )?
283283
$self, $fn $(, $arg )*
284284
)
285285
};
286286
}
287287

288+
/// ```compile_fail,E0133
289+
#[doc = include_str!("../violations_coverage/unsafe_method/some_args/arg.rs")]
290+
/// ```
291+
#[cfg(doctest)]
292+
pub const _: () = {};
293+
294+
/// ```compile_fail,E0133
295+
#[doc = include_str!("../violations_coverage/unsafe_method/some_args/self.rs")]
296+
/// ```
297+
#[cfg(doctest)]
298+
pub const _: () = {};
299+
288300
#[doc(hidden)]
289301
#[macro_export]
290-
macro_rules! unsafe_method_internal {
302+
macro_rules! unsafe_method_internal_check_self_etc {
291303
(
292-
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator:tt } )? )?
293304
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator:tt } )? )?
305+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator:tt } )? )?
294306
$self:expr, $fn:ident $(, $arg:expr )*
295307
) => {
296308
// See unsafe_fn for why here we enclose in (...) and not in {...}.
297309
(
298310
if false {
299311
if false {
300-
// This block "makes" an instance/owned value of the same type as $self. (Of
301-
// course, the instance is invalid - this is for compile-time checks only, hence
302-
// `if false {...}`.)
312+
// This block "makes" owned_receiver, an instance/owned value of the same type
313+
// as $self. (Of course, the instance is invalid - this is for compile-time
314+
// checks only, hence `if false {...}`.)
303315
//
304316
// Then we simulate invocation of the given method inside `unsafe {...}``, BUT
305317
// without evaluating the given $self expression inside that same `unsafe
@@ -339,16 +351,22 @@ macro_rules! unsafe_method_internal {
339351
// 2. #[deny(unused_unsafe)]
340352
let _ = unsafe { owned_receiver. $fn( $( $arg ),* ) };
341353
} else {
354+
// @TODO eliminate
342355
$(
343356
#[deny(unused_unsafe)]
344357
let _ = $arg;
345358
)*
346359
}
347360
unreachable!()
348361
} else {
349-
#[allow(unsafe_code)]
350-
// If $self includes `unsafe {...}`, but no ~allow_unsafe or ~expect_unsafe, that
351-
// would trigger "unused_unsafe". Let's notify the user:
362+
//compile_error!("TODO move to unsafe_method_internal_check_args_etc");
363+
$crate::unsafe_method_internal_check_args_etc!(
364+
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator } )? )?
365+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator } )? )?
366+
$self, $fn $(, $arg )*
367+
)
368+
/*#[allow(unsafe_code)]
369+
// Notify if $self includes `unsafe {...}`, but no ~allow_unsafe or ~expect_unsafe:
352370
#[deny(unused_unsafe)]
353371
$(
354372
$( { $allow_unsafe_empty_indicator } )?
@@ -358,24 +376,107 @@ macro_rules! unsafe_method_internal {
358376
$( { $expect_unsafe_empty_indicator } )?
359377
#[expect(unused_unsafe)]
360378
)?
361-
unsafe { $self. $fn ( $( $arg ),* ) }
379+
unsafe { $self. $fn ( $( $arg ),* ) }*/
362380
}
363381
)
364-
};
382+
}
365383
}
366384

367-
/// ```compile_fail,E0133
368-
#[doc = include_str!("../violations_coverage/unsafe_method/some_args/arg.rs")]
369-
/// ```
370-
#[cfg(doctest)]
371-
pub const _: () = {};
385+
#[doc(hidden)]
386+
#[macro_export]
387+
macro_rules! unsafe_method_internal_check_args_etc {
388+
(
389+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator:tt } )? )?
390+
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator:tt } )? )?
391+
$self:expr, $fn:ident $(, $arg:expr )+
392+
) => {({
393+
let tuple_tree =
394+
$crate::unsafe_fn_internal_build_tuple_tree!{ $($arg),+ };
372395

373-
/// ```compile_fail,E0133
374-
#[doc = include_str!("../violations_coverage/unsafe_method/some_args/self.rs")]
375-
/// ```
376-
#[cfg(doctest)]
377-
pub const _: () = {};
396+
$crate::unsafe_method_internal_build_accessors_check_args_call! {
397+
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator } )? )?
398+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator } )? )?
399+
$self,
400+
$fn,
401+
tuple_tree,
402+
( $( $arg ),* ),
403+
(0)
404+
}
405+
})};
406+
(
407+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator:tt } )? )?
408+
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator:tt } )? )?
409+
$self:expr, $fn:ident
410+
) => {({
411+
#[allow(unsafe_code)]
412+
// Notify if $self includes `unsafe {...}`, but no ~allow_unsafe or ~expect_unsafe:
413+
#[deny(unused_unsafe)]
414+
$(
415+
$( { $allow_unsafe_empty_indicator } )?
416+
#[allow(unused_unsafe)]
417+
)?
418+
$(
419+
$( { $expect_unsafe_empty_indicator } )?
420+
#[expect(unused_unsafe)]
421+
)?
422+
let result = unsafe { $self. $fn () };
423+
result
424+
})};
425+
}
378426

427+
#[doc(hidden)]
428+
#[macro_export]
429+
macro_rules! unsafe_method_internal_build_accessors_check_args_call {
430+
// Access tuple_tree parts and get ready to call the method:
431+
(
432+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator:tt } )? )?
433+
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator:tt } )? )?
434+
$self:expr, $fn:ident, $tuple_tree:ident,
435+
( $_first_arg:expr, $($other_arg:expr),+ ),
436+
$( ( $($accessor_part:tt),+
437+
)
438+
),*
439+
) => {
440+
$crate::unsafe_method_internal_build_accessors_check_args_call!{
441+
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator } )? )?
442+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator } )? )?
443+
$self, $fn, $tuple_tree, ( $($other_arg),+ ),
444+
// Insert a new accessor to front (left): 0.
445+
(0),
446+
$( // Prepend 1 to each supplied/existing accessor
447+
( 1, $($accessor_part),+ )
448+
),*
449+
}
450+
};
451+
// All accessors are ready. Call the function:
452+
(
453+
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator:tt } )? )?
454+
$( ~allow_unsafe $( { $allow_unsafe_empty_indicator:tt } )? )?
455+
$self:expr, $fn:ident, $tuple_tree:ident,
456+
( $_last_or_only_arg:expr ),
457+
$( ( $($accessor_part:tt),+
458+
)
459+
),*
460+
) => {({
461+
#[allow(unsafe_code)]
462+
#[deny(unused_unsafe)]
463+
$(
464+
$( { $allow_unsafe_empty_indicator } )?
465+
#[allow(unused_unsafe)]
466+
)?
467+
$(
468+
$( { $expect_unsafe_empty_indicator } )?
469+
#[expect(unused_unsafe)]
470+
)?
471+
let result = unsafe {
472+
$self. $fn( $(
473+
$crate::unsafe_fn_internal_access_tuple_tree_field!{ $tuple_tree, $($accessor_part),+ }
474+
),*
475+
)
476+
};
477+
result
478+
})};
479+
}
379480
//-------------
380481

381482
/// Set a value of a `static mut` variable or its (sub...-)field, but isolate `unsafe {...}` only to

0 commit comments

Comments
 (0)