Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/lower/view_to_library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4445,6 +4445,13 @@ pub(super) fn todo_io_append(tag: &str, span: crate::span::Span) -> Expr {
runs no side effect in the emitted view"
),
));
noop_io_append()
}

/// `io << ""` — a statement that keeps an arm non-empty while rendering
/// nothing. The catch-all's body without its ledger line, for statements
/// that genuinely have nothing to drop (a bare literal).
pub(super) fn noop_io_append() -> Expr {
send(
Some(var_ref(Symbol::from("io"))),
"<<",
Expand Down
6 changes: 5 additions & 1 deletion src/lower/view_to_library/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::turbo_drive::emit_turbo_drive_directive;
use super::predicates::rewrite_predicates;
use super::{
accumulator_append_call, accumulator_result_ref, assign_accumulator_string_new, lit_sym,
nil_lit, seq, send, todo_io_append, view_helpers_call, ViewCtx,
nil_lit, seq, noop_io_append, send, todo_io_append, view_helpers_call, ViewCtx,
};

/// Walk a compiled-ERB body (`Seq` of `_buf = …` statements + control-
Expand Down Expand Up @@ -431,6 +431,10 @@ fn walk_stmt(stmt: &Expr, ctx: &ViewCtx) -> Vec<Expr> {
// genuinely a statement (the prior TODO-append swallowed the
// side effect and broke stack-walking templates).
ExprNode::Send { recv: Some(_), block: None, .. } => vec![stmt.clone()],
// A bare literal — most often the synthesized `nil` arm `<% unless
// %>` lowers to — renders nothing and does nothing: nothing is
// dropped, so no residue line.
ExprNode::Lit { .. } => vec![noop_io_append()],
_ => vec![todo_io_append("unknown stmt", stmt.span)],
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/view_walker_residue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ fn an_unlowerable_statement_files_residue() {
res[0].message
);
}

/// `<% unless cond %>…<% end %>` reaches the walker as `if cond then nil
/// else … end`: the `then` arm is a SYNTHESIZED bare `nil`, with no span.
/// A literal in statement position renders nothing and does nothing, so
/// there is nothing to drop — but it fell to the catch-all, which filed a
/// span-less "template statement dropped" line for every `unless` in a
/// template. A ledger that cries wolf hides the real drops.
#[test]
fn an_unless_block_files_no_residue_and_keeps_its_body() {
let (body, diags) = emit("<% unless @articles.empty? %>\n<p>listed</p>\n<% end %>\n");
assert!(body.contains("<p>listed</p>"), "unless body survives:\n{body}");
assert!(residues(&diags).is_empty(), "no residue for a bare literal arm: {diags:?}");
}

/// The same holds for a literal written in the template itself.
#[test]
fn a_literal_statement_files_no_residue() {
let (_, diags) = emit("<% nil %>\n<p>x</p>\n");
assert!(residues(&diags).is_empty(), "{diags:?}");
}