Skip to content

[BUG] Analysis of uninitialized local fails at if without else #440

Closed
@realgdman

Description

@realgdman

Analysis of uninitialized local fails to see initialization which happens after single-if without else.

Reproduce Code

foo: (x: bool) -> int = {
  i: int;
  if(x) { 
    i = 1;
    return i; 
  }
  i = 2;
  return i;
}

main: () = foo(true);

Version
latest
Command line
cppfront/cppfront $1.cpp2 -p
clang++-15 -Icppfront/include $1.cpp -std=c++20 -o $1

Expected result
Compilation success

Actual result/error

initpath.cpp2(2,2): error: local variable i must be initialized on both branches or neither branch
initpath.cpp2(3,2): error: "if" initializes i on:
  branch starting at line 3
but not on:
  implicit else branch

Additional context
With return, both paths init and return.
Without return, there is double initialization, but error is the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions