Skip to content

Optional chaining on a tuple element (o?.1): Sema types it wrong, and inside an f-string MIR lowering fails with no source location #1503

Closed

Description

@ehartford

Spec §10.3: expr?.field → expr.map(v => v.field), result Option[U]. A tuple element is a field (§4.8 pair.0), and the parser accepts an integer after ?. (Parser.parse_optional_chain).

fn first(o: Option[(str, i32)]) -> Option[i32]: o?.1
fn main:
    let o: Option[(str, i32)] = Some(("xyz".clone(), 4))
    print(f"{first(o) ?? 0}")

Actual (stage1 of origin/main ba2d5d1; same after #1450's lexer change, which does not touch this path):

error: return type mismatch
 --> optchain.w:1:49
1 | fn first(o: Option[(str, i32)]) -> Option[i32]: o?.1

The same chain inside an f-string hole:

fn main:
    let o: Option[(str, i32)] = Some(("xyz".clone(), 4))
    print(f"{o?.1}")
error: MIR lowering failed for function 'main' in optchain_f.w; AST codegen was removed, so this function cannot be compiled (re-run with WITH_MIR_AUDIT=1 to inspect unsupported nodes)
error: code generation failed

This error is loud but gives no source location, and it is a MIR failure rather than a Sema type.

The struct-field form works: type P { a: i32 }, print(f"{o?.a}") prints Some(4).

Expected: o?.1 has type Option[i32], first(o) ?? 0 prints 4, and print(f"{o?.1}") prints Some(4).

Found while building the #1450 matrix.

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