Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warning: [unchecked] unchecked method invocation: method singletonMap in class Collections is applied to given types #1039

Closed
jaredsburrows opened this issue Jan 21, 2018 · 10 comments · Fixed by #1523

Comments

@jaredsburrows
Copy link

Can suppress unchecked in the generated code to prevent warnings during compilation?

Issue:

warning: [unchecked] unchecked method invocation: method singletonMap in class Collections is applied to given types

Error:

$ gradlew assembleDEbug

> Task :compileDebugJavaWithJavac FAILED
/Users/<>/repo/android-gif-example/build/generated/source/kapt/debug/burrows/apps/example/gif/presentation/di/component/DaggerAppComponent.java:60: warning: [unchecked] unchecked method invocation: method singletonMap in class Collections is applied to given types
            singletonMap(MainActivity.class, (Provider) mainActivitySubcomponentBuilderProvider);
                        ^
  required: K,V
  found: Class<MainActivity>,Provider
  where K,V are type-variables:
    K extends Object declared in method <K,V>singletonMap(K,V)
    V extends Object declared in method <K,V>singletonMap(K,V)
/Users/<>/repo/android-gif-example/build/generated/source/kapt/debug/burrows/apps/example/gif/presentation/di/component/DaggerAppComponent.java:60: warning: [unchecked] unchecked conversion
            singletonMap(MainActivity.class, (Provider) mainActivitySubcomponentBuilderProvider);
                                             ^
  required: V
  found:    Provider
  where V,K are type-variables:
    V extends Object declared in method <K,V>singletonMap(K,V)
    K extends Object declared in method <K,V>singletonMap(K,V)
/Users/<>/repo/android-gif-example/build/generated/source/kapt/debug/burrows/apps/example/gif/presentation/di/component/DaggerAppComponent.java:60: warning: [unchecked] unchecked conversion
            singletonMap(MainActivity.class, (Provider) mainActivitySubcomponentBuilderProvider);
                        ^
  required: Map<Class<? extends Activity>,Provider<Factory<? extends Activity>>>
  found:    Map
error: warnings found and -Werror specified
1 error
3 warnings


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
17 actionable tasks: 16 executed, 1 up-to-date

Temp fix:

"-Xlint:-unchecked"

@ronshapiro
Copy link

ronshapiro commented Jan 21, 2018

Shouldn't we be specifying type parameters there (

private CodeBlock maybeTypeParameters(ClassName requestingClass) {
TypeMirror bindingKeyType = binding.key().type();
MapType mapType = MapType.from(binding.key());
return isTypeAccessibleFrom(bindingKeyType, requestingClass.packageName())
? CodeBlock.of("<$T, $T>", mapType.keyType(), mapType.valueType())
: CodeBlock.of("");
}
)?

Can you provide a failing example?

@jaredsburrows
Copy link
Author

@ronshapiro A failing example? The code above fails with -Werror or will simply produce the following warnings and not fail the build.

The specific line is: singletonMap(MainActivity.class, (Provider) mainActivitySubcomponentBuilderProvider);.

@ronshapiro
Copy link

I meant a small repo that exhibits the issue (that we can test+verify a fix on)

@jaredsburrows
Copy link
Author

@ronshapiro Oh sorry, I usually add that in the description. Here are the repro steps:

  1. git clone https://github.com/jaredsburrows/android-gif-example
  2. cd android-gif-example
  3. Comment out or remove add("-Xlint:-unchecked") from https://github.com/jaredsburrows/android-gif-example/blob/master/gradle/compile.gradle.kts#L16
  4. graldew assembleDebug

@jaredsburrows
Copy link
Author

Any update on this?

@ronshapiro
Copy link

I see what's happening - there's a @Binds method that is being requested as a provider, which causes Dagger to cast to a rawtype.

Unfortunately, I don't know of a good way to fix this without suppressing warnings over far too much code.

@ronshapiro
Copy link

Actually, we may be able to check in MapBindingExpression if the type is the erasure but could be the accessible type. This should also probably happen in other places where BindingExpressions are composed too.

@gildor
Copy link

gildor commented Apr 19, 2018

@ronshapiro Any updates? I suppose that would be better to suppress warning if it's hard/impossible to fix, after all this is generated code. This cause problem with real problems detection using -Werror option.
Also in my case I see additional warning:

unchecked call to put(K,V) as a member of the raw type MapBuilder

for the generated code of method getMapOfClassOfAndProviderOfFactoryOf2 that puts my dagger-android providers for activity to MapBuilder

@ronshapiro
Copy link

I'm leaning towards an approach like this one: #1248 (comment)

@gildor
Copy link

gildor commented Sep 5, 2018

It's little bit unfortunate that it cannot be fixed on side of dagger, but approach form your comment still works and allows use -Werror

@ronshapiro ronshapiro mentioned this issue Jun 6, 2019
ronshapiro added a commit that referenced this issue Jun 6, 2019
An internal change recently enable these warnings (which seem to have been turned off by default). This is causing spam for Dagger builds because we haven't traditionally been aware of the issues where this arises. This is a temporary stopgap measure to disable those warnings until we build infrastructure to determine how to locally-suppress this code.

Also fix two of these warnings in our own test code.

Fixes #1039
Fixes #1172

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=251730395
ronshapiro added a commit that referenced this issue Jun 6, 2019
An internal change recently enable these warnings (which seem to have been turned off by default). This is causing spam for Dagger builds because we haven't traditionally been aware of the issues where this arises. This is a temporary stopgap measure to disable those warnings until we build infrastructure to determine how to locally-suppress this code.

Also fix two of these warnings in our own test code.

Fixes #1039
Fixes #1172

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=251730395
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants