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: https://github.com/rust-lang/rust/issues/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 https://github.com/rust-lang/rust/issues/67984#issuecomment-801394736 ). - 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.
This commit is contained in:
commit
c26746af5a
22 changed files with 83 additions and 167 deletions
|
@ -3,7 +3,7 @@
|
|||
#![feature(bool_to_option)]
|
||||
#![feature(box_patterns)]
|
||||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(format_args_capture)]
|
||||
#![cfg_attr(bootstrap, feature(format_args_capture))]
|
||||
#![feature(in_band_lifetimes)]
|
||||
#![feature(iter_zip)]
|
||||
#![feature(let_else)]
|
||||
|
|
|
@ -527,17 +527,9 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||
self.verify_arg_type(Exact(idx), ty)
|
||||
}
|
||||
None => {
|
||||
let capture_feature_enabled = self
|
||||
.ecx
|
||||
.ecfg
|
||||
.features
|
||||
.map_or(false, |features| features.format_args_capture);
|
||||
|
||||
// For the moment capturing variables from format strings expanded from macros is
|
||||
// disabled (see RFC #2795)
|
||||
let can_capture = capture_feature_enabled && self.is_literal;
|
||||
|
||||
if can_capture {
|
||||
if self.is_literal {
|
||||
// Treat this name as a variable to capture from the surrounding scope
|
||||
let idx = self.args.len();
|
||||
self.arg_types.push(Vec::new());
|
||||
|
@ -559,23 +551,15 @@ impl<'a, 'b> Context<'a, 'b> {
|
|||
};
|
||||
let mut err = self.ecx.struct_span_err(sp, &msg[..]);
|
||||
|
||||
if capture_feature_enabled && !self.is_literal {
|
||||
err.note(&format!(
|
||||
"did you intend to capture a variable `{}` from \
|
||||
the surrounding scope?",
|
||||
name
|
||||
));
|
||||
err.note(
|
||||
"to avoid ambiguity, `format_args!` cannot capture variables \
|
||||
when the format string is expanded from a macro",
|
||||
);
|
||||
} else if self.ecx.parse_sess().unstable_features.is_nightly_build() {
|
||||
err.help(&format!(
|
||||
"if you intended to capture `{}` from the surrounding scope, add \
|
||||
`#![feature(format_args_capture)]` to the crate attributes",
|
||||
name
|
||||
));
|
||||
}
|
||||
err.note(&format!(
|
||||
"did you intend to capture a variable `{}` from \
|
||||
the surrounding scope?",
|
||||
name
|
||||
));
|
||||
err.note(
|
||||
"to avoid ambiguity, `format_args!` cannot capture variables \
|
||||
when the format string is expanded from a macro",
|
||||
);
|
||||
|
||||
err.emit();
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(backtrace)]
|
||||
#![feature(if_let_guard)]
|
||||
#![feature(format_args_capture)]
|
||||
#![cfg_attr(bootstrap, feature(format_args_capture))]
|
||||
#![feature(iter_zip)]
|
||||
#![feature(let_else)]
|
||||
#![feature(nll)]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(decl_macro)]
|
||||
#![feature(destructuring_assignment)]
|
||||
#![feature(format_args_capture)]
|
||||
#![cfg_attr(bootstrap, feature(format_args_capture))]
|
||||
#![feature(if_let_guard)]
|
||||
#![feature(iter_zip)]
|
||||
#![feature(let_else)]
|
||||
|
|
|
@ -301,6 +301,8 @@ declare_features! (
|
|||
(accepted, relaxed_struct_unsize, "1.58.0", Some(81793), None),
|
||||
/// Allows dereferencing raw pointers during const eval.
|
||||
(accepted, const_raw_ptr_deref, "1.58.0", Some(51911), None),
|
||||
/// Allows capturing variables in scope using format_args!
|
||||
(accepted, format_args_capture, "1.58.0", Some(67984), None),
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// feature-group-end: accepted features
|
||||
|
|
|
@ -539,9 +539,6 @@ declare_features! (
|
|||
/// Be more precise when looking for live drops in a const context.
|
||||
(active, const_precise_live_drops, "1.46.0", Some(73255), None),
|
||||
|
||||
/// Allows capturing variables in scope using format_args!
|
||||
(active, format_args_capture, "1.46.0", Some(67984), None),
|
||||
|
||||
/// Allows `if let` guard in match arms.
|
||||
(active, if_let_guard, "1.47.0", Some(51114), None),
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
#![feature(bool_to_option)]
|
||||
#![feature(box_patterns)]
|
||||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(format_args_capture)]
|
||||
#![cfg_attr(bootstrap, feature(format_args_capture))]
|
||||
#![feature(iter_order_by)]
|
||||
#![feature(iter_zip)]
|
||||
#![feature(never_type)]
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
||||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(in_band_lifetimes)]
|
||||
#![feature(format_args_capture)]
|
||||
#![cfg_attr(bootstrap, feature(format_args_capture))]
|
||||
#![feature(iter_zip)]
|
||||
#![feature(map_try_insert)]
|
||||
#![feature(min_specialization)]
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
#![feature(drain_filter)]
|
||||
#![feature(bool_to_option)]
|
||||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(format_args_capture)]
|
||||
#![cfg_attr(bootstrap, feature(format_args_capture))]
|
||||
#![feature(iter_zip)]
|
||||
#![feature(let_else)]
|
||||
#![feature(never_type)]
|
||||
|
|
|
@ -58,7 +58,7 @@ This API is completely unstable and subject to change.
|
|||
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
||||
#![feature(bool_to_option)]
|
||||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(format_args_capture)]
|
||||
#![cfg_attr(bootstrap, feature(format_args_capture))]
|
||||
#![feature(if_let_guard)]
|
||||
#![feature(in_band_lifetimes)]
|
||||
#![feature(is_sorted)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue