Skip to content

Reject nested f-strings (§15.4) #441

Closed

Description

@ehartford

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-3560 scans f-string contents, tracks nested brace depth, and parses each hole with parse_interpolated_expr/parse_interpolated_expr_attempt. The scanner tracks quoted strings and braces, but it does not reject an f"..." token sequence inside the hole.
  • src/SemaCheck.w:3613-3641 type-checks f-string segments and validates format specs, but it does not inspect the expression subtree for nested NK_FSTRING.
  • test/behavior/behav_fstring_debug_nested.w covers debug formatting of nested data structures, not nested f-string syntax.
  • There is no compile-error test for nested f-strings.

Implementation plan

  1. 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_FSTRING with a diagnostic like nested f-strings are not allowed; bind the inner f-string to a name first.
  2. 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.
  3. Make sure the check does not reject:
    • ordinary string literals containing braces;
    • escaped literal braces {{ and }};
    • f-strings in separate statements later interpolated as str variables.
  4. If parser-level recovery is better for source spans, add a parser diagnostic during parse_interpolated_expr but 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" expects nested 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 :test pass

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.

Activity

  1. ehartford commented on Jun 20, 2026

    @ehartford
    ContributorAuthor

    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.

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