Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the determined type of constant value expressions #38183

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,15 @@ private void updateConstantType(BConstantSymbol symbol, BLangExpression expr, Sy
return;
}

// When setting the BType of the expression, the determined type as well would be set to the same BType.
// Therefore, if the determined type was already not null, it shall be preserved and overridden with its
// previous value.
BType determinedType = expr.getDeterminedType();
expr.setBType(resolvedType);
if (determinedType != null) {
expr.setDeterminedType(determinedType);
}

symbol.type = resolvedType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ public Object[][] getExprPos() {
{78, 8, 20, BOOLEAN},
{78, 8, 10, ANYDATA},
{78, 14, 20, null},
{361, 14, 15, INT},
{362, 14, 21, INT},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,6 @@ function fooFn() returns future<int> {
function barFn() returns int {
return 10;
}

const int A = 10;
const int B = 20 + 30;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sanjana I have a doubt on the original issue (#36831) though.
Say I want to extract the constant. The extracted constant can be either the exact singleton value 30 or it can have the wider basic type node int. As of the current implementation, it only allows int. But @SandaruJayawardana is doing a complete revamp for constant expressions and both have to be allowed. What should be the user experience for this one?

const 30 A = 10 + 20; // valid
const int A = 10 + 20; // also valid

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is with the expression, right?

I think the behaviour should be consistent for both RHS expressions in

const int A = 10;
const int B = 20 + 30;

If the type of 10 is int, it should be the same for 20 + 30 also. But if the type for 20 + 30 is singleton 50, the type for 10 should also be singleton 10. Feels like the singleton approach is more accurate, but since this applies to all literals, may result in a lot of new types being created.

For example, even with int i = 10;, the type of 10 will then be the singleton.

I think we need to discuss this and come to a conclusion and then fix across the compiler for all literals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be holding this PR until the discussion initiated at #38250 is finalized.