Skip to content

Commit

Permalink
Fix IllegalArgumentException when requesting Map<K,Provider<V>>.
Browse files Browse the repository at this point in the history
This CL fixes b/370367984.

RELNOTES=N/A
PiperOrigin-RevId: 681538199
  • Loading branch information
bcorso authored and Dagger Team committed Oct 2, 2024
1 parent 1338062 commit 826c2f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ ImmutableSet<ContributionBinding> bindings(Key key) {
ImmutableSet<DelegateDeclaration> delegates(Key key) {
// @Binds @IntoMap declarations have key Map<K, V> but may be requested as
// Map<K, Provider/Producer<V>> keys, so unwrap the multibinding map contribution key first.
return delegates.get(keyFactory.unwrapMapValueType(key));
return delegates.get(
key.multibindingContributionIdentifier().isPresent()
// TODO(bcorso): Consider using TypeNameKey here instead of Key, to avoid losing
// variance information when unwrapping KSP types (see TypeNameKey's javadoc).
? keyFactory.unwrapMapValueType(key)
: key);
}

/**
Expand Down
22 changes: 6 additions & 16 deletions javatests/dagger/internal/codegen/BindsMethodValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package dagger.internal.codegen;

import static com.google.common.truth.Truth.assertThat;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatMethodInUnannotatedClass;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatModuleMethod;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assert.assertThrows;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.util.Source;
Expand Down Expand Up @@ -436,20 +434,12 @@ public void bindsMapKVAndRequestMapKProviderV_failsWithMissingBindingError() {
"@Qualifier @interface TestQualifier {}");
Source k = CompilerTests.javaSource("test.K", "package test;", "interface K {}");
Source v = CompilerTests.javaSource("test.V", "package test;", "interface V {}");
// TODO(b/370367984): Once this bug is fixed, no exception should be thrown.
RuntimeException expectedException =
assertThrows(
RuntimeException.class,
() -> CompilerTests.daggerCompiler(component, module, qualifier, k, v)
.compile(subject -> {}));
assertThat(expectedException)
.hasMessageThat()
.contains(
"no expression found for BindingRequest{"
+ "key=java.util.Map<test.K,javax.inject.Provider<test.V>>, "
+ "requestKind=INSTANCE, "
+ "frameworkType=Optional.empty"
+ "}");
CompilerTests.daggerCompiler(component, module, qualifier, k, v)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining("Map<test.K,Provider<test.V>> cannot be provided");
});
}

private DaggerModuleMethodSubject assertThatMethod(String method) {
Expand Down

0 comments on commit 826c2f1

Please sign in to comment.