Skip to content

Inferred return type is not visible to a caller that precedes the definition #1196

Closed

Description

@ehartford

Spec §9.1 (D43): an unannotated function's return type is the type of its tail. A caller that appears earlier in the file than the definition does not see that type.

type C { n: i32 }
impl C:
    fn go(x: i32) -> bool: x > 0 and later_pred()
fn later_pred(): 1 + 1 == 2
fn main:
    let c = C { n: 1 }
    if c.go(1): print("ok")
error: right operand of logical operator must be bool
 --> fwd.w:3:28

Expected: prints ok. Declaration order must not matter. Moving later_pred above impl C, or writing -> bool, makes it pass.

Workaround in use: -> bool on codegen_c_abi_aarch64 / codegen_c_abi_sysv_x86_64 in src/Codegen.w (the #1195 fix); remove when this is fixed.

Activity

  1. ehartford commented on Sep 19, 2026

    @ehartford
    ContributorAuthor

    Verified on the pinned seed v0.15.1.10: this predates D43. Any forward call to an unannotated function fails, free function or method.

    Fix on branch fix-filed-issues: check_bodies types the functions that take their type from their body first, then the ones with a written return type or a fixed contract (main, tests). On-demand checking of a callee mid-caller is not possible: the body check resets per-function state rather than saving it.

    Remaining shape: an unannotated function calling an unannotated function declared after it. That call is recorded and, once every body is typed, reported only if the callee turned out to produce a value (a call to a procedure was typed correctly all along — the compiler's own source has such calls). Ordering those by dependency needs the call graph before Sema; name resolution already records uses per definition, which is the place to get it from.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce; b11637b (#1205) types a function from its body before callers that precede it.

    $ cat main.w
    type C { n: i32 }
    impl C:
        fn go(x: i32) -> bool: x > 0 and later_pred()
    fn later_pred(): 1 + 1 == 2
    fn main:
        let c = C { n: 1 }
        if c.go(1): print("ok")
    $ with run main.w
    ok
    
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