Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,5 @@ builtin_macros_unexpected_lit = expected path to a trait, found literal
.other = for example, write `#[derive(Debug)]` for `Debug`

builtin_macros_unnameable_test_items = cannot test inner items

builtin_macros_use_rust_debug_printing_macro = use rust debug printing macro
12 changes: 12 additions & 0 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ pub(crate) enum InvalidFormatStringSuggestion {
#[primary_span]
span: Span,
},

#[suggestion(
builtin_macros_use_rust_debug_printing_macro,
code = "{replacement}",
style = "verbose",
applicability = "machine-applicable"
)]
UseRustDebugPrintingMacro {
#[primary_span]
macro_span: Span,
replacement: String,
},
}

#[derive(Diagnostic)]
Expand Down
41 changes: 40 additions & 1 deletion compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn make_format_args(
ecx: &mut ExtCtxt<'_>,
input: MacroInput,
append_newline: bool,
macro_span: Span,
) -> ExpandResult<Result<FormatArgs, ErrorGuaranteed>, ()> {
let msg = "format argument must be a string literal";
let unexpanded_fmt_span = input.fmtstr.span;
Expand Down Expand Up @@ -333,6 +334,44 @@ fn make_format_args(
let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
e.sugg_ = Some(errors::InvalidFormatStringSuggestion::AddMissingColon { span });
}
parse::Suggestion::UseRustDebugPrintingMacro() => {
// there should only be suggestion when we have only one argument
if args.all_args().len() == 1 {
let arg = &args.all_args()[0];
match &arg.kind {
// println!("{x=}")
FormatArgumentKind::Captured(ident) => {
let ident_name = ident.name.to_ident_string();

let replacement = format!("dbg!({})", ident_name);

e.sugg_ = Some(
errors::InvalidFormatStringSuggestion::UseRustDebugPrintingMacro {
macro_span,
replacement,
},
);
}

// println!("{=}", x) or println!("{0=}", x)
FormatArgumentKind::Normal => {
let expr_span = arg.expr.span;

if let Ok(expr_snippet) = ecx.source_map().span_to_snippet(expr_span) {
let replacement = format!("dbg!({})", expr_snippet);

e.sugg_ = Some(
errors::InvalidFormatStringSuggestion::UseRustDebugPrintingMacro {
macro_span,
replacement,
},
);
}
}
_ => {}
}
}
}
}
let guar = ecx.dcx().emit_err(e);
return ExpandResult::Ready(Err(guar));
Expand Down Expand Up @@ -1048,7 +1087,7 @@ fn expand_format_args_impl<'cx>(
sp = ecx.with_def_site_ctxt(sp);
ExpandResult::Ready(match parse_args(ecx, sp, tts) {
Ok(input) => {
let ExpandResult::Ready(mac) = make_format_args(ecx, input, nl) else {
let ExpandResult::Ready(mac) = make_format_args(ecx, input, nl, sp) else {
return ExpandResult::Retry(());
};
match mac {
Expand Down
25 changes: 25 additions & 0 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ pub enum Suggestion {
/// Add missing colon:
/// `format!("{foo?}")` -> `format!("{foo:?}")`
AddMissingColon(Range<usize>),
/// Use Rust format string:
/// `format!("{x=}")` -> `dbg!(x)`
UseRustDebugPrintingMacro(),
}

/// The parser structure for interpreting the input format string. This is
Expand Down Expand Up @@ -462,6 +465,7 @@ impl<'input> Parser<'input> {
('?', _) => self.suggest_format_debug(),
('<' | '^' | '>', _) => self.suggest_format_align(c),
(',', _) => self.suggest_unsupported_python_numeric_grouping(),
('=', '}') => self.suggest_rust_debug_printing_macro(),
_ => self.suggest_positional_arg_instead_of_captured_arg(arg),
}
}
Expand Down Expand Up @@ -871,6 +875,27 @@ impl<'input> Parser<'input> {
}
}

fn suggest_rust_debug_printing_macro(&mut self) {
if let Some((range, _)) = self.consume_pos('=') {
self.errors.insert(
0,
ParseError {
description:
"python's f-string debug `=` is not supported in rust, use `dbg(x)` instead"
.to_owned(),
note: Some(format!("to print `{{`, you can escape it using `{{{{`",)),
label: "expected `}`".to_owned(),
span: range,
secondary_label: self
.last_open_brace
.clone()
.map(|sp| ("because of this opening brace".to_owned(), sp)),
suggestion: Suggestion::UseRustDebugPrintingMacro(),
},
);
}
}

fn suggest_format_align(&mut self, alignment: char) {
if let Some((range, _)) = self.consume_pos(alignment) {
self.errors.insert(
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/fmt/format-string-error-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,7 @@ raw { \n
//~^ ERROR invalid format string: expected `}`, found `?`
println!("{x,}, world!",);
//~^ ERROR invalid format string: python's numeric grouping `,` is not supported in rust format strings

println!("{x=}");
//~^ ERROR invalid format string: python's f-string debug `=` is not supported in rust, use `dbg(x)` instead
}
12 changes: 11 additions & 1 deletion tests/ui/fmt/format-string-error-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,15 @@ LL | println!("{x,}, world!",);
|
= note: to print `{`, you can escape it using `{{`

error: aborting due to 20 previous errors
error: invalid format string: python's f-string debug `=` is not supported in rust, use `dbg(x)` instead
--> $DIR/format-string-error-2.rs:92:17
|
LL | println!("{x=}");
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: to print `{`, you can escape it using `{{`

error: aborting due to 21 previous errors

Loading