Repository navigation
Description
Spec
§29.13 Block Body Syntax (normative):
Form 1 — Inline colon. A colon immediately followed by content on the same line. [...] The body is a single block item. The inline body ends at the first top-level newline. Newlines inside balanced delimiters (parentheses, brackets, braces) do not terminate the inline body.
Form 2 — Indented colon. The colon ends the line; the body is the indented block on subsequent lines.
§30.8 (informative) restates it: COLON_INLINE := ':' BLOCK_ITEM NEWLINE // single item, same line, and §30.3 gives TRAIT_DECL := ... 'trait' IDENT [ TYPE_PARAMS ] BODY, IMPL_DECL := 'impl' ... TYPE BODY. §2.3 shows the bodyless marker impl impl Copy for Point.
So trait X: fn a(...) -> T (one member on the header line, even with its parameter list broken across lines inside the parens) is a valid single-item inline body, and the stdlib's aligned one-liners (impl Clone for i8: fn clone(): *self) are conforming. What the spec does not allow is an inline body followed by further indented members, and a bodyless impl Copy for X is complete at its newline.
Repro 1: inline first member + indented members accepted as one body
trait Foo: fn a(self: &Self) -> i32
fn b(self: &Self) -> i32
type X { v: i32 }
impl X: fn a(self: &Self) -> i32: 1
fn b(self: &Self) -> i32: 2
fn main:
let x = X { v: 0 }
print(f"{x.a() + x.b()}")
Expected: a parse error at the indented fn b (the inline body ended at the newline), exactly as for fn:
fn f() -> i32: print("one")
41
which the compiler already rejects (the 41 is not part of f's body). Actual (installed with v0.15.2.1-g478b26481): the trait/impl case compiles and prints 3.
Repro 2: bodyless impl consumes the indented members that follow
type X { v: i32 }
trait Foo:
fn a(self: &Self) -> i32
impl Foo for X
fn a(self: &Self) -> i32: 5
fn main:
let x = X { v: 0 }
print(f"{x.a()}")
Expected: impl Foo for X is a bodyless impl (§2.3 form), so either missing a is reported or the indented fn is a parse error (§29.13: "After a construct's header, a body introducer is required ... omitting it is a parse error"). Actual: prints 5; the fn became the impl's member with no introducer.
Root cause (source reading)
src/Parser.w parse_trait_decl (around lines 2904-2936) and parse_impl_block (around 3181-3203) treat : as optional punctuation: after it they skip_newlines() and then loop over member keywords, stopping only when a member starts at column 0. There is no inline-vs-indented distinction and no check that a member is on the header line or deeper than the header's indentation, and parse_impl_block has no else for a missing introducer (only TK_EQ errors), so the same column-0 loop runs after a bare header.
Why it matters
129c4d1 (#385, "Remove legacy equals trait bodies") scrambled 45 files into trait Foo: fn bar(self: / Self) -> i32 + further indented members. The parser accepted every one, so the damage sat in the tree (examples/service, lib/std, 37 tests) for three months. Repaired whitespace-only in the branch repair-scrambled-trait-headers; the parser should have rejected the multi-member shape.
Workaround used: none needed after the repair; the layout is now the canonical Form 2 everywhere.
Activity
Parser: trait/impl/extend bodies take exactly the three §29.13 body f…
Parser: trait/impl/extend bodies take exactly the three §29.13 body f…
Parser: trait/impl/extend bodies take exactly the three §29.13 body f…
Parser: trait/impl/extend bodies take exactly the three §29.13 body f…
Parser: trait/impl/extend bodies take exactly the three §29.13 body f…
var PATTERN binds mutable names; let PATTERN: TYPE = EXPR (§9.7, §30.…
:install-user installs through the .Install kind's placement: temp + …