@@ -129,9 +129,9 @@ private static void reportInvalidAssignmentInstantiationError(
129129 ErrorMessage .MessageTypes .ASSIGN_GENERIC_NULLABLE ,
130130 String .format (
131131 "Cannot assign from type "
132- + prettyTypeForError (rhsType )
132+ + prettyTypeForError (rhsType , state )
133133 + " to type "
134- + prettyTypeForError (lhsType )
134+ + prettyTypeForError (lhsType , state )
135135 + " due to mismatched nullability of type parameters" ));
136136 state .reportMatch (
137137 errorBuilder .createErrorDescription (
@@ -146,9 +146,9 @@ private static void reportInvalidReturnTypeError(
146146 ErrorMessage .MessageTypes .RETURN_NULLABLE_GENERIC ,
147147 String .format (
148148 "Cannot return expression of type "
149- + prettyTypeForError (returnType )
149+ + prettyTypeForError (returnType , state )
150150 + " from method with return type "
151- + prettyTypeForError (methodType )
151+ + prettyTypeForError (methodType , state )
152152 + " due to mismatched nullability of type parameters" ));
153153 state .reportMatch (
154154 errorBuilder .createErrorDescription (
@@ -163,9 +163,9 @@ private static void reportMismatchedTypeForTernaryOperator(
163163 ErrorMessage .MessageTypes .ASSIGN_GENERIC_NULLABLE ,
164164 String .format (
165165 "Conditional expression must have type "
166- + prettyTypeForError (expressionType )
166+ + prettyTypeForError (expressionType , state )
167167 + " but the sub-expression has type "
168- + prettyTypeForError (subPartType )
168+ + prettyTypeForError (subPartType , state )
169169 + ", which has mismatched nullability of type parameters" ));
170170 state .reportMatch (
171171 errorBuilder .createErrorDescription (
@@ -183,9 +183,9 @@ private static void reportInvalidParametersNullabilityError(
183183 new ErrorMessage (
184184 ErrorMessage .MessageTypes .PASS_NULLABLE_GENERIC ,
185185 "Cannot pass parameter of type "
186- + prettyTypeForError (actualParameterType )
186+ + prettyTypeForError (actualParameterType , state )
187187 + ", as formal parameter has type "
188- + prettyTypeForError (formalParameterType )
188+ + prettyTypeForError (formalParameterType , state )
189189 + ", which has mismatched type parameter nullability" );
190190 state .reportMatch (
191191 errorBuilder .createErrorDescription (
@@ -203,9 +203,9 @@ private static void reportInvalidOverridingMethodReturnTypeError(
203203 new ErrorMessage (
204204 ErrorMessage .MessageTypes .WRONG_OVERRIDE_RETURN_GENERIC ,
205205 "Method returns "
206- + prettyTypeForError (overridingMethodReturnType )
206+ + prettyTypeForError (overridingMethodReturnType , state )
207207 + ", but overridden method returns "
208- + prettyTypeForError (overriddenMethodReturnType )
208+ + prettyTypeForError (overriddenMethodReturnType , state )
209209 + ", which has mismatched type parameter nullability" );
210210 state .reportMatch (
211211 errorBuilder .createErrorDescription (
@@ -223,9 +223,9 @@ private static void reportInvalidOverridingMethodParamTypeError(
223223 new ErrorMessage (
224224 ErrorMessage .MessageTypes .WRONG_OVERRIDE_PARAM_GENERIC ,
225225 "Parameter has type "
226- + prettyTypeForError (methodParamType )
226+ + prettyTypeForError (methodParamType , state )
227227 + ", but overridden method has parameter type "
228- + prettyTypeForError (typeParameterType )
228+ + prettyTypeForError (typeParameterType , state )
229229 + ", which has mismatched type parameter nullability" );
230230 state .reportMatch (
231231 errorBuilder .createErrorDescription (
@@ -546,7 +546,10 @@ public Boolean visitClassType(Type.ClassType lhsType, Type rhsType) {
546546 return false ;
547547 }
548548 }
549- return true ;
549+ // If there is an enclosing type (for non-static inner classes), its type argument nullability
550+ // should also match. When there is no enclosing type, getEnclosingType() returns a NoType
551+ // object, which gets handled by the fallback visitType() method
552+ return lhsType .getEnclosingType ().accept (this , rhsType .getEnclosingType ());
550553 }
551554
552555 @ Override
@@ -992,57 +995,66 @@ private static Nullness getTypeNullness(Type type, Config config) {
992995 * Returns a pretty-printed representation of type suitable for error messages. The representation
993996 * uses simple names rather than fully-qualified names, and retains all type-use annotations.
994997 */
995- public static String prettyTypeForError (Type type ) {
996- return type .accept (PRETTY_TYPE_VISITOR , null );
998+ public static String prettyTypeForError (Type type , VisitorState state ) {
999+ return type .accept (new PrettyTypeVisitor ( state ) , null );
9971000 }
9981001
9991002 /** This code is a modified version of code in {@link com.google.errorprone.util.Signatures} */
1000- private static final Type .Visitor <String , Void > PRETTY_TYPE_VISITOR =
1001- new Types .DefaultTypeVisitor <String , Void >() {
1002- @ Override
1003- public String visitWildcardType (Type .WildcardType t , Void unused ) {
1004- StringBuilder sb = new StringBuilder ();
1005- sb .append (t .kind );
1006- if (t .kind != BoundKind .UNBOUND ) {
1007- sb .append (t .type .accept (this , null ));
1008- }
1009- return sb .toString ();
1010- }
1003+ private static final class PrettyTypeVisitor extends Types .DefaultTypeVisitor <String , Void > {
10111004
1012- @ Override
1013- public String visitClassType (Type .ClassType t , Void s ) {
1014- StringBuilder sb = new StringBuilder ();
1015- for (Attribute .TypeCompound compound : t .getAnnotationMirrors ()) {
1016- sb .append ('@' );
1017- sb .append (compound .type .accept (this , null ));
1018- sb .append (' ' );
1019- }
1020- sb .append (t .tsym .getSimpleName ());
1021- if (t .getTypeArguments ().nonEmpty ()) {
1022- sb .append ('<' );
1023- sb .append (
1024- t .getTypeArguments ().stream ()
1025- .map (a -> a .accept (this , null ))
1026- .collect (joining (", " )));
1027- sb .append (">" );
1028- }
1029- return sb .toString ();
1030- }
1005+ private final VisitorState state ;
10311006
1032- @ Override
1033- public String visitCapturedType (Type .CapturedType t , Void s ) {
1034- return t .wildcard .accept (this , null );
1035- }
1007+ PrettyTypeVisitor (VisitorState state ) {
1008+ this .state = state ;
1009+ }
10361010
1037- @ Override
1038- public String visitArrayType (Type .ArrayType t , Void unused ) {
1039- // TODO properly print cases like int @Nullable[]
1040- return t .elemtype .accept (this , null ) + "[]" ;
1041- }
1011+ @ Override
1012+ public String visitWildcardType (Type .WildcardType t , Void unused ) {
1013+ // NOTE: we have not tested this code yet as we do not yet support wildcard types
1014+ StringBuilder sb = new StringBuilder ();
1015+ sb .append (t .kind );
1016+ if (t .kind != BoundKind .UNBOUND ) {
1017+ sb .append (t .type .accept (this , null ));
1018+ }
1019+ return sb .toString ();
1020+ }
10421021
1043- @ Override
1044- public String visitType (Type t , Void s ) {
1045- return t .toString ();
1046- }
1047- };
1022+ @ Override
1023+ public String visitClassType (Type .ClassType t , Void s ) {
1024+ StringBuilder sb = new StringBuilder ();
1025+ Type enclosingType = t .getEnclosingType ();
1026+ if (!ASTHelpers .isSameType (enclosingType , Type .noType , state )) {
1027+ sb .append (enclosingType .accept (this , null )).append ('.' );
1028+ }
1029+ for (Attribute .TypeCompound compound : t .getAnnotationMirrors ()) {
1030+ sb .append ('@' );
1031+ sb .append (compound .type .accept (this , null ));
1032+ sb .append (' ' );
1033+ }
1034+ sb .append (t .tsym .getSimpleName ());
1035+ if (t .getTypeArguments ().nonEmpty ()) {
1036+ sb .append ('<' );
1037+ sb .append (
1038+ t .getTypeArguments ().stream ().map (a -> a .accept (this , null )).collect (joining (", " )));
1039+ sb .append (">" );
1040+ }
1041+ return sb .toString ();
1042+ }
1043+
1044+ @ Override
1045+ public String visitCapturedType (Type .CapturedType t , Void s ) {
1046+ return t .wildcard .accept (this , null );
1047+ }
1048+
1049+ @ Override
1050+ public String visitArrayType (Type .ArrayType t , Void unused ) {
1051+ // TODO properly print cases like int @Nullable[]
1052+ return t .elemtype .accept (this , null ) + "[]" ;
1053+ }
1054+
1055+ @ Override
1056+ public String visitType (Type t , Void s ) {
1057+ return t .toString ();
1058+ }
1059+ }
10481060}
0 commit comments