Skip to content

Commit 8c9bfaa

Browse files
committed
Stabilize format_args_capture
Works as expected, and there are widespread reports of success with it, as well as interest in it.
1 parent 89c3d84 commit 8c9bfaa

File tree

18 files changed

+20
-118
lines changed

18 files changed

+20
-118
lines changed

compiler/rustc_borrowck/src/lib.rs

+1-1
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

+10-26
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

+1-1
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

+1-1
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

+2
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

-3
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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)]

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
#![feature(fmt_internals)]
106106
#![feature(fn_traits)]
107107
#![feature(inherent_ascii_escape)]
108-
#![feature(format_args_capture)]
108+
#![cfg_attr(bootstrap, feature(format_args_capture))]
109109
#![feature(inplace_iteration)]
110110
#![feature(iter_advance_by)]
111111
#![feature(iter_zip)]

src/doc/unstable-book/src/library-features/format-args-capture.md

-47
This file was deleted.

src/test/ui/fmt/feature-gate-format-args-capture.rs

-6
This file was deleted.

src/test/ui/fmt/feature-gate-format-args-capture.stderr

-18
This file was deleted.

src/test/ui/fmt/format-args-capture-macro-hygiene.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(format_args_capture)]
2-
31
fn main() {
42
format!(concat!("{foo}")); //~ ERROR: there is no argument named `foo`
53
format!(concat!("{ba", "r} {}"), 1); //~ ERROR: there is no argument named `bar`

src/test/ui/fmt/format-args-capture-missing-variables.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(format_args_capture)]
2-
31
fn main() {
42
format!("{} {foo} {} {bar} {}", 1, 2, 3);
53
//~^ ERROR: cannot find value `foo` in this scope

src/test/ui/fmt/format-args-capture.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// run-pass
2-
#![feature(format_args_capture)]
32
#![feature(cfg_panic)]
43

54
fn main() {

src/test/ui/fmt/ifmt-bad-arg.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,20 @@ error: there is no argument named `foo`
6464
LL | format!("{} {foo} {} {bar} {}", 1, 2, 3);
6565
| ^^^^^
6666
|
67-
= help: if you intended to capture `foo` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
6867

6968
error: there is no argument named `bar`
7069
--> $DIR/ifmt-bad-arg.rs:27:26
7170
|
7271
LL | format!("{} {foo} {} {bar} {}", 1, 2, 3);
7372
| ^^^^^
7473
|
75-
= help: if you intended to capture `bar` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
7674

7775
error: there is no argument named `foo`
7876
--> $DIR/ifmt-bad-arg.rs:31:14
7977
|
8078
LL | format!("{foo}");
8179
| ^^^^^
8280
|
83-
= help: if you intended to capture `foo` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
8481

8582
error: multiple unused formatting arguments
8683
--> $DIR/ifmt-bad-arg.rs:32:17
@@ -162,7 +159,6 @@ error: there is no argument named `valueb`
162159
LL | format!("{valuea} {valueb}", valuea=5, valuec=7);
163160
| ^^^^^^^^
164161
|
165-
= help: if you intended to capture `valueb` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
166162

167163
error: named argument never used
168164
--> $DIR/ifmt-bad-arg.rs:45:51
@@ -214,7 +210,6 @@ error: there is no argument named `foo`
214210
LL | {foo}
215211
| ^^^^^
216212
|
217-
= help: if you intended to capture `foo` from the surrounding scope, add `#![feature(format_args_capture)]` to the crate attributes
218213

219214
error: invalid format string: expected `'}'`, found `'t'`
220215
--> $DIR/ifmt-bad-arg.rs:75:1

0 commit comments

Comments
 (0)