Repository navigation
Closed
Description
Summary
Reject nested f-strings as required by §15.4. The parser currently treats an inner f-string inside an interpolation hole as an ordinary expression and accepts it, even though the spec explicitly forbids nesting. This is a parser/sema validation bug with a small, clear negative test.
Spec
§15.4 says plain string literals do not interpolate and "F-strings may not be nested" (docs/with-specification.md:7640-7642). F-string holes may contain ordinary With expressions (docs/with-specification.md:7630-7632), but that expression allowance is constrained by the no-nesting rule.
Invalid:
let x = 1
let s = f"outer {f"inner {x}"}"
Valid alternative:
let inner = f"inner {x}"
let s = f"outer {inner}"
Current state
- The original issue verified that
f"outer {f\"inner {x}\"} end"compiles. src/Parser.w:3465-3560scans f-string contents, tracks nested brace depth, and parses each hole withparse_interpolated_expr/parse_interpolated_expr_attempt. The scanner tracks quoted strings and braces, but it does not reject anf"..."token sequence inside the hole.src/SemaCheck.w:3613-3641type-checks f-string segments and validates format specs, but it does not inspect the expression subtree for nestedNK_FSTRING.test/behavior/behav_fstring_debug_nested.wcovers debug formatting of nested data structures, not nested f-string syntax.- There is no compile-error test for nested f-strings.
Implementation plan
- Add a validation pass in the parser or sema. Sema is less brittle: after parsing an f-string, walk each expression segment and reject any descendant
NK_FSTRINGwith a diagnostic likenested f-strings are not allowed; bind the inner f-string to a name first. - Put the recursive check near
Sema.check_fstring(src/SemaCheck.w:3613-3641) so it catches nested f-strings regardless of whether they appear directly, in a call argument, in an index expression, or under parentheses. - Make sure the check does not reject:
- ordinary string literals containing braces;
- escaped literal braces
{{and}}; - f-strings in separate statements later interpolated as
strvariables.
- If parser-level recovery is better for source spans, add a parser diagnostic during
parse_interpolated_exprbut keep sema coverage as the authoritative guard.
Test plan
test/compile_errors/err_nested_fstring_direct.w:let s = f"outer {f\"inner {x}\"} end"expectsnested f-strings are not allowed.
- Additional negative case with nesting under a call or grouping:
f"outer {(f\"inner {x}\").len()}"
- Positive behavior test:
let inner = f"inner {x}"; let outer = f"outer {inner}"compiles and prints the expected string.
- Existing escaped-brace tests from Tests: fstring spec coverage (§15) #503 must keep passing.
Acceptance criteria
- Any f-string expression subtree containing another f-string is rejected
- Diagnostic explains the fix: bind/compute the inner f-string separately
- Escaped braces and ordinary string literals remain legal
- Positive non-nested f-string interpolation still works
-
with build && with build :fixpoint && with build :testpass
Dependencies
None. Related to #503 for f-string coverage.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 15.4.1.13.
Metadata
Metadata
Assignees
Labels
No labels
Activity
ehartford commentedon Jun 20, 2026
Implemented in 9bfbef5. Added sema validation that rejects any f-string expression hole whose expression subtree contains another f-string, with diagnostics telling the user to bind the inner f-string first. Added direct and call-argument negative tests plus positive coverage for the bind-first pattern, escaped braces, and ordinary string braces. Verification passed: build, fixpoint, full test suite, and explicit test-green.