Implement multi-span error reporting for structs/records#28893
Implement multi-span error reporting for structs/records#28893tetektoza wants to merge 6 commits into
Conversation
|
That's awesome :D I'll review shortly. |
| writeln!( | ||
| f, | ||
| "{INDENT } |\n\ | ||
| {INDENT } = note: {label}\n\ |
There was a problem hiding this comment.
Not a big fan of note: here.. Just a generic label here is probably better.
There was a problem hiding this comment.
Removed that. You can check new changes.
6954a06 to
41356f0
Compare
As the title says, this adds support for displaying multiple related source locations in error messages, starting with duplicate struct/record member detection. Before we have only showed the duplicate location, now after this patch we are showing both duplicate and "first declared here" locations.
41356f0 to
8636134
Compare
|
@mohammadfawaz I really liked your proposal and went ahead and tried to reflect Rust as closely as possible. Key changes:
Hopefully you like it. Let's make this attempt the best possible, so I'm really open for more suggestions if you see some additional improvements. |
|
This is really awesome. I'll review carefully tomorrow, but a tiny feedback is to color both the label messages and the "caret"s in red, as in write!(f, "{}", "^".bold().red())?;and write!(f, " {}", label.bold().red())?;Now, in a dream world, the colors would be optionally passed for each label... and the default would be red. |
- Carets and labels now display in bold red by default - added SpanColor enum for optional per-label color customization
|
@mohammadfawaz Sure, implemented it as well and tried to respect |
mohammadfawaz
left a comment
There was a problem hiding this comment.
Overall this looks great! Just wondering about the color feature and whether it's enabled.
|
|
||
| impl SpanColor { | ||
| /// apply bold formatting with an optional color (defaults to red if None). | ||
| pub fn apply_bold(text: &str, color: Option<&SpanColor>) -> String { |
There was a problem hiding this comment.
Maybe we rename this to something like format_bold_colored
|
|
||
| /// Creates a backtraced error from multiple spans with labels, colors, and a backtrace. | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub fn new_from_spans_with_colors<S>( |
There was a problem hiding this comment.
Is this not used yet?
There was a problem hiding this comment.
Good catch, I've started adding this, then figured out I can merge it with new_from_spans and then dropped the idea midway :D Had to be late night.
| } | ||
|
|
||
| let mut last_line: Option<usize> = None; | ||
| let use_color = std::env::var("NOCOLOR").unwrap_or_default().trim().to_owned().is_empty(); |
There was a problem hiding this comment.
What is the reasoning for gating the color functionality with NOCOLOR?
There was a problem hiding this comment.
Yes, so just a little bit above those lines there's this comment:
// To avoid the color enabling characters for comparison with test expectations.
if std::env::var("NOCOLOR").unwrap_or_default().trim().to_owned().is_empty() {
I figured there could be some problem in the past with color being enabled and test expectations not being matched correctly. Although blame shows latest modifications 6 months ago, so maybe it's redundant now? I can change it, let me know.
|
Closing in favor of #28917 |


As the title says, this adds support for displaying multiple related source locations in error messages, starting with duplicate struct/record member detection.
Before we have only showed the duplicate location, now after this patch we are showing both duplicate and "first declared here" locations.
For a program:
Before:
After:
@mohammadfawaz as promised :)