Skip to content

Commit 216a9d2

Browse files
bcorsoDagger Team
authored andcommitted
Fix broken assertion in RequiresResolutionChecker#visitUncachedDependencies()
Within `visitUncachedDependencies()` we assert that each uncached key is actually uncached before caching it. However, this assertion can fail because during the process of caching, we call `getLocalExplicitBindings()` and `getLocalMultibindingContributions()` which can trigger its own call to `visitUncachedDependencies()` and cache it first. This CL fixes this issue by avoiding the calls to `getLocalExplicitBindings()` and `getLocalMultibindingContributions()`. In these cases we don't actually need the bindings themselves since we just need to know if the declarations exist or not, so we can just use the declarations directly. This saves us a bit of work because creating the binding requires us to not only resolve the binding itself, but also bindings for dependencies (possibly even transitive dependencies) in the case `@Binds` delegate declarations. It's also nice to keep the assertion because it gaurantees some simplicity to the code. RELNOTES=N/A PiperOrigin-RevId: 653633501
1 parent 982dab4 commit 216a9d2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

java/dagger/internal/codegen/binding/BindingGraphFactory.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ void resolve(Key key) {
725725
.anyMatch(binding -> binding.kind() == BindingKind.ASSISTED_INJECTION);
726726
if (!isAssistedInjectionBinding
727727
&& !requiresResolution(key)
728-
&& getLocalExplicitBindings(key).isEmpty()) {
728+
&& !hasLocalExplicitBindings(key)) {
729729
/* Cache the inherited parent component's bindings in case resolving at the parent found
730730
* bindings in some component between this one and the previously-resolved one. */
731731
resolvedContributionBindings.put(key, previouslyResolvedBindings);
@@ -867,9 +867,12 @@ private boolean hasLocalBindings(ResolvedBindings resolvedBindings) {
867867
* this component's modules that matches the key.
868868
*/
869869
private boolean hasLocalMultibindingContributions(Key requestKey) {
870-
return keysMatchingRequest(requestKey)
871-
.stream()
872-
.anyMatch(key -> !getLocalMultibindingContributions(key).isEmpty());
870+
return keysMatchingRequest(requestKey).stream()
871+
.anyMatch(
872+
multibindingKey ->
873+
!declarations.multibindingContributions(multibindingKey).isEmpty()
874+
|| !declarations.delegateMultibindingContributions(
875+
keyFactory.unwrapMapValueType(multibindingKey)).isEmpty());
873876
}
874877

875878
/**
@@ -886,14 +889,21 @@ private boolean hasLocalOptionalBindingContribution(
886889
if (previouslyResolvedBindings.stream()
887890
.map(Binding::kind)
888891
.anyMatch(isEqual(OPTIONAL))) {
889-
return !getLocalExplicitBindings(keyFactory.unwrapOptional(key).get())
890-
.isEmpty();
892+
return hasLocalExplicitBindings(keyFactory.unwrapOptional(key).get());
891893
} else {
892894
// If a parent contributes a @Provides Optional<Foo> binding and a child has a
893895
// @BindsOptionalOf Foo method, the two should conflict, even if there is no binding for
894896
// Foo on its own
895897
return !getOptionalBindingDeclarations(key).isEmpty();
896898
}
897899
}
900+
901+
/**
902+
* Returns {@code true} if there is at least one explicit binding that matches the given key.
903+
*/
904+
private boolean hasLocalExplicitBindings(Key requestKey) {
905+
return !declarations.bindings(requestKey).isEmpty()
906+
|| !declarations.delegates(keyFactory.unwrapMapValueType(requestKey)).isEmpty();
907+
}
898908
}
899909
}

0 commit comments

Comments
 (0)