Skip to content

Commit

Permalink
Rollforward: Create casts expressions with the nullability from the s…
Browse files Browse the repository at this point in the history
…cope where they appear in.

In addition to the original cl, this cl makes JsDoc casts to be always to a nullable type to circumvent jscompiler error when casting null.

PiperOrigin-RevId: 487953635
  • Loading branch information
rluble authored and copybara-github committed Nov 12, 2022
1 parent 5e8c52e commit 55cf0ed
Show file tree
Hide file tree
Showing 16 changed files with 711 additions and 437 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,12 @@ private BooleanLiteral convert(org.eclipse.jdt.core.dom.BooleanLiteral literal)

private CastExpression convert(org.eclipse.jdt.core.dom.CastExpression expression) {
TypeDescriptor castTypeDescriptor =
environment.createTypeDescriptor(expression.getType().resolveBinding());
environment.createTypeDescriptor(
expression.getType().resolveBinding(),
getCurrentType().getDeclaration().isNullMarked());
return CastExpression.newBuilder()
.setExpression(convert(expression.getExpression()))
.setCastTypeDescriptor(castTypeDescriptor)
.setCastTypeDescriptor(castTypeDescriptor.toNullable())
.build();
}

Expand Down Expand Up @@ -1426,7 +1428,6 @@ private Type createType(ITypeBinding typeBinding, ASTNode sourcePositionNode) {
type.setStatic(JdtEnvironment.isStatic(typeBinding));
return type;
}

}

private CompilationUnit buildCompilationUnit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ open class CastGenerics<T, E: Number?> {

open fun testCastToTypeVariable() {
val o: Any? = Int(1)
val e: E? = o as E
val t: T? = o as T
val e: E? = o as E?
val t: T? = o as T?
val es: Array<E>? = o as Array<E>?
val ts: Array<T>? = o as Array<T>?
}

open fun <S, V: Enum<V>> testCastToMethodTypeVariable() {
val o: Any? = Int(1)
val s: S? = o as S
val s: S? = o as S?
var c: Any? = o as CastGenerics<S, Number?>?
c = o as Array<S>?
c = o as V
c = o as V?
}

open fun <TT: Enum<*>> outerGenericMethod() {
open class ___1Nested<SS> {
fun nestedGenericMethod_private_2(o: Any?) {
val t: TT? = o as TT
val t: TT? = o as TT?
}
}
}

open fun <EE> method(o: Any?): EE where EE: CastGenerics.Empty1?, EE: CastGenerics.Empty2<EE>? {
if (o is CastGenerics.Empty1) {
return o as EE
return (o as EE?) as EE
}
return null as EE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ open class IntersectionTypeTest<U> {

@JvmStatic
fun <T> m_private_2(): T where T: IntersectionTypeTest.A?, T: IntersectionTypeTest.EmptyA? {
return IntersectionTypeTest.get_private_2<Any?>(Any()) as T
return (IntersectionTypeTest.get_private_2<Any?>(Any()) as T?) as T
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ static void parametrizedDefaultNullabilityMethod(
o.defaultNullability.length();
}

public void casts() {
List<String> listOfString = (List<String>) null;
@Nullable List<String> nullableListOfString = (@Nullable List<String>) null;
List<@Nullable String> listOfNullableString = (List<@Nullable String>) null;
@Nullable List<@Nullable String> nullableListOfNullableString =
(@Nullable List<@Nullable String>) null;
@JsNonNull List<String> nonNullListOfString = (@JsNonNull List<String>) null;
List<@JsNonNull String> listOfNonNullString = (List<@JsNonNull String>) null;
@JsNonNull
List<@JsNonNull String> nonNullListOfNonNullString = (@JsNonNull List<@JsNonNull String>) null;
}

// Wildcards
static void testListOfWildcard(List<?> l) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ static void parametrizedDefaultNullabilityMethod(
o.defaultNullability.length();
}

public void casts() {
List<String> listOfString = (List<String>) null;
@Nullable List<String> nullableListOfString = (@Nullable List<String>) null;
List<@Nullable String> listOfNullableString = (List<@Nullable String>) null;
@Nullable List<@Nullable String> nullableListOfNullableString =
(@Nullable List<@Nullable String>) null;
@JsNonNull List<String> nonNullListOfString = (@JsNonNull List<String>) null;
List<@JsNonNull String> listOfNonNullString = (List<@JsNonNull String>) null;
@JsNonNull
List<@JsNonNull String> nonNullListOfNonNullString = (@JsNonNull List<@JsNonNull String>) null;
}

// Wildcards
static void testListOfWildcard(List<?> l) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ static void parametrizedDefaultNullabilityMethod(
o.defaultNullability.length();
}

public void casts() {
List<String> listOfString = (List<String>) null;
@Nullable List<String> nullableListOfString = (@Nullable List<String>) null;
List<@Nullable String> listOfNullableString = (List<@Nullable String>) null;
@Nullable List<@Nullable String> nullableListOfNullableString =
(@Nullable List<@Nullable String>) null;
@JsNonNull List<String> nonNullListOfString = (@JsNonNull List<String>) null;
List<@JsNonNull String> listOfNonNullString = (List<@JsNonNull String>) null;
@JsNonNull
List<@JsNonNull String> nonNullListOfNonNullString = (@JsNonNull List<@JsNonNull String>) null;
}

// Wildcards
static void testListOfWildcard(List<?> l) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ class DefaultNotNullable extends j_l_Object {
j_l_String.m_length__java_lang_String__int(/**@type {string}*/ ($Casts.$to(o.f_defaultNullability__nullability_defaultnotnullable_DefaultNotNullable_ParameterizedDefaultNullability, j_l_String)));
}
/** @nodts */
m_casts__void() {
let listOfString = /**@type {List<string>}*/ (null);
let nullableListOfString = /**@type {List<string>}*/ (null);
let listOfNullableString = /**@type {List<?string>}*/ (null);
let nullableListOfNullableString = /**@type {List<?string>}*/ (null);
let nonNullListOfString = /**@type {List<string>}*/ (null);
let listOfNonNullString = /**@type {List<string>}*/ (null);
let nonNullListOfNonNullString = /**@type {List<string>}*/ (null);
}
/** @nodts */
static m_testListOfWildcard__java_util_List__void(/** !List<?> */ l) {
DefaultNotNullable.$clinit();
}
Expand Down Expand Up @@ -281,7 +291,7 @@ class DefaultNotNullable extends j_l_Object {
/** @nodts */
static m_testLocalNullability__void() {
DefaultNotNullable.$clinit();
let stringConsumer = /**@type {Consumer<?string>}*/ (null);
let stringConsumer = /**@type {Consumer<string>}*/ (null);
let nullableStringConsumer = /**@type {Consumer<?string>}*/ (null);
let nonNullStringConsumer = /**@type {Consumer<string>}*/ (null);
let b = Consumer.$isInstance(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@
[o.nullable.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {?string}*/ ($Casts.$to(o.f_nullable__nullability_defaultnotnullable_DefaultNotNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.defaultnotnullable.DefaultNotNullable.parametrizedDefaultNullabilityMethod"
[o.nonNullable.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {string}*/ ($Casts.$to(o.f_nonNullable__nullability_defaultnotnullable_DefaultNotNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.defaultnotnullable.DefaultNotNullable.parametrizedDefaultNullabilityMethod"
[o.defaultNullability.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {string}*/ ($Casts.$to(o.f_defaultNullability__nullability_defaultnotnullable_DefaultNotNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.defaultnotnullable.DefaultNotNullable.parametrizedDefaultNullabilityMethod"
[casts] => [m_casts__void]
[List<String> listOfString = (List<String>) null;] => [let listOfString = /**@type {List<string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.casts"
[listOfString] => [listOfString] "listOfString"
[@Nullable List<String> nullableListOfString = (@Nullable List<String>) null;] => [let nullableListOfString = /**@type {List<string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.casts"
[nullableListOfString] => [nullableListOfString] "nullableListOfString"
[List<@Nullable String> listOfNullableString = (List<@Nullable String>) null;] => [let listOfNullableString = /**@type {List<?string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.casts"
[listOfNullableString] => [listOfNullableString] "listOfNullableString"
[@Nullable List<@Nullable String> nullableListOfNullableString =
(@Nullable List<@Nullable String>) null;] => [let nullableListOfNullableString = /**@type {List<?string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.casts"
[nullableListOfNullableString] => [nullableListOfNullableString] "nullableListOfNullableString"
[@JsNonNull List<String> nonNullListOfString = (@JsNonNull List<String>) null;] => [let nonNullListOfString = /**@type {List<string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.casts"
[nonNullListOfString] => [nonNullListOfString] "nonNullListOfString"
[List<@JsNonNull String> listOfNonNullString = (List<@JsNonNull String>) null;] => [let listOfNonNullString = /**@type {List<string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.casts"
[listOfNonNullString] => [listOfNonNullString] "listOfNonNullString"
[@JsNonNull
List<@JsNonNull String> nonNullListOfNonNullString = (@JsNonNull List<@JsNonNull String>) null;] => [let nonNullListOfNonNullString = /**@type {List<string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.casts"
[nonNullListOfNonNullString] => [nonNullListOfNonNullString] "nonNullListOfNonNullString"
[testListOfWildcard] => [m_testListOfWildcard__java_util_List__void]
[l] => [l] "l"
[{}] => [DefaultNotNullable.$clinit();] "nullability.defaultnotnullable.DefaultNotNullable.testListOfWildcard"
Expand Down Expand Up @@ -301,7 +318,7 @@
Consumer<String> stringConsumer = (Consumer<String>) null;
... boolean b = null instanceof Consumer<?>;
}] => [DefaultNotNullable.$clinit();] "nullability.defaultnotnullable.DefaultNotNullable.testLocalNullability"
[Consumer<String> stringConsumer = (Consumer<String>) null;] => [let stringConsumer = /**@type {Consumer<?string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.testLocalNullability"
[Consumer<String> stringConsumer = (Consumer<String>) null;] => [let stringConsumer = /**@type {Consumer<string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.testLocalNullability"
[stringConsumer] => [stringConsumer] "stringConsumer"
[Consumer<@Nullable String> nullableStringConsumer = (Consumer<@Nullable String>) null;] => [let nullableStringConsumer = /**@type {Consumer<?string>}*/ (null);] "nullability.defaultnotnullable.DefaultNotNullable.testLocalNullability"
[nullableStringConsumer] => [nullableStringConsumer] "nullableStringConsumer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ class DefaultNullable extends j_l_Object {
j_l_String.m_length__java_lang_String__int(/**@type {?string}*/ ($Casts.$to(o.f_defaultNullability__nullability_defaultnullable_DefaultNullable_ParameterizedDefaultNullability, j_l_String)));
}
/** @nodts */
m_casts__void() {
let listOfString = /**@type {List<?string>}*/ (null);
let nullableListOfString = /**@type {List<?string>}*/ (null);
let listOfNullableString = /**@type {List<?string>}*/ (null);
let nullableListOfNullableString = /**@type {List<?string>}*/ (null);
let nonNullListOfString = /**@type {List<?string>}*/ (null);
let listOfNonNullString = /**@type {List<string>}*/ (null);
let nonNullListOfNonNullString = /**@type {List<string>}*/ (null);
}
/** @nodts */
static m_testListOfWildcard__java_util_List__void(/** List<?> */ l) {
DefaultNullable.$clinit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@
[o.nullable.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {?string}*/ ($Casts.$to(o.f_nullable__nullability_defaultnullable_DefaultNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.defaultnullable.DefaultNullable.parametrizedDefaultNullabilityMethod"
[o.nonNullable.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {string}*/ ($Casts.$to(o.f_nonNullable__nullability_defaultnullable_DefaultNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.defaultnullable.DefaultNullable.parametrizedDefaultNullabilityMethod"
[o.defaultNullability.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {?string}*/ ($Casts.$to(o.f_defaultNullability__nullability_defaultnullable_DefaultNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.defaultnullable.DefaultNullable.parametrizedDefaultNullabilityMethod"
[casts] => [m_casts__void]
[List<String> listOfString = (List<String>) null;] => [let listOfString = /**@type {List<?string>}*/ (null);] "nullability.defaultnullable.DefaultNullable.casts"
[listOfString] => [listOfString] "listOfString"
[@Nullable List<String> nullableListOfString = (@Nullable List<String>) null;] => [let nullableListOfString = /**@type {List<?string>}*/ (null);] "nullability.defaultnullable.DefaultNullable.casts"
[nullableListOfString] => [nullableListOfString] "nullableListOfString"
[List<@Nullable String> listOfNullableString = (List<@Nullable String>) null;] => [let listOfNullableString = /**@type {List<?string>}*/ (null);] "nullability.defaultnullable.DefaultNullable.casts"
[listOfNullableString] => [listOfNullableString] "listOfNullableString"
[@Nullable List<@Nullable String> nullableListOfNullableString =
(@Nullable List<@Nullable String>) null;] => [let nullableListOfNullableString = /**@type {List<?string>}*/ (null);] "nullability.defaultnullable.DefaultNullable.casts"
[nullableListOfNullableString] => [nullableListOfNullableString] "nullableListOfNullableString"
[@JsNonNull List<String> nonNullListOfString = (@JsNonNull List<String>) null;] => [let nonNullListOfString = /**@type {List<?string>}*/ (null);] "nullability.defaultnullable.DefaultNullable.casts"
[nonNullListOfString] => [nonNullListOfString] "nonNullListOfString"
[List<@JsNonNull String> listOfNonNullString = (List<@JsNonNull String>) null;] => [let listOfNonNullString = /**@type {List<string>}*/ (null);] "nullability.defaultnullable.DefaultNullable.casts"
[listOfNonNullString] => [listOfNonNullString] "listOfNonNullString"
[@JsNonNull
List<@JsNonNull String> nonNullListOfNonNullString = (@JsNonNull List<@JsNonNull String>) null;] => [let nonNullListOfNonNullString = /**@type {List<string>}*/ (null);] "nullability.defaultnullable.DefaultNullable.casts"
[nonNullListOfNonNullString] => [nonNullListOfNonNullString] "nonNullListOfNonNullString"
[testListOfWildcard] => [m_testListOfWildcard__java_util_List__void]
[l] => [l] "l"
[{}] => [DefaultNullable.$clinit();] "nullability.defaultnullable.DefaultNullable.testListOfWildcard"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ class ExplicitNotNullable extends j_l_Object {
j_l_String.m_length__java_lang_String__int(/**@type {string}*/ ($Casts.$to(o.f_defaultNullability__nullability_explicitnotnullable_ExplicitNotNullable_ParameterizedDefaultNullability, j_l_String)));
}
/** @nodts */
m_casts__void() {
let listOfString = /**@type {List<string>}*/ (null);
let nullableListOfString = /**@type {List<string>}*/ (null);
let listOfNullableString = /**@type {List<?string>}*/ (null);
let nullableListOfNullableString = /**@type {List<?string>}*/ (null);
let nonNullListOfString = /**@type {List<string>}*/ (null);
let listOfNonNullString = /**@type {List<string>}*/ (null);
let nonNullListOfNonNullString = /**@type {List<string>}*/ (null);
}
/** @nodts */
static m_testListOfWildcard__java_util_List__void(/** !List<?> */ l) {
ExplicitNotNullable.$clinit();
}
Expand Down Expand Up @@ -323,7 +333,7 @@ class ExplicitNotNullable extends j_l_Object {
/** @nodts */
static m_testLocalNullability__void() {
ExplicitNotNullable.$clinit();
let stringConsumer = /**@type {Consumer<?string>}*/ (null);
let stringConsumer = /**@type {Consumer<string>}*/ (null);
let nullableStringConsumer = /**@type {Consumer<?string>}*/ (null);
let nonNullStringConsumer = /**@type {Consumer<string>}*/ (null);
let b = Consumer.$isInstance(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@
[o.nullable.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {?string}*/ ($Casts.$to(o.f_nullable__nullability_explicitnotnullable_ExplicitNotNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.explicitnotnullable.ExplicitNotNullable.parametrizedDefaultNullabilityMethod"
[o.nonNullable.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {string}*/ ($Casts.$to(o.f_nonNullable__nullability_explicitnotnullable_ExplicitNotNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.explicitnotnullable.ExplicitNotNullable.parametrizedDefaultNullabilityMethod"
[o.defaultNullability.length();] => [j_l_String.m_length__java_lang_String__int(/**@type {string}*/ ($Casts.$to(o.f_defaultNullability__nullability_explicitnotnullable_ExplicitNotNullable_ParameterizedDefaultNullability, j_l_String)));] "nullability.explicitnotnullable.ExplicitNotNullable.parametrizedDefaultNullabilityMethod"
[casts] => [m_casts__void]
[List<String> listOfString = (List<String>) null;] => [let listOfString = /**@type {List<string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.casts"
[listOfString] => [listOfString] "listOfString"
[@Nullable List<String> nullableListOfString = (@Nullable List<String>) null;] => [let nullableListOfString = /**@type {List<string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.casts"
[nullableListOfString] => [nullableListOfString] "nullableListOfString"
[List<@Nullable String> listOfNullableString = (List<@Nullable String>) null;] => [let listOfNullableString = /**@type {List<?string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.casts"
[listOfNullableString] => [listOfNullableString] "listOfNullableString"
[@Nullable List<@Nullable String> nullableListOfNullableString =
(@Nullable List<@Nullable String>) null;] => [let nullableListOfNullableString = /**@type {List<?string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.casts"
[nullableListOfNullableString] => [nullableListOfNullableString] "nullableListOfNullableString"
[@JsNonNull List<String> nonNullListOfString = (@JsNonNull List<String>) null;] => [let nonNullListOfString = /**@type {List<string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.casts"
[nonNullListOfString] => [nonNullListOfString] "nonNullListOfString"
[List<@JsNonNull String> listOfNonNullString = (List<@JsNonNull String>) null;] => [let listOfNonNullString = /**@type {List<string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.casts"
[listOfNonNullString] => [listOfNonNullString] "listOfNonNullString"
[@JsNonNull
List<@JsNonNull String> nonNullListOfNonNullString = (@JsNonNull List<@JsNonNull String>) null;] => [let nonNullListOfNonNullString = /**@type {List<string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.casts"
[nonNullListOfNonNullString] => [nonNullListOfNonNullString] "nonNullListOfNonNullString"
[testListOfWildcard] => [m_testListOfWildcard__java_util_List__void]
[l] => [l] "l"
[{}] => [ExplicitNotNullable.$clinit();] "nullability.explicitnotnullable.ExplicitNotNullable.testListOfWildcard"
Expand Down Expand Up @@ -341,7 +358,7 @@
Consumer<String> stringConsumer = (Consumer<String>) null;
... boolean b = null instanceof Consumer<?>;
}] => [ExplicitNotNullable.$clinit();] "nullability.explicitnotnullable.ExplicitNotNullable.testLocalNullability"
[Consumer<String> stringConsumer = (Consumer<String>) null;] => [let stringConsumer = /**@type {Consumer<?string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.testLocalNullability"
[Consumer<String> stringConsumer = (Consumer<String>) null;] => [let stringConsumer = /**@type {Consumer<string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.testLocalNullability"
[stringConsumer] => [stringConsumer] "stringConsumer"
[Consumer<@Nullable String> nullableStringConsumer = (Consumer<@Nullable String>) null;] => [let nullableStringConsumer = /**@type {Consumer<?string>}*/ (null);] "nullability.explicitnotnullable.ExplicitNotNullable.testLocalNullability"
[nullableStringConsumer] => [nullableStringConsumer] "nullableStringConsumer"
Expand Down
Loading

0 comments on commit 55cf0ed

Please sign in to comment.