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

Handle constant folding properly in poly conditional expressions in assignment context #3211

Merged
merged 1 commit into from
Nov 1, 2024
Merged
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 @@ -527,22 +527,11 @@ public TypeBinding resolveType(BlockScope scope) {
if (this.originalValueIfFalseType == null || !this.originalValueIfFalseType.isValidBinding())
return this.resolvedType = null;
}
// Propagate the constant value from the valueIfTrue and valueIFFalse expression if it is possible
Constant condConstant, trueConstant, falseConstant;
if ((condConstant = this.condition.constant) != Constant.NotAConstant
&& (trueConstant = this.valueIfTrue.constant) != Constant.NotAConstant
&& (falseConstant = this.valueIfFalse.constant) != Constant.NotAConstant) {
// all terms are constant expression so we can propagate the constant
// from valueIFTrue or valueIfFalse to the receiver constant
this.constant = condConstant.booleanValue() ? trueConstant : falseConstant;
}
if (isPolyExpression()) {
if (this.expectedType == null || !this.expectedType.isProperType(true)) {
// We will be back here in case of a PolyTypeBinding. So, to enable
// further processing, set it back to default.
this.constant = Constant.NotAConstant;
return new PolyTypeBinding(this);
}
constantFold();
return this.resolvedType = computeConversions(scope, this.expectedType) ? this.expectedType : null;
}

Expand Down Expand Up @@ -584,6 +573,8 @@ public TypeBinding resolveType(BlockScope scope) {
}
}
}
constantFold();
Constant condConstant;
if (TypeBinding.equalsEquals(valueIfTrueType, valueIfFalseType)) { // harmed the implicit conversion
this.valueIfTrue.computeConversion(scope, valueIfTrueType, this.originalValueIfTrueType);
this.valueIfFalse.computeConversion(scope, valueIfFalseType, this.originalValueIfFalseType);
Expand Down Expand Up @@ -687,6 +678,18 @@ public TypeBinding resolveType(BlockScope scope) {
return null;
}

private void constantFold() {
// Propagate the constant value from the valueIfTrue and valueIFFalse expression if it is possible
Constant condConstant, trueConstant, falseConstant;
if ((condConstant = this.condition.constant) != Constant.NotAConstant
&& (trueConstant = this.valueIfTrue.constant) != Constant.NotAConstant
&& (falseConstant = this.valueIfFalse.constant) != Constant.NotAConstant) {
// all terms are constant expression so we can propagate the constant
// from valueIFTrue or valueIfFalse to the receiver constant
this.constant = condConstant.booleanValue() ? trueConstant : falseConstant;
}
}

protected boolean computeConversions(BlockScope scope, TypeBinding targetType) {
boolean ok = true;
if (this.originalValueIfTrueType != null && this.originalValueIfTrueType.isValidBinding()) {
Expand Down
Loading