diff --git a/dagger-compiler/main/java/dagger/internal/codegen/base/DaggerSuperficialValidation.java b/dagger-compiler/main/java/dagger/internal/codegen/base/DaggerSuperficialValidation.java index 2efa17b88d8..3cfd03329ba 100644 --- a/dagger-compiler/main/java/dagger/internal/codegen/base/DaggerSuperficialValidation.java +++ b/dagger-compiler/main/java/dagger/internal/codegen/base/DaggerSuperficialValidation.java @@ -265,6 +265,22 @@ private void validateTypeHierarchy(String desc, XType type, Set visit .forEach(typeArg -> validateTypeHierarchy("type argument", typeArg, visited)); type.getSuperTypes() .forEach(supertype -> validateTypeHierarchy("supertype", supertype, visited)); + + // XType.getSuperTypes() misses some error types due to a bug in Javac that causes error + // types to be excluded from the supertype list (b/444278301). To work around this, we + // additionally check XTypeElement.getSuperClass() and XTypeElement.getSuperInterfaces() + // to ensure we catch them. + // TODO(b/444278301): Remove this workaround once this Javac bug is fixed. + XTypeElement typeElement = type.getTypeElement(); + if (typeElement != null) { + if (typeElement.getSuperClass() != null) { + validateTypeHierarchy("supertype", typeElement.getSuperClass(), visited); + } + typeElement + .getSuperInterfaces() + .forEach( + superInterface -> validateTypeHierarchy("supertype", superInterface, visited)); + } } else if (isTypeVariable(type)) { asTypeVariable(type) .getUpperBounds() @@ -617,6 +633,7 @@ private ValidationException append(XExecutableType type) { Ascii.toLowerCase(getKindName(type)), XExecutableTypes.toStableString(type))); } + /** * Appends a message for the given annotation and returns this instance of {@link * ValidationException} diff --git a/javatests/dagger/internal/codegen/DaggerSuperficialValidationTest.java b/javatests/dagger/internal/codegen/DaggerSuperficialValidationTest.java index 6266dfc5104..11e8bfbe77d 100644 --- a/javatests/dagger/internal/codegen/DaggerSuperficialValidationTest.java +++ b/javatests/dagger/internal/codegen/DaggerSuperficialValidationTest.java @@ -653,11 +653,6 @@ public void invalidSupertypeInClass() { "class Foo : MissingType"), (processingEnv, superficialValidation) -> { XTypeElement foo = processingEnv.findTypeElement("test.Foo"); - if (processingEnv.getBackend() == XProcessingEnv.Backend.JAVAC) { - // TODO: b/444278301 - Javac's model is missing the error types. - superficialValidation.validateTypeHierarchyOf("type", foo, foo.getType()); - return; - } ValidationException exception = assertThrows( ValidationException.KnownErrorType.class, @@ -686,11 +681,6 @@ public void invalidSupertypeInInterface() { "interface Foo : MissingType"), (processingEnv, superficialValidation) -> { XTypeElement foo = processingEnv.findTypeElement("test.Foo"); - if (processingEnv.getBackend() == XProcessingEnv.Backend.JAVAC) { - // TODO: b/444278301 - Javac's model is missing the error types. - superficialValidation.validateTypeHierarchyOf("type", foo, foo.getType()); - return; - } ValidationException exception = assertThrows( ValidationException.KnownErrorType.class, @@ -723,12 +713,6 @@ public void invalidSuperInterfaceInTypeHierarchy() { "class Baz : MissingType {}"), (processingEnv, superficialValidation) -> { XTypeElement foo = processingEnv.findTypeElement("test.Foo"); - if (isKAPT(processingEnv)) { - // https://youtrack.jetbrains.com/issue/KT-34193/Kapt-CorrectErrorTypes-doesnt-work-for-generics - // There's no way to work around this bug in KAPT so validation doesn't catch this case. - superficialValidation.validateTypeHierarchyOf("type", foo, foo.getType()); - return; - } ValidationException exception = assertThrows( ValidationException.KnownErrorType.class, @@ -772,13 +756,6 @@ public void invalidSuperInterfaceInArrayTypeHierarchy() { (processingEnv, superficialValidation) -> { XTypeElement outerElement = processingEnv.findTypeElement("test.Outer"); XMethodElement getFooMethod = outerElement.getDeclaredMethods().get(0); - if (isKAPT(processingEnv)) { - // https://youtrack.jetbrains.com/issue/KT-34193/Kapt-CorrectErrorTypes-doesnt-work-for-generics - // There's no way to work around this bug in KAPT so validation doesn't catch this case. - superficialValidation.validateTypeHierarchyOf( - "return type", getFooMethod, getFooMethod.getReturnType()); - return; - } ValidationException exception = assertThrows( ValidationException.KnownErrorType.class,