Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 434602 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java (+27 lines)
Lines 7436-7439 public void testBug434374c() { Link Here
7436
		getCompilerOptions(),
7436
		getCompilerOptions(),
7437
		"");
7437
		"");
7438
}
7438
}
7439
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=434602
7440
// Possible error with inferred null annotations leading to contradictory null annotations
7441
public void testBug434602() {
7442
	if (this.complianceLevel < ClassFileConstants.JDK1_8)
7443
		return;
7444
	runConformTestWithLibs(
7445
			new String[] {
7446
				"X.java",
7447
				"import org.eclipse.jdt.annotation.NonNullByDefault;\n" +
7448
				"import org.eclipse.jdt.annotation.Nullable;\n" +
7449
				"\n" +
7450
				"class Y {}\n" +
7451
				"@NonNullByDefault\n" +
7452
				"class X {\n" +
7453
				"	void foo() {\n" +
7454
				"		X x = new X();\n" +
7455
				"		x.bar(); // Error: Contradictory null annotations before the fix\n" +
7456
				"	}\n" +
7457
				"\n" +
7458
				"	public <T extends Y> @Nullable T bar() {\n" +
7459
				"		return null;\n" +
7460
				"	}\n" +
7461
				"}\n"
7462
			},
7463
			getCompilerOptions(),
7464
			"");
7465
}
7439
}
7466
}
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedGenericMethodBinding.java (-2 / +30 lines)
Lines 484-489 public class ParameterizedGenericMethodBinding extends ParameterizedMethodBindin Link Here
484
	    this.defaultNullness = originalMethod.defaultNullness;
484
	    this.defaultNullness = originalMethod.defaultNullness;
485
	}
485
	}
486
486
487
	/**
488
	 * Override NonNull with Nullable for return type.
489
	 */
490
	private TypeBinding substitutedConflictingNullAnnotationReturnType(MethodBinding method, LookupEnvironment env) {
491
		// error case where exception type variable would have been substituted by a non-reference type (207573)
492
		TypeBinding substitutedReturnType = Scope.substitute(this, method.returnType);
493
		if (!env.globalOptions.isAnnotationBasedNullAnalysisEnabled)
494
			return substitutedReturnType;
495
496
		// No conflicting null annotations (NonNull and Nullable).
497
		if ((substitutedReturnType.tagBits & TagBits.AnnotationNullMASK) != TagBits.AnnotationNullMASK)
498
			return substitutedReturnType;
499
500
		// We should not suppress errors where user annotated the return type with both NonNull
501
		// and Nullable explicitly.
502
		if ((method.returnType.tagBits & TagBits.AnnotationNullable) == TagBits.AnnotationNullable
503
				&& (method.returnType.tagBits & TagBits.AnnotationNonNull) != TagBits.AnnotationNonNull) {
504
			AnnotationBinding[] annotationBinding = substitutedReturnType.typeAnnotations;
505
			AnnotationBinding[] newbies = new AnnotationBinding[annotationBinding.length-1];
506
			for (int i = 0, length = annotationBinding.length, j = 0; i < length; i++) {
507
				if (annotationBinding[i].type.id != TypeIds.T_ConfiguredAnnotationNonNull)
508
					newbies[j++] = annotationBinding[i];
509
			}
510
			substitutedReturnType.typeAnnotations = newbies;
511
			substitutedReturnType.tagBits &= ~TagBits.AnnotationNonNull;
512
		}
513
		return substitutedReturnType;
514
	}
515
487
    /**
516
    /**
488
     * Create method of parameterized type, substituting original parameters with type arguments.
517
     * Create method of parameterized type, substituting original parameters with type arguments.
489
     */
518
     */
Lines 498-505 public class ParameterizedGenericMethodBinding extends ParameterizedMethodBindin Link Here
498
	    this.tagBits = originalMethod.tagBits;
527
	    this.tagBits = originalMethod.tagBits;
499
	    this.originalMethod = originalMethod;
528
	    this.originalMethod = originalMethod;
500
	    this.parameters = Scope.substitute(this, originalMethod.parameters);
529
	    this.parameters = Scope.substitute(this, originalMethod.parameters);
501
	    // error case where exception type variable would have been substituted by a non-reference type (207573)
530
	    this.returnType = substitutedConflictingNullAnnotationReturnType(originalMethod, environment);
502
	    this.returnType = Scope.substitute(this, originalMethod.returnType);
503
	    this.thrownExceptions = Scope.substitute(this, originalMethod.thrownExceptions);
531
	    this.thrownExceptions = Scope.substitute(this, originalMethod.thrownExceptions);
504
	    if (this.thrownExceptions == null) this.thrownExceptions = Binding.NO_EXCEPTIONS;
532
	    if (this.thrownExceptions == null) this.thrownExceptions = Binding.NO_EXCEPTIONS;
505
		checkMissingType: {
533
		checkMissingType: {

Return to bug 434602