Skip to content

Commit

Permalink
[javac] Bring nullability in casts, conditional and newarray to parit…
Browse files Browse the repository at this point in the history
…y with jdt.

PiperOrigin-RevId: 716356607
  • Loading branch information
rluble authored and copybara-github committed Jan 16, 2025
1 parent 430b6d1 commit bab9e6e
Showing 1 changed file with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private Variable createVariable(JCVariableDecl variableDeclaration, boolean isPa
getNamePosition(variableElement.getSimpleName().toString(), variableDeclaration),
variableElement,
isParameter,
getCurrentType().getDeclaration().isNullMarked());
inNullMarkedScope());
variableByVariableElement.put(variableElement, variable);
return variable;
}
Expand Down Expand Up @@ -772,19 +772,39 @@ private UnaryExpression convertPrefixUnary(JCUnary expression) {
}

private CastExpression convertCast(JCTypeCast expression) {
TypeDescriptor castTypeDescriptor = environment.createTypeDescriptor(expression.getType().type);
TypeDescriptor castTypeDescriptor =
environment.createTypeDescriptor(expression.getType().type, inNullMarkedScope());

Expression castExpression = convertExpression(expression.getExpression());

if (!castExpression.canBeNull()) {
castTypeDescriptor = castTypeDescriptor.toNonNullable();
} else if (castExpression.getTypeDescriptor().isNullable()) {
castTypeDescriptor = castTypeDescriptor.toNullable();
}

return CastExpression.newBuilder()
.setExpression(convertExpression(expression.getExpression()))
.setExpression(castExpression)
.setCastTypeDescriptor(castTypeDescriptor)
.build();
}

private ConditionalExpression convertConditional(JCConditional conditionalExpression) {
TypeDescriptor conditionalTypeDescriptor =
environment.createTypeDescriptor(conditionalExpression.type, inNullMarkedScope());

Expression condition = convertExpression(conditionalExpression.getCondition());
Expression trueExpression = convertExpression(conditionalExpression.getTrueExpression());
Expression falseExpression = convertExpression(conditionalExpression.getFalseExpression());
return ConditionalExpression.newBuilder()
.setTypeDescriptor(environment.createTypeDescriptor(conditionalExpression.type))
.setConditionExpression(convertExpression(conditionalExpression.getCondition()))
.setTrueExpression(convertExpression(conditionalExpression.getTrueExpression()))
.setFalseExpression(convertExpression(conditionalExpression.getFalseExpression()))
.setTypeDescriptor(
trueExpression.getTypeDescriptor().canBeNull()
|| falseExpression.getTypeDescriptor().canBeNull()
? conditionalTypeDescriptor.toNullable()
: conditionalTypeDescriptor)
.setConditionExpression(condition)
.setTrueExpression(trueExpression)
.setFalseExpression(falseExpression)
.build();
}

Expand Down Expand Up @@ -865,7 +885,9 @@ private Expression convertMemberReference(JCMemberReference memberReference) {
return ArrayCreationReference.newBuilder()
.setTargetTypeDescriptor(
environment.createTypeDescriptor(
memberReference.getQualifierExpression().type, ArrayTypeDescriptor.class))
memberReference.getQualifierExpression().type,
inNullMarkedScope(),
ArrayTypeDescriptor.class))
.setInterfaceMethodDescriptor(functionalMethodDescriptor)
.setSourcePosition(getSourcePosition(memberReference))
.build();
Expand Down Expand Up @@ -894,7 +916,8 @@ private Expression convertMemberReference(JCMemberReference memberReference) {

private NewArray convertNewArray(JCNewArray expression) {
ArrayTypeDescriptor typeDescriptor =
environment.createTypeDescriptor(expression.type, ArrayTypeDescriptor.class);
environment.createTypeDescriptor(
expression.type, inNullMarkedScope(), ArrayTypeDescriptor.class);

List<Expression> dimensionExpressions = convertExpressions(expression.getDimensions());
// Pad the dimension expressions with null values to denote omitted dimensions.
Expand Down Expand Up @@ -1391,4 +1414,8 @@ private static void sortPackageInfoFirst(List<CompilationUnitTree> compilationUn
.result();
});
}

private boolean inNullMarkedScope() {
return getCurrentType().getDeclaration().isNullMarked();
}
}

0 comments on commit bab9e6e

Please sign in to comment.