Skip to content

Commit

Permalink
Merge pull request #880 from jqno/scala-caseclass-checkinit
Browse files Browse the repository at this point in the history
Support scalac -Xcheckinit
  • Loading branch information
jqno authored Nov 29, 2023
2 parents 8df346e + 2bac2f6 commit ff19e65
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- A check for Scala with its `-Xcheckinit` flag switched on. This flag generates a field that should have been marked as synthetic, but isn't, so EqualsVerifier has to check for this field explicitly.

## [3.15.3] - 2023-11-01

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ private List<Field> addFieldsFor(Class<?> c) {
List<Field> statics = new ArrayList<>();

for (Field field : c.getDeclaredFields()) {
if (!field.isSynthetic() && !"__cobertura_counters".equals(field.getName())) {
if (
!field.isSynthetic() &&
!"__cobertura_counters".equals(field.getName()) &&
!field.getName().startsWith("bitmap$init$") // Generated by Scala 2.x's -Xcheckinit flag
) {
boolean isStatic = Modifier.isStatic(field.getModifiers());
if (isStatic && includeStatic) {
statics.add(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void succeed_whenClassIsInstrumentedByCobertura_givenCoberturaDoesntMarkI
EqualsVerifier.forClass(CoberturaContainer.class).verify();
}

@Test
public void succeed_whenScalacOptionCheckinitIsEnabled_givenScalaDoesntMarkItsFieldsSynthetic() {
EqualsVerifier.forClass(ScalaCheckinit.class).verify();
}

static final class LambdaContainer {

private static final Comparator<LambdaContainer> COMPARATOR = (c1, c2) -> 0; // A lambda is a synthetic class
Expand Down Expand Up @@ -152,4 +157,32 @@ public int hashCode() {
return defaultHashCode(this);
}
}

public static final class ScalaCheckinit {

// CHECKSTYLE OFF: MemberName
// This field is generated by the Scala 2.x compiler when -Xcheckinit is switched on.
// However, it's not marked as 'synthetic', so EqualsVerifier doesn't ignore it automatically.
private volatile byte bitmap$init$0 = 0;
// CHECKSTYLE ON: MemberName
private final int i;

public ScalaCheckinit(int i) {
this.i = i;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof ScalaCheckinit)) {
return false;
}
ScalaCheckinit p = (ScalaCheckinit) obj;
return p.i == i;
}

@Override
public int hashCode() {
return Objects.hash(i);
}
}
}

0 comments on commit ff19e65

Please sign in to comment.