Skip to content

inline tail call does not honor comptime semantics #12108

Open

Description

@andrewrk

Zig Version: 0.10.0-dev.2992+1fee9eac8

test "inline tail call" {
    if (!foo()) {
        @compileError("expected a comptime value");
    }
}

inline fn foo() bool {
    return bar();
}

inline fn bar() bool {
    return true;
}

Should pass, however, the result of foo() ends up being runtime-known:

$ stage2/bin/zig test test3.zig 
test3.zig:10:9: error: expected a comptime value
        @compileError("expected a comptime value");
        ^

Problem disappears if foo is rewritten like this:

inline fn foo() bool {
    const result = bar();
    return result;
}

Activity

  1. added
    bugObserved behavior contradicts documented or intended behavior
    frontendTokenization, parsing, AstGen, Sema, and Liveness.
    on Jul 13, 2022
  2. added this to the 0.10.0 milestone on Jul 13, 2022
  3. nektro commented on Jul 14, 2022

    @nektro
    Contributor

    doesnt pass on stage1 either. is this a new feature of inline fns being eagerly evaluated?

  4. andrewrk commented on Jul 20, 2022

    @andrewrk
    MemberAuthor

    Yes it's new inline semantics that are going into the language specification. Function inlining happens during semantic analysis, meaning that if an inlined function returns a comptime-known value, it is comptime-known at the callsite.

    It also makes recursive inline functions work as long as they don't recurse too many times.

  5. modified the milestones: 0.10.0, 0.10.1 on Oct 12, 2022
  6. modified the milestones: 0.10.1, 0.11.0 on Jan 10, 2023
  7. modified the milestones: 0.11.0, 0.11.1 on Jul 20, 2023
  8. tauoverpi commented on Aug 31, 2023

    @tauoverpi
    Contributor

    With the new behaviour, would inline fn assert(ok: bool) void make more sense than the current implementation given that the condition may be comptime known and thus all code following it can be deleted (replaced by panic or a return in release-fast/small)?

  9. mlugg commented on Sep 15, 2023

    @mlugg
    Member

    Appears to be fixed in master.

  10. modified the milestones: 0.11.1, 0.12.0, 0.13.0 on Jan 29, 2024
  11. modified the milestones: 0.14.0, 0.15.0 on Feb 10, 2025
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

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions