Skip to content

with -n: a two-line snippet (let, then if) ignores the if's condition and prints every line #1238

Closed as not planned

Description

@ehartford

Repro, installed compiler (main at be767c0 + #1237 branch build):

printf "x\ta\nx\tdocs\nx\tREADME.md\n" > in.txt

# two lines: let, then if — prints ALL three lines (wrong)
with -n 'let path = line.split("\t").get(1)
if path != "docs" and not path.ends_with(".md"): print(line)' < in.txt

# the same condition inline — prints only `x\ta` (right)
with -n 'if line.split("\t").get(1) != "docs" and not line.split("\t").get(1).ends_with(".md"): print(line)' < in.txt

# two lines, simpler condition — still prints all three (wrong)
with -n 'let path = line.split("\t").get(1)
if path != "docs": print(line)' < in.txt

Expected: a multi-line -n body is the per-line program, statements in order, line bound; the if filters. Actual: with a leading let, the following if … : print(line) prints unconditionally (or path is not the value of the let).

Found while checking the green-identity fix (#1237) by hand: the by-hand listing kept the docs and *.md entries the lanes had (correctly) dropped, and for a minute it looked like the implementations disagreed. §18.5b one-liners; a failing with -n is a product bug, never a license for sed/awk.

Activity

  1. ehartford commented on Sep 20, 2026

    @ehartford
    ContributorAuthor

    Duplicate of #962, with a stronger repro: the view is not merely zeroed, it is dangling.

    fn main:
        let line = "x\tdocs"
        let path = line.split("\t").get(1)     // view into the temporary Vec split() returned
        print(f"{path != "docs"} {path == "docs"} {path.ends_with(".md")}")   // true false false  (all wrong)
        let parts = line.split("\t")
        let p2 = parts.get(1)
        print(f"{p2 != "docs"} {p2 == "docs"}")   // false true  (right)
    

    This is not a one-liner bug: with -n wraps the snippet correctly; the let in it binds a view into a statement temporary and safe code reads freed memory. Closing this in favour of #962, which names the fix (reject the binding under D27/§21.1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions