Skip to content

Commit b1745d5

Browse files
committed
[experiment] Remove format_args_nl!().
1 parent 4b87ed9 commit b1745d5

File tree

76 files changed

+269
-1551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+269
-1551
lines changed

compiler/rustc_builtin_macros/src/asm.rs

-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
534534
template_str,
535535
str_style,
536536
template_snippet,
537-
false,
538537
parse::ParseMode::InlineAsm,
539538
);
540539
parser.curarg = curarg;

compiler/rustc_builtin_macros/src/format.rs

+4-35
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,13 @@ fn parse_args<'a>(ecx: &mut ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<
153153
Ok(MacroInput { fmtstr, args, is_direct_literal })
154154
}
155155

156-
fn make_format_args(
157-
ecx: &mut ExtCtxt<'_>,
158-
input: MacroInput,
159-
append_newline: bool,
160-
) -> Result<FormatArgs, ()> {
156+
fn make_format_args(ecx: &mut ExtCtxt<'_>, input: MacroInput) -> Result<FormatArgs, ()> {
161157
let msg = "format argument must be a string literal";
162158
let unexpanded_fmt_span = input.fmtstr.span;
163159

164160
let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;
165161

166162
let (fmt_str, fmt_style, fmt_span) = match expr_to_spanned_string(ecx, efmt, msg) {
167-
Ok(mut fmt) if append_newline => {
168-
fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
169-
fmt
170-
}
171163
Ok(fmt) => fmt,
172164
Err(err) => {
173165
if let Some((mut err, suggested)) = err {
@@ -196,13 +188,7 @@ fn make_format_args(
196188

197189
let fmt_str = fmt_str.as_str(); // for the suggestions below
198190
let fmt_snippet = ecx.source_map().span_to_snippet(unexpanded_fmt_span).ok();
199-
let mut parser = parse::Parser::new(
200-
fmt_str,
201-
str_style,
202-
fmt_snippet,
203-
append_newline,
204-
parse::ParseMode::Format,
205-
);
191+
let mut parser = parse::Parser::new(fmt_str, str_style, fmt_snippet, parse::ParseMode::Format);
206192

207193
let mut pieces = Vec::new();
208194
while let Some(piece) = parser.next() {
@@ -831,16 +817,15 @@ fn report_invalid_references(
831817
e.emit();
832818
}
833819

834-
fn expand_format_args_impl<'cx>(
820+
pub fn expand_format_args<'cx>(
835821
ecx: &'cx mut ExtCtxt<'_>,
836822
mut sp: Span,
837823
tts: TokenStream,
838-
nl: bool,
839824
) -> Box<dyn base::MacResult + 'cx> {
840825
sp = ecx.with_def_site_ctxt(sp);
841826
match parse_args(ecx, sp, tts) {
842827
Ok(input) => {
843-
if let Ok(format_args) = make_format_args(ecx, input, nl) {
828+
if let Ok(format_args) = make_format_args(ecx, input) {
844829
MacEager::expr(ecx.expr(sp, ExprKind::FormatArgs(P(format_args))))
845830
} else {
846831
MacEager::expr(DummyResult::raw_expr(sp, true))
@@ -852,19 +837,3 @@ fn expand_format_args_impl<'cx>(
852837
}
853838
}
854839
}
855-
856-
pub fn expand_format_args<'cx>(
857-
ecx: &'cx mut ExtCtxt<'_>,
858-
sp: Span,
859-
tts: TokenStream,
860-
) -> Box<dyn base::MacResult + 'cx> {
861-
expand_format_args_impl(ecx, sp, tts, false)
862-
}
863-
864-
pub fn expand_format_args_nl<'cx>(
865-
ecx: &'cx mut ExtCtxt<'_>,
866-
sp: Span,
867-
tts: TokenStream,
868-
) -> Box<dyn base::MacResult + 'cx> {
869-
expand_format_args_impl(ecx, sp, tts, true)
870-
}

compiler/rustc_builtin_macros/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
8383
concat: concat::expand_concat,
8484
env: env::expand_env,
8585
file: source_util::expand_file,
86-
format_args_nl: format::expand_format_args_nl,
8786
format_args: format::expand_format_args,
8887
const_format_args: format::expand_format_args,
8988
global_asm: asm::expand_global_asm,

compiler/rustc_lint/src/non_fmt_panic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn check_panic_str<'tcx>(
244244
Err(_) => (None, None),
245245
};
246246

247-
let mut fmt_parser = Parser::new(fmt, style, snippet.clone(), false, ParseMode::Format);
247+
let mut fmt_parser = Parser::new(fmt, style, snippet.clone(), ParseMode::Format);
248248
let n_arguments = (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();
249249

250250
if n_arguments > 0 && fmt_parser.errors.is_empty() {

compiler/rustc_parse_format/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ pub struct Parser<'a> {
233233
width_map: Vec<InnerWidthMapping>,
234234
/// Span of the last opening brace seen, used for error reporting
235235
last_opening_brace: Option<InnerSpan>,
236-
/// Whether the source string is comes from `println!` as opposed to `format!` or `print!`
237-
append_newline: bool,
238236
/// Whether this formatting string was written directly in the source. This controls whether we
239237
/// can use spans to refer into it and give better error messages.
240238
/// N.B: This does _not_ control whether implicit argument captures can be used.
@@ -322,7 +320,6 @@ impl<'a> Parser<'a> {
322320
s: &'a str,
323321
style: Option<usize>,
324322
snippet: Option<string::String>,
325-
append_newline: bool,
326323
mode: ParseMode,
327324
) -> Parser<'a> {
328325
let input_string_kind = find_width_map_from_snippet(s, snippet, style);
@@ -341,7 +338,6 @@ impl<'a> Parser<'a> {
341338
arg_places: vec![],
342339
width_map,
343340
last_opening_brace: None,
344-
append_newline,
345341
is_source_literal,
346342
cur_line_start: 0,
347343
line_spans: vec![],
@@ -485,8 +481,7 @@ impl<'a> Parser<'a> {
485481
} else {
486482
let description = format!("expected `{c:?}` but string was terminated");
487483
// point at closing `"`
488-
let pos = self.input.len() - if self.append_newline { 1 } else { 0 };
489-
let pos = self.to_span_index(pos);
484+
let pos = self.to_span_index(self.input.len());
490485
if c == '}' {
491486
let label = format!("expected `{c:?}`");
492487
let (note, secondary_label) = if c == '}' {

compiler/rustc_parse_format/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22

33
#[track_caller]
44
fn same(fmt: &'static str, p: &[Piece<'static>]) {
5-
let parser = Parser::new(fmt, None, None, false, ParseMode::Format);
5+
let parser = Parser::new(fmt, None, None, ParseMode::Format);
66
assert_eq!(parser.collect::<Vec<Piece<'static>>>(), p);
77
}
88

@@ -24,7 +24,7 @@ fn fmtdflt() -> FormatSpec<'static> {
2424
}
2525

2626
fn musterr(s: &str) {
27-
let mut p = Parser::new(s, None, None, false, ParseMode::Format);
27+
let mut p = Parser::new(s, None, None, ParseMode::Format);
2828
p.next();
2929
assert!(!p.errors.is_empty());
3030
}

compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ symbols! {
734734
format_args,
735735
format_args_capture,
736736
format_args_macro,
737-
format_args_nl,
738737
format_argument,
739738
format_arguments,
740739
format_count,

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ impl<'tcx> OnUnimplementedFormatString {
576576
let trait_name = tcx.item_name(trait_def_id);
577577
let generics = tcx.generics_of(item_def_id);
578578
let s = self.0.as_str();
579-
let parser = Parser::new(s, None, None, false, ParseMode::Format);
579+
let parser = Parser::new(s, None, None, ParseMode::Format);
580580
let mut result = Ok(());
581581
for token in parser {
582582
match token {
@@ -650,7 +650,7 @@ impl<'tcx> OnUnimplementedFormatString {
650650
let empty_string = String::new();
651651

652652
let s = self.0.as_str();
653-
let parser = Parser::new(s, None, None, false, ParseMode::Format);
653+
let parser = Parser::new(s, None, None, ParseMode::Format);
654654
let item_context = (options.get(&sym::ItemContext)).unwrap_or(&empty_string);
655655
parser
656656
.map(|p| match p {

library/core/src/macros/mod.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,12 @@ macro_rules! write {
549549
#[macro_export]
550550
#[stable(feature = "rust1", since = "1.0.0")]
551551
#[cfg_attr(not(test), rustc_diagnostic_item = "writeln_macro")]
552-
#[allow_internal_unstable(format_args_nl)]
553552
macro_rules! writeln {
554553
($dst:expr $(,)?) => {
555554
$crate::write!($dst, "\n")
556555
};
557556
($dst:expr, $($arg:tt)*) => {
558-
$dst.write_fmt($crate::format_args_nl!($($arg)*))
557+
$dst.write_fmt($crate::format_args!("{}\n", $crate::format_args!($($arg)*)))
559558
};
560559
}
561560

@@ -893,21 +892,6 @@ pub(crate) mod builtin {
893892
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
894893
}
895894

896-
/// Same as [`format_args`], but adds a newline in the end.
897-
#[unstable(
898-
feature = "format_args_nl",
899-
issue = "none",
900-
reason = "`format_args_nl` is only for internal \
901-
language use and is subject to change"
902-
)]
903-
#[allow_internal_unstable(fmt_internals)]
904-
#[rustc_builtin_macro]
905-
#[macro_export]
906-
macro_rules! format_args_nl {
907-
($fmt:expr) => {{ /* compiler built-in */ }};
908-
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
909-
}
910-
911895
/// Inspects an environment variable at compile time.
912896
///
913897
/// This macro will expand to the value of the named environment variable at

library/core/src/prelude/v1.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ pub use crate::hash::macros::Hash;
5555
#[allow(deprecated)]
5656
#[doc(no_inline)]
5757
pub use crate::{
58-
assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args,
59-
format_args_nl, include, include_bytes, include_str, line, log_syntax, module_path, option_env,
60-
stringify, trace_macros,
58+
assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args, include,
59+
include_bytes, include_str, line, log_syntax, module_path, option_env, stringify, trace_macros,
6160
};
6261

6362
#[unstable(

library/std/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@
343343
#![feature(core_panic)]
344344
#![feature(custom_test_frameworks)]
345345
#![feature(edition_panic)]
346-
#![feature(format_args_nl)]
347346
#![feature(get_many_mut)]
348347
#![feature(lazy_cell)]
349348
#![feature(log_syntax)]
@@ -621,8 +620,8 @@ pub use core::{
621620
#[allow(deprecated)]
622621
pub use core::{
623622
assert, assert_matches, cfg, column, compile_error, concat, concat_idents, const_format_args,
624-
env, file, format_args, format_args_nl, include, include_bytes, include_str, line, log_syntax,
625-
module_path, option_env, stringify, trace_macros,
623+
env, file, format_args, include, include_bytes, include_str, line, log_syntax, module_path,
624+
option_env, stringify, trace_macros,
626625
};
627626

628627
#[unstable(

library/std/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ macro_rules! print {
128128
#[macro_export]
129129
#[stable(feature = "rust1", since = "1.0.0")]
130130
#[cfg_attr(not(test), rustc_diagnostic_item = "println_macro")]
131-
#[allow_internal_unstable(print_internals, format_args_nl)]
131+
#[allow_internal_unstable(print_internals)]
132132
macro_rules! println {
133133
() => {
134134
$crate::print!("\n")
135135
};
136136
($($arg:tt)*) => {{
137-
$crate::io::_print($crate::format_args_nl!($($arg)*));
137+
$crate::io::_print($crate::format_args!("{}\n", $crate::format_args!($($arg)*)));
138138
}};
139139
}
140140

@@ -200,13 +200,13 @@ macro_rules! eprint {
200200
#[macro_export]
201201
#[stable(feature = "eprint", since = "1.19.0")]
202202
#[cfg_attr(not(test), rustc_diagnostic_item = "eprintln_macro")]
203-
#[allow_internal_unstable(print_internals, format_args_nl)]
203+
#[allow_internal_unstable(print_internals)]
204204
macro_rules! eprintln {
205205
() => {
206206
$crate::eprint!("\n")
207207
};
208208
($($arg:tt)*) => {{
209-
$crate::io::_eprint($crate::format_args_nl!($($arg)*));
209+
$crate::io::_eprint($crate::format_args!("{}\n", $crate::format_args!($($arg)*)));
210210
}};
211211
}
212212

library/std/src/prelude/v1.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ pub use crate::result::Result::{self, Err, Ok};
3939
#[allow(deprecated)]
4040
#[doc(no_inline)]
4141
pub use core::prelude::v1::{
42-
assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args,
43-
format_args_nl, include, include_bytes, include_str, line, log_syntax, module_path, option_env,
44-
stringify, trace_macros, Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd,
42+
assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args, include,
43+
include_bytes, include_str, line, log_syntax, module_path, option_env, stringify, trace_macros,
44+
Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd,
4545
};
4646

4747
#[unstable(

library/std/src/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,7 @@ impl<T: Termination, E: fmt::Debug> Termination for Result<T, E> {
22662266
match self {
22672267
Ok(val) => val.report(),
22682268
Err(err) => {
2269-
io::attempt_print_to_stderr(format_args_nl!("Error: {err:?}"));
2269+
io::attempt_print_to_stderr(format_args!("Error: {err:?}\n"));
22702270
ExitCode::FAILURE
22712271
}
22722272
}

src/tools/clippy/clippy_utils/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ pub fn find_format_args(cx: &LateContext<'_>, start: &Expr<'_>, expn_id: ExpnId,
391391
if ctxt.outer_expn().is_descendant_of(expn_id) {
392392
if macro_backtrace(expr.span)
393393
.map(|macro_call| cx.tcx.item_name(macro_call.def_id))
394-
.any(|name| matches!(name, sym::const_format_args | sym::format_args | sym::format_args_nl))
394+
.any(|name| matches!(name, sym::const_format_args | sym::format_args))
395395
{
396396
ControlFlow::Break(expr)
397397
} else {

src/tools/clippy/tests/ui/explicit_write.fixed

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ fn main() {
2323
use std::io::Write;
2424
print!("test");
2525
eprint!("test");
26-
println!("test");
27-
eprintln!("test");
26+
println!("{}\n", $crate::format_args!($($arg)*));
27+
eprintln!("{}\n", $crate::format_args!($($arg)*));
2828
print!("test");
2929
eprint!("test");
3030

3131
// including newlines
32-
println!("test\ntest");
33-
eprintln!("test\ntest");
32+
println!("{}\n", $crate::format_args!($($arg)*));
33+
eprintln!("{}\n", $crate::format_args!($($arg)*));
3434

3535
let value = 1;
36-
eprintln!("with {}", value);
37-
eprintln!("with {} {}", 2, value);
38-
eprintln!("with {value}");
39-
eprintln!("macro arg {}", one!());
36+
eprintln!("{}\n", $crate::format_args!($($arg)*));
37+
eprintln!("{}\n", $crate::format_args!($($arg)*));
38+
eprintln!("{}\n", $crate::format_args!($($arg)*));
39+
eprintln!("{}\n", $crate::format_args!($($arg)*));
4040
let width = 2;
41-
eprintln!("{:w$}", value, w = width);
41+
eprintln!("{}\n", $crate::format_args!($($arg)*));
4242
}
4343
// these should not warn, different destination
4444
{

0 commit comments

Comments
 (0)