Switch tests to use the rendered form of diagnostics (#8070)

Historically, we disabled the rendered form in tests outside of
`toolchain/diagnostics`. The idea was to keep tests shorter and avoid
very long chains of autoupdate before stabilizing due to all the line
numbers and other artifacts in the rendered form.

This PR adds `--diagnostic-line-numbers=[all|first]` and defaults to
`first` in tests. This keeps the rendered form so that we can easily
code review that form and the visual cues it does or doesn't provide.
But it removes all but the first location's line numbers to provide
significantly more stability in tests.

By default, tests get the full unicode line-drawing form of diagnostics
so we can ensure all of the artifacts there make sense with the specific
case being diagnosed, but we do retain some focused ASCII-rendering
tests to ensure that fallback remains usable.

There are some issues here that will be fixed in subsequent PRs.
Notably, we try to keep the frame together in the check lines, and so
force some parts to use the first location. This largely works, but
fails in some edge cases, causing the entire frame to be located
incorrectly. There is a TODO about this that we need to fix, and it only
occurs 6 times across the test suite, so it didn't seem worth
front-loading a fix for here.

Assisted-by: Claude Code
This commit is contained in:
Chandler Carruth authored and GitHub committed 2026-11-05 07:19:59 +00:00
1 parent 0b7059a360
commit d0badecd2a
888 files changed
+39731 -12799

No files matched your search

+39 -10
View File
@@ -1752,25 +1752,29 @@ static auto DrawLeadingLabelLine(Buffer& buffer, int x, int underline_y,
LineEnd::Edge, LineEnd::Center, weight);
}
// Draws `number` in the columns before the frame, or nothing when there is no
// line to name.
// Draws `number` in the columns before the frame when `numbered` is set, and
// nothing when there is no line to name.
//
// `points_here` is the leading part when its text is placed under this line.
// It draws `->` in the margin and colors the number to match the level word.
// It draws `->` in the margin, whether or not the line is numbered, and colors
// the number to match the level word.
// Every other number keeps the frame's color, so the line to begin reading at
// is the one that stands out from the frame.
static auto DrawLineNumber(Buffer& buffer, int frame_x, int y, int number,
const PreparedPart* points_here) -> void {
bool numbered, const PreparedPart* points_here)
-> void {
if (number <= 0) {
return;
}
std::string text = std::to_string(number);
// The line the problem is on takes the leading part's color and weight, and
// every other line number is left as light as the frame it belongs to, so
// that the one being reported is the one that stands out rather than every
// number competing with it.
Style style = points_here ? points_here->style : FrameStyle().Bold(false);
buffer.DrawText(frame_x - 1 - text.size(), y, text, style);
if (numbered) {
std::string text = std::to_string(number);
buffer.DrawText(frame_x - 1 - text.size(), y, text, style);
}
if (points_here) {
buffer.DrawText(0, y, LineArrow, style);
}
@@ -1960,6 +1964,25 @@ auto Renderer::Render(Terminal::OutputBufferRef out,
return;
}
// Golden files use `LineNumbers::First`. Autoupdate rewrites only the first
// location row's line number as a `[[@LINE...]]` offset, and any other line
// number in a golden would change whenever a `CHECK` line is added above the
// line it names. So only that row keeps its line and column, every other
// location is reduced to its file name, and `FrameRenderer` omits the
// gutter's numbers. The rows and layout don't change. The compact form above
// keeps every line number, because autoupdate rewrites the location that
// starts each of its lines as an offset.
if (line_numbers_ == LineNumbers::First) {
bool location_kept = false;
for (PreparedPart& part : parts) {
if (HasLocationRow(part) && !location_kept && !part.location.empty()) {
location_kept = true;
} else {
part.location = FormatLocation({.filename = part.file});
}
}
}
RenderFramed(out, parts, leading, diagnostic.level, frame_x, content_x,
source_columns, columns);
}
@@ -1970,14 +1993,15 @@ namespace {
class FrameRenderer {
public:
FrameRenderer(Buffer& buffer, Charset charset, int frame_x, int content_x,
int columns, Style dotted_frame_style)
int columns, Style dotted_frame_style, bool numbered)
: buffer_(&buffer),
glyphs_(Glyphs::For(charset)),
charset_(charset),
frame_x_(frame_x),
content_x_(content_x),
columns_(columns),
dotted_frame_style_(dotted_frame_style) {}
dotted_frame_style_(dotted_frame_style),
numbered_(numbered) {}
// Draws `rows` from the buffer's first row, followed by the closing rule.
auto Draw(llvm::ArrayRef<Row> rows) -> void;
@@ -2043,6 +2067,9 @@ class FrameRenderer {
// line's number also take.
Style dotted_frame_style_;
// Whether source rows are drawn with their line numbers.
bool numbered_;
// The row the underlines were drawn on, which every label below it connects
// back up to. A label can be several rows down from it, since the labels on
// one underline row stack.
@@ -2132,7 +2159,8 @@ auto FrameRenderer::DrawPath(const PreparedPart& part, int y) -> int {
}
auto FrameRenderer::DrawSource(const Row& row, int y) -> int {
DrawLineNumber(*buffer_, frame_x_, y, row.line_number, row.points_here);
DrawLineNumber(*buffer_, frame_x_, y, row.line_number, numbered_,
row.points_here);
buffer_->DrawText(content_x_, y, row.text, Style());
return y + 1;
}
@@ -2327,7 +2355,8 @@ auto Renderer::RenderFramed(Terminal::OutputBufferRef out,
.LayOut();
FrameRenderer(buffer, capabilities_.charset, frame_x, content_x, columns,
UnderlineStyle(parts[leading].style))
UnderlineStyle(parts[leading].style),
/*numbered=*/line_numbers_ == LineNumbers::All)
.Draw(rows);
buffer.Render(out, capabilities_.color_mode);
}