-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve error message when writer is forgotten in write and writeln macro #109149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,6 +245,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
None | ||
} | ||
|
||
fn suggest_missing_writer( | ||
&self, | ||
rcvr_ty: Ty<'tcx>, | ||
args: (&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>]), | ||
) -> DiagnosticBuilder<'_, ErrorGuaranteed> { | ||
let (ty_str, _ty_file) = self.tcx.short_ty_string(rcvr_ty); | ||
let mut err = | ||
struct_span_err!(self.tcx.sess, args.0.span, E0599, "cannot write into `{}`", ty_str); | ||
err.span_note( | ||
args.0.span, | ||
"must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this message include a subject? E.g.: "a writer must..."? I'm worried that otherwise it might not be clear what the message is talking about. |
||
); | ||
if let ExprKind::Lit(_) = args.0.kind { | ||
err.span_help( | ||
args.0.span.shrink_to_lo(), | ||
"a writer is needed before this format string", | ||
); | ||
}; | ||
|
||
err | ||
} | ||
|
||
pub fn report_no_match_method_error( | ||
&self, | ||
mut span: Span, | ||
|
@@ -323,16 +345,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
} | ||
} | ||
|
||
let mut err = struct_span_err!( | ||
tcx.sess, | ||
span, | ||
E0599, | ||
"no {} named `{}` found for {} `{}` in the current scope", | ||
item_kind, | ||
item_name, | ||
rcvr_ty.prefix_string(self.tcx), | ||
ty_str_reported, | ||
); | ||
let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.map_or(false, |def_id| { | ||
tcx.is_diagnostic_item(sym::write_macro, def_id) | ||
|| tcx.is_diagnostic_item(sym::writeln_macro, def_id) | ||
}) && item_name.name == Symbol::intern("write_fmt"); | ||
let mut err = if is_write | ||
&& let Some(args) = args | ||
{ | ||
self.suggest_missing_writer(rcvr_ty, args) | ||
} else { | ||
struct_span_err!( | ||
tcx.sess, | ||
span, | ||
E0599, | ||
"no {} named `{}` found for {} `{}` in the current scope", | ||
item_kind, | ||
item_name, | ||
rcvr_ty.prefix_string(self.tcx), | ||
ty_str_reported, | ||
) | ||
}; | ||
if tcx.sess.source_map().is_multiline(sugg_span) { | ||
err.span_label(sugg_span.with_hi(span.lo()), ""); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Check error for missing writer in writeln! and write! macro | ||
fn main() { | ||
let x = 1; | ||
let y = 2; | ||
write!("{}_{}", x, y); | ||
//~^ ERROR format argument must be a string literal | ||
//~| HELP you might be missing a string literal to format with | ||
//~| ERROR cannot write into `&'static str` | ||
//~| NOTE must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method | ||
//~| HELP a writer is needed before this format string | ||
writeln!("{}_{}", x, y); | ||
//~^ ERROR format argument must be a string literal | ||
//~| HELP you might be missing a string literal to format with | ||
//~| ERROR cannot write into `&'static str` | ||
//~| NOTE must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method | ||
//~| HELP a writer is needed before this format string | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
error: format argument must be a string literal | ||
--> $DIR/missing-writer.rs:5:21 | ||
| | ||
LL | write!("{}_{}", x, y); | ||
| ^ | ||
| | ||
help: you might be missing a string literal to format with | ||
| | ||
LL | write!("{}_{}", "{} {}", x, y); | ||
| ++++++++ | ||
|
||
error: format argument must be a string literal | ||
--> $DIR/missing-writer.rs:11:23 | ||
| | ||
LL | writeln!("{}_{}", x, y); | ||
| ^ | ||
| | ||
help: you might be missing a string literal to format with | ||
| | ||
LL | writeln!("{}_{}", "{} {}", x, y); | ||
| ++++++++ | ||
|
||
error[E0599]: cannot write into `&'static str` | ||
--> $DIR/missing-writer.rs:5:12 | ||
| | ||
LL | write!("{}_{}", x, y); | ||
| -------^^^^^^^------- method not found in `&str` | ||
| | ||
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method | ||
--> $DIR/missing-writer.rs:5:12 | ||
| | ||
LL | write!("{}_{}", x, y); | ||
| ^^^^^^^ | ||
help: a writer is needed before this format string | ||
--> $DIR/missing-writer.rs:5:12 | ||
| | ||
LL | write!("{}_{}", x, y); | ||
| ^ | ||
|
||
error[E0599]: cannot write into `&'static str` | ||
--> $DIR/missing-writer.rs:11:14 | ||
| | ||
LL | writeln!("{}_{}", x, y); | ||
| ---------^^^^^^^------- method not found in `&str` | ||
| | ||
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method | ||
--> $DIR/missing-writer.rs:11:14 | ||
| | ||
LL | writeln!("{}_{}", x, y); | ||
| ^^^^^^^ | ||
help: a writer is needed before this format string | ||
--> $DIR/missing-writer.rs:11:14 | ||
| | ||
LL | writeln!("{}_{}", x, y); | ||
| ^ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.