Skip to content

[Bug]: conditional return/expression types should not be dependent on branch position #26991

Open
@arezaii

Description

@arezaii

Summary of Problem

Conditionals in procedures and expressions may result in unexpected types when casting literals to integrals.

Description:
If the branches of a conditional return a value cast to different integral types, such as int(64) and uint(64), the evaluated type is always the first branch's type if the value fits in both types. The expected type would be the result of unifying the branch types, or uint(64) in both examples.

Steps to Reproduce

Source Code:

These examples use the value 0, but other values behave the same if they fit in either type.

Given the following codes:

proc foo(arg: bool) {
  if arg then return 0:int(64); else return 0:uint(64);
}
var x = foo(false);

proc bar(arg: bool) {
  if arg then return 0:uint(64); else return 0:int(64);
}

var y = bar(false);

The type of x will be int(64) while the type of y will be uint(64), independent of the value of arg. In both cases, we expect it will be uint(64). This behavior can also be seen in if expressions:

var b: bool = true;
var x = if b then 0:int(64) else 0:uint(64);
var y = if b then 0:uint(64) else 0:int(64);

As with the previous example, the expectation is that both x and y are uint(64) but instead the type of x is int(64) and y is uint(64).

Associated Future Test:

test/statements/conditionals/integralReturnTypesInConditional.chpl (to be merged as part of #26603)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions