printer: multi-line raw text keeps the column honest

Raw text carrying a string literal across a line break, and the #( ... )
of a parameterized type, were emitted as a single Doc::Text with embedded
newlines -- violating that variant's contract and leaving the printer's
column tracker stale, so every later fits() on the line broke
conservatively. Both now emit Doc::Inline, whose printer arm resumes
measuring from the text's own last line. Output bytes are identical
except where the stale column forced a spurious break: one corpus file
(id_queue_tb.sv) improves, pass set unchanged at 813/820, verify.rs
untouched. Found by QA in the directive-split round; pinned by a printer
test and a classes.cases entry.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5 committed 2026-08-26 02:10:10 -07:00
1 parent 19253c105b
commit 53f226981f
4 files changed
+42 -7

No files matched your search

+3 -1
View File
@@ -8,7 +8,9 @@ pub enum Doc {
/// Verbatim text. Must not contain newlines (use `HardLine`).
Text(String),
/// Verbatim text spliced into the *middle* of a line, newlines and all:
/// the `{ ... }` of a `randomize() with { ... }` constraint block. It
/// the `{ ... }` of a `randomize() with { ... }` constraint block, the
/// `#( ... )` of a parameterized type reference, or raw text that
/// carries a string literal across a line break. It
/// carries its own indentation, so the printer copies it through
/// untouched and resumes measuring from its last line. Unlike
/// [`Doc::Verbatim`] it does not force enclosing groups to break — the
+10 -6
View File
@@ -219,12 +219,14 @@ impl<'a> Fmt<'a> {
/// A token that carries a string literal across a line break is emitted
/// completely untouched instead: the leading whitespace of such a
/// continuation line is part of the string's *value*, and re-indenting it
/// would rewrite the string. Emitting the text as a single `Doc::Text`
/// means the printer copies the newlines through without adding indent,
/// so nothing changes and the result is a fixed point by construction.
/// would rewrite the string. Emitting the text as a `Doc::Inline` means
/// the printer copies the newlines through without adding indent — so
/// nothing changes and the result is a fixed point by construction —
/// while its column tracking resumes from the text's own last line
/// (`Doc::Text` must not contain newlines).
fn raw_text(&self, raw: &RawText) -> Doc {
if crosses_line_inside_string(&raw.text) {
return text(raw.text.clone());
return Doc::Inline(raw.text.clone());
}
self.raw_lines(&raw.text, raw.col)
}
@@ -236,9 +238,11 @@ impl<'a> Fmt<'a> {
/// `raw_lines`'s dedent-by-column only reaches a fixed point when the text
/// begins its own line — the dedent has to cancel the indent the printer
/// adds back — and this text never does, so re-indenting it would drift.
/// Emitting it unchanged is idempotent by construction.
/// Emitting it unchanged is idempotent by construction. `Doc::Inline`
/// rather than `Doc::Text` so a multi-line `#( ... )` keeps the printer's
/// column honest (`Doc::Text` must not contain newlines).
fn raw_inline(&self, raw: &RawText) -> Doc {
text(raw.text.clone())
Doc::Inline(raw.text.clone())
}
/// The `{ ... }` of a `randomize() with { ... }`: verbatim source spliced
+16
View File
@@ -254,6 +254,22 @@ mod tests {
);
}
/// After a multi-line `Inline` is *printed*, `col` is the width of its
/// last line — so a group that follows it on that line is measured from
/// there, not from an accumulated character count. Raw text that carries
/// a string literal across a line break rides this arm; when it was
/// emitted as `Doc::Text` (whose printer arm never resets `col` on the
/// embedded newlines it is not supposed to contain), the stale column
/// forced every later fit check on the line into a conservative break.
#[test]
fn a_group_after_a_multi_line_inline_measures_from_its_last_line() {
let d = concat(vec![
Doc::Inline("x = \"aaaaaaaaaaaaaaaaaaaaaaaa\nbbb\" ".into()),
group(vec![text("+"), indent(vec![Doc::Line, text("yyyy")]), text(";")]),
]);
assert_eq!(p(20, d), "x = \"aaaaaaaaaaaaaaaaaaaaaaaa\nbbb\" + yyyy;");
}
#[test]
fn hard_line_forces_break() {
let d = group(vec![text("a"), Doc::HardLine, text("b")]);
@@ -560,3 +560,16 @@ class c;
g = 1;
endfunction
endclass
====
# the printer's column stays honest after a multi-line #( ... ): the
# initializer is measured from the raw text's last line, not from an
# accumulated count, so a call that fits the real line stays on it
class c;
uvm_analysis_port #(bit [31:0],
foo_t) p = new("a_fairly_long_component_name", some_parent_handle, extra_argument);
endclass
----
class c;
uvm_analysis_port #(bit [31:0],
foo_t) p = new("a_fairly_long_component_name", some_parent_handle, extra_argument);
endclass