Skip to content

Commit e2a0d0d

Browse files
authored
refactor: better handling for the lifetime extension issue (#5239)
1 parent d0fcdd3 commit e2a0d0d

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

crates/biome_formatter/src/builders.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2642,9 +2642,15 @@ impl<'a, Context> BestFitting<'a, Context> {
26422642
///
26432643
/// You're looking for a way to create a `BestFitting` object, use the `best_fitting![least_expanded, most_expanded]` macro.
26442644
///
2645+
/// This is intended to be only used in the `best_fitting!` macro. As we can't place tail
2646+
/// expressions in a block for temporary lifetime extension since Rust 2024, we can't use an
2647+
/// `unsafe` block in the macro. Thus, this function can't be marked as unsafe, but it shouldn't
2648+
/// be used from outside.
2649+
///
26452650
/// ## Safety
26462651
/// The slice must contain at least two variants.
2647-
pub unsafe fn from_arguments_unchecked(variants: Arguments<'a, Context>) -> Self {
2652+
#[doc(hidden)]
2653+
pub fn from_arguments_unchecked(variants: Arguments<'a, Context>) -> Self {
26482654
assert!(
26492655
variants.0.len() >= 2,
26502656
"Requires at least the least expanded and most expanded variants"

crates/biome_formatter/src/macros.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -329,23 +329,10 @@ macro_rules! format {
329329
#[macro_export]
330330
macro_rules! best_fitting {
331331
($least_expanded:expr, $($tail:expr),+ $(,)?) => {
332-
// FIXME: Using any block in this macro causes a "temporary value dropped while borrowed"
333-
// (E0716) error since Rust 2024 edition. It seems temporary lifetime extension is
334-
// not working correctly, so it is a compiler bug?
335-
$crate::macros::__macro_helper::best_fitting($crate::format_args!($least_expanded, $($tail),+))
332+
BestFitting::from_arguments_unchecked($crate::format_args!($least_expanded, $($tail),+))
336333
};
337334
}
338335

339-
#[doc(hidden)]
340-
pub mod __macro_helper {
341-
#[inline(always)]
342-
pub fn best_fitting<Context>(
343-
arguments: crate::Arguments<Context>,
344-
) -> crate::BestFitting<Context> {
345-
unsafe { crate::BestFitting::from_arguments_unchecked(arguments) }
346-
}
347-
}
348-
349336
#[cfg(test)]
350337
mod tests {
351338
use crate::prelude::*;

0 commit comments

Comments
 (0)