Skip to content

Commit c26746a

Browse files
committed
Auto merge of #90473 - joshtriplett:stabilize-format-args-capture, r=Mark-Simulacrum
stabilize format args capture Works as expected, and there are widespread reports of success with it, as well as interest in it. RFC: rust-lang/rfcs#2795 Tracking issue: #67984 Addressing items from the tracking issue: - We don't support capturing arguments from a non-literal format string like `format_args!(concat!(...))`. We could add that in a future enhancement, or we can decide that it isn't supported (as suggested in #67984 (comment) ). - I've updated the documentation. - `panic!` now supports capture as well. - There are potentially opportunities to further improve diagnostics for invalid usage, such as if it looks like the user tried to use an expression rather than a variable. However, such cases are all already caught and provide reasonable syntax errors now, and we can always provided even friendlier diagnostics in the future.
2 parents eab2d75 + afa719e commit c26746a

File tree

22 files changed

+83
-167
lines changed

22 files changed

+83
-167
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![feature(bool_to_option)]
44
#![feature(box_patterns)]
55
#![feature(crate_visibility_modifier)]
6-
#![feature(format_args_capture)]
6+
#![cfg_attr(bootstrap, feature(format_args_capture))]
77
#![feature(in_band_lifetimes)]
88
#![feature(iter_zip)]
99
#![feature(let_else)]

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,9 @@ impl<'a, 'b> Context<'a, 'b> {
527527
self.verify_arg_type(Exact(idx), ty)
528528
}
529529
None => {
530-
let capture_feature_enabled = self
531-
.ecx
532-
.ecfg
533-
.features
534-
.map_or(false, |features| features.format_args_capture);
535-
536530
// For the moment capturing variables from format strings expanded from macros is
537531
// disabled (see RFC #2795)
538-
let can_capture = capture_feature_enabled && self.is_literal;
539-
540-
if can_capture {
532+
if self.is_literal {
541533
// Treat this name as a variable to capture from the surrounding scope
542534
let idx = self.args.len();
543535
self.arg_types.push(Vec::new());
@@ -559,23 +551,15 @@ impl<'a, 'b> Context<'a, 'b> {
559551
};
560552
let mut err = self.ecx.struct_span_err(sp, &msg[..]);
561553

562-
if capture_feature_enabled && !self.is_literal {
563-
err.note(&format!(
564-
"did you intend to capture a variable `{}` from \
565-
the surrounding scope?",
566-
name
567-
));
568-
err.note(
569-
"to avoid ambiguity, `format_args!` cannot capture variables \
570-
when the format string is expanded from a macro",
571-
);
572-
} else if self.ecx.parse_sess().unstable_features.is_nightly_build() {
573-
err.help(&format!(
574-
"if you intended to capture `{}` from the surrounding scope, add \
575-
`#![feature(format_args_capture)]` to the crate attributes",
576-
name
577-
));
578-
}
554+
err.note(&format!(
555+
"did you intend to capture a variable `{}` from \
556+
the surrounding scope?",
557+
name
558+
));
559+
err.note(
560+
"to avoid ambiguity, `format_args!` cannot capture variables \
561+
when the format string is expanded from a macro",
562+
);
579563

580564
err.emit();
581565
}

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![feature(crate_visibility_modifier)]
77
#![feature(backtrace)]
88
#![feature(if_let_guard)]
9-
#![feature(format_args_capture)]
9+
#![cfg_attr(bootstrap, feature(format_args_capture))]
1010
#![feature(iter_zip)]
1111
#![feature(let_else)]
1212
#![feature(nll)]

compiler/rustc_expand/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(crate_visibility_modifier)]
22
#![feature(decl_macro)]
33
#![feature(destructuring_assignment)]
4-
#![feature(format_args_capture)]
4+
#![cfg_attr(bootstrap, feature(format_args_capture))]
55
#![feature(if_let_guard)]
66
#![feature(iter_zip)]
77
#![feature(let_else)]

compiler/rustc_feature/src/accepted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ declare_features! (
301301
(accepted, relaxed_struct_unsize, "1.58.0", Some(81793), None),
302302
/// Allows dereferencing raw pointers during const eval.
303303
(accepted, const_raw_ptr_deref, "1.58.0", Some(51911), None),
304+
/// Allows capturing variables in scope using format_args!
305+
(accepted, format_args_capture, "1.58.0", Some(67984), None),
304306

305307
// -------------------------------------------------------------------------
306308
// feature-group-end: accepted features

compiler/rustc_feature/src/active.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,6 @@ declare_features! (
539539
/// Be more precise when looking for live drops in a const context.
540540
(active, const_precise_live_drops, "1.46.0", Some(73255), None),
541541

542-
/// Allows capturing variables in scope using format_args!
543-
(active, format_args_capture, "1.46.0", Some(67984), None),
544-
545542
/// Allows `if let` guard in match arms.
546543
(active, if_let_guard, "1.47.0", Some(51114), None),
547544

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#![feature(bool_to_option)]
3131
#![feature(box_patterns)]
3232
#![feature(crate_visibility_modifier)]
33-
#![feature(format_args_capture)]
33+
#![cfg_attr(bootstrap, feature(format_args_capture))]
3434
#![feature(iter_order_by)]
3535
#![feature(iter_zip)]
3636
#![feature(never_type)]

compiler/rustc_passes/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(crate_visibility_modifier)]
99
#![feature(in_band_lifetimes)]
10-
#![feature(format_args_capture)]
10+
#![cfg_attr(bootstrap, feature(format_args_capture))]
1111
#![feature(iter_zip)]
1212
#![feature(map_try_insert)]
1313
#![feature(min_specialization)]

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#![feature(drain_filter)]
1414
#![feature(bool_to_option)]
1515
#![feature(crate_visibility_modifier)]
16-
#![feature(format_args_capture)]
16+
#![cfg_attr(bootstrap, feature(format_args_capture))]
1717
#![feature(iter_zip)]
1818
#![feature(let_else)]
1919
#![feature(never_type)]

compiler/rustc_typeck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ This API is completely unstable and subject to change.
5858
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
5959
#![feature(bool_to_option)]
6060
#![feature(crate_visibility_modifier)]
61-
#![feature(format_args_capture)]
61+
#![cfg_attr(bootstrap, feature(format_args_capture))]
6262
#![feature(if_let_guard)]
6363
#![feature(in_band_lifetimes)]
6464
#![feature(is_sorted)]

0 commit comments

Comments
 (0)