Skip to content

Commit 9157927

Browse files
committed
Auto merge of #15006 - HKalbasi:format-panic, r=HKalbasi
Fix unwrap on None in expanding format args fix #15002
2 parents dcab226 + 30e16e2 commit 9157927

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,44 @@ fn main() {
207207
);
208208
}
209209

210+
#[test]
211+
fn regression_15002() {
212+
check(
213+
r#"
214+
#[rustc_builtin_macro]
215+
macro_rules! format_args {
216+
($fmt:expr) => ({ /* compiler built-in */ });
217+
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
218+
}
219+
220+
fn main() {
221+
format_args!(x = 2);
222+
format_args!(x =);
223+
format_args!(x =, x = 2);
224+
format_args!("{}", x =);
225+
format_args!(=, "{}", x =);
226+
format_args!(x = 2, "{}", 5);
227+
}
228+
"#,
229+
expect![[r##"
230+
#[rustc_builtin_macro]
231+
macro_rules! format_args {
232+
($fmt:expr) => ({ /* compiler built-in */ });
233+
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
234+
}
235+
236+
fn main() {
237+
/* error: no rule matches input tokens */;
238+
/* error: no rule matches input tokens */;
239+
/* error: no rule matches input tokens */;
240+
/* error: no rule matches input tokens */::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::Argument::new(&(), ::core::fmt::Display::fmt), ]);
241+
/* error: no rule matches input tokens */;
242+
::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::Argument::new(&(5), ::core::fmt::Display::fmt), ]);
243+
}
244+
"##]],
245+
);
246+
}
247+
210248
#[test]
211249
fn test_format_args_expand_with_comma_exprs() {
212250
check(

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,6 @@ fn format_args_expand_general(
262262
let expand_error =
263263
ExpandResult::new(tt::Subtree::empty(), mbe::ExpandError::NoMatchingRule.into());
264264

265-
if args.is_empty() {
266-
return expand_error;
267-
}
268265
let mut key_args = FxHashMap::default();
269266
let mut args = args.into_iter().filter_map(|mut arg| {
270267
// Remove `key =`.
@@ -281,7 +278,9 @@ fn format_args_expand_general(
281278
Some(arg)
282279
}).collect::<Vec<_>>().into_iter();
283280
// ^^^^^^^ we need this collect, to enforce the side effect of the filter_map closure (building the `key_args`)
284-
let format_subtree = args.next().unwrap();
281+
let Some(format_subtree) = args.next() else {
282+
return expand_error;
283+
};
285284
let format_string = (|| {
286285
let token_tree = format_subtree.token_trees.get(0)?;
287286
match token_tree {

0 commit comments

Comments
 (0)