JPS mappings for incremental compilation refactoring: more precise handling of default parameter definition changes

GitOrigin-RevId: 5debe385629850cb5c0115318638f7650cb60cad
This commit is contained in:
Eugene Zhuravlev
2024-04-10 21:50:53 +02:00
committed by intellij-monorepo-bot
parent 85afa4fbc6
commit 44a4e0097a
4 changed files with 44 additions and 39 deletions

View File

@@ -143,6 +143,7 @@ f:org.jetbrains.jps.javac.Iterators
- s:asIterator(java.lang.Object):java.util.Iterator
- s:collect(java.lang.Iterable,java.util.Collection):java.util.Collection
- s:contains(java.lang.Iterable,java.lang.Object):Z
- s:count(java.lang.Iterable):I
- s:equals(java.lang.Iterable,java.lang.Iterable):Z
- s:equals(java.lang.Iterable,java.lang.Iterable,org.jetbrains.jps.javac.Iterators$BiFunction):Z
- s:filter(java.lang.Iterable,org.jetbrains.jps.javac.Iterators$BooleanFunction):java.lang.Iterable

View File

@@ -36,6 +36,17 @@ public final class Iterators {
return false;
}
public static int count(Iterable<?> iterable) {
if (iterable instanceof Collection) {
return ((Collection<?>)iterable).size();
}
int count = 0;
for (Object obj: iterable) {
count += 1;
}
return count;
}
public static <T> T find(Iterable<? extends T> iterable, BooleanFunction<? super T> cond) {
if (iterable != null) {
for (T o : iterable) {

View File

@@ -271,7 +271,7 @@ public final class KotlinMeta implements JvmMetadata<KotlinMeta, KotlinMeta.Diff
@Override
public boolean unchanged() {
return !becameNullable() && !argsBecameNotNull() && !receiverParameterChanged() && !visibilityChanged() && !defaultValueDeclarationChanged();
return !becameNullable() && !argsBecameNotNull() && !receiverParameterChanged() && !visibilityChanged() && !hasDefaultDeclarationChanges();
}
public boolean becameNullable() {
@@ -313,20 +313,13 @@ public final class KotlinMeta implements JvmMetadata<KotlinMeta, KotlinMeta.Diff
return nowType == null || !Objects.equals(pastType.getClassifier(), nowType.getClassifier());
}
public boolean defaultValueDeclarationChanged() {
Iterator<KmValueParameter> nowParams = Iterators.reverse(now.getValueParameters()).iterator();
for (KmValueParameter pastParam : Iterators.reverse(past.getValueParameters())) {
KmValueParameter nowParam = nowParams.hasNext()? nowParams.next() : null;
if (nowParam != null? Attributes.getDeclaresDefaultValue(nowParam) != Attributes.getDeclaresDefaultValue(pastParam) : Attributes.getDeclaresDefaultValue(pastParam)) {
return true;
}
public boolean hasDefaultDeclarationChanges() {
int before = Iterators.count(Iterators.filter(past.getValueParameters(), Attributes::getDeclaresDefaultValue));
int after = Iterators.count(Iterators.filter(now.getValueParameters(), Attributes::getDeclaresDefaultValue));
if (before == 0) {
return after > 0; // there were no default declarations, but some parameters now define default values
}
while (nowParams.hasNext()) {
if (Attributes.getDeclaresDefaultValue(nowParams.next())) {
return true;
}
}
return false;
return after < before; // default definitions still exist, but some parameters do not define default values anymore
}
private static Iterable<KmType> getParameterTypes(KmFunction f) {
@@ -346,7 +339,7 @@ public final class KotlinMeta implements JvmMetadata<KotlinMeta, KotlinMeta.Diff
@Override
public boolean unchanged() {
return !argsBecameNotNull() && !visibilityChanged() && !defaultValueDeclarationChanged();
return !argsBecameNotNull() && !visibilityChanged() && !hasDefaultDeclarationChanges();
}
public boolean visibilityChanged() {
@@ -373,20 +366,13 @@ public final class KotlinMeta implements JvmMetadata<KotlinMeta, KotlinMeta.Diff
return false;
}
public boolean defaultValueDeclarationChanged() {
Iterator<KmValueParameter> nowParams = Iterators.reverse(now.getValueParameters()).iterator();
for (KmValueParameter pastParam : Iterators.reverse(past.getValueParameters())) {
KmValueParameter nowParam = nowParams.hasNext()? nowParams.next() : null;
if (nowParam != null? Attributes.getDeclaresDefaultValue(nowParam) != Attributes.getDeclaresDefaultValue(pastParam) : Attributes.getDeclaresDefaultValue(pastParam)) {
return true;
}
public boolean hasDefaultDeclarationChanges() {
int before = Iterators.count(Iterators.filter(past.getValueParameters(), Attributes::getDeclaresDefaultValue));
int after = Iterators.count(Iterators.filter(now.getValueParameters(), Attributes::getDeclaresDefaultValue));
if (before == 0) {
return after > 0; // there were no default declarations, but some parameters now define default values
}
while (nowParams.hasNext()) {
if (Attributes.getDeclaresDefaultValue(nowParams.next())) {
return true;
}
}
return false;
return after < before; // default definitions still exist, but some parameters do not define default values anymore
}
private static Iterable<KmType> getParameterTypes(KmConstructor f) {

View File

@@ -196,6 +196,15 @@ public final class KotlinAwareJavaDifferentiateStrategy extends JvmDifferentiate
}
}
for (KmFunction func : flat(metaDiff.functions().removed(), metaDiff.functions().added())) {
Visibility visibility = Attributes.getVisibility(func);
if (visibility == Visibility.PRIVATE || visibility == Visibility.PRIVATE_TO_THIS || find(func.getValueParameters(), Attributes::getDeclaresDefaultValue) == null) {
continue;
}
debug("Removed or added function declares default values: ", changedClass.getName());
affectMemberLookupUsages(context, changedClass, func.getName(), future);
}
for (Difference.Change<KmFunction, KotlinMeta.KmFunctionsDiff> funChange : metaDiff.functions().changed()) {
KmFunction changedKmFunction = funChange.getPast();
Visibility visibility = Attributes.getVisibility(changedKmFunction);
@@ -221,21 +230,19 @@ public final class KotlinAwareJavaDifferentiateStrategy extends JvmDifferentiate
affectMemberLookupUsages(context, changedClass, changedKmFunction.getName(), future);
}
}
if (funDiff.receiverParameterChanged() || funDiff.defaultValueDeclarationChanged()) {
debug("Function's receiver parameter changed or parameter default value declaration changed: ", changedKmFunction.getName());
if (funDiff.receiverParameterChanged() || funDiff.hasDefaultDeclarationChanges()) {
debug("Function's receiver parameter changed or function has breaking changes in default value declarations: ", changedKmFunction.getName());
affectMemberLookupUsages(context, changedClass, changedKmFunction.getName(), future);
}
}
for (KmConstructor removedCons : metaDiff.constructors().removed()) {
Visibility visibility = Attributes.getVisibility(removedCons);
if (visibility == Visibility.PRIVATE || visibility == Visibility.PRIVATE_TO_THIS) {
for (KmConstructor con : flat(metaDiff.constructors().removed(), metaDiff.constructors().added())) {
Visibility visibility = Attributes.getVisibility(con);
if (visibility == Visibility.PRIVATE || visibility == Visibility.PRIVATE_TO_THIS || find(con.getValueParameters(), Attributes::getDeclaresDefaultValue) == null) {
continue;
}
if (find(removedCons.getValueParameters(), Attributes::getDeclaresDefaultValue) != null) {
debug("Removed constructor's contained default value declarations: ", changedClass.getName());
affectClassLookupUsages(context, changedClass);
}
debug("Removed or added constructor declares default values: ", changedClass.getName());
affectClassLookupUsages(context, changedClass);
}
for (Difference.Change<KmConstructor, KotlinMeta.KmConstructorsDiff> conChange : metaDiff.constructors().changed()) {
@@ -245,8 +252,8 @@ public final class KotlinAwareJavaDifferentiateStrategy extends JvmDifferentiate
continue;
}
KotlinMeta.KmConstructorsDiff conDiff = conChange.getDiff();
if (conDiff.argsBecameNotNull() || conDiff.accessRestricted() || conDiff.defaultValueDeclarationChanged()) {
debug("Constructor's args became non-nullable; or the constructor has become less accessible or default value declaration changed: ", changedClass.getName());
if (conDiff.argsBecameNotNull() || conDiff.accessRestricted() || conDiff.hasDefaultDeclarationChanges()) {
debug("Constructor's args became non-nullable; or the constructor has become less accessible or has breaking changes in default value declarations: ", changedClass.getName());
affectClassLookupUsages(context, changedClass);
}
}