Skip to content

Commit

Permalink
Ban constructor injection on Kotlin inline value classes
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 585117945
  • Loading branch information
kuanyingchou authored and Dagger Team committed Nov 24, 2023
1 parent 7121b67 commit fdb8ec8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions java/dagger/internal/codegen/validation/InjectValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ private ValidationReport validateConstructor(XConstructorElement constructorElem
constructorElement);
}

if (enclosingElement.isValueClass()) {
builder.addError(
String.format("@%s constructors on Kotlin inline value classes is not supported",
injectAnnotation.simpleName()),
constructorElement);
}

// Note: superficial validation of the annotations is done as part of getting the scopes.
ImmutableSet<Scope> scopes =
injectionAnnotations.getScopes(constructorElement.getEnclosingElement());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ public final class InjectConstructorFactoryGeneratorTest {
});
}

@Test public void injectConstructorOnInlineValueClass() {
Source file =
CompilerTests.kotlinSource(
"test.InlineValueClass.kt",
"package test",
"",
"import javax.inject.Inject;",
"",
"@JvmInline value class InlineValueClass",
" @Inject constructor(private val v: Int)");
CompilerTests.daggerCompiler(file)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"@Inject constructors on Kotlin inline value classes is not supported")
.onSource(file)
.onLine(6);
});
}

@Test public void fieldAndMethodGenerics() {
Source file =
CompilerTests.javaSource(
Expand Down

0 comments on commit fdb8ec8

Please sign in to comment.