From a0c91fcc0c3d0f4c0485fdf2cbf91ea0f5dd5f7a Mon Sep 17 00:00:00 2001 From: "Wuyue (Tony) Sun" Date: Sun, 19 Mar 2023 23:14:31 -0400 Subject: [PATCH 1/3] Fix position of marker for dereference of nullable errors --- .../checkerframework/checker/nullness/NullnessVisitor.java | 6 ++++-- docs/manual/contributors.tex | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java b/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java index d8ecfff6ee9..4189cb5b2e9 100644 --- a/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java +++ b/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java @@ -271,7 +271,8 @@ public Void visitMemberSelect(MemberSelectTree tree, Void p) { || tree.getExpression().getKind() == Tree.Kind.PARAMETERIZED_TYPE // case 8. static member access || ElementUtils.isStatic(e))) { - checkForNullability(tree.getExpression(), DEREFERENCE_OF_NULLABLE); + AnnotatedTypeMirror type = atypeFactory.getAnnotatedType(tree.getExpression()); + checkForNullability(type, tree.getExpression(), DEREFERENCE_OF_NULLABLE); } return super.visitMemberSelect(tree, p); @@ -743,7 +744,8 @@ public Void visitForLoop(ForLoopTree tree, Void p) { public Void visitNewClass(NewClassTree tree, Void p) { ExpressionTree enclosingExpr = tree.getEnclosingExpression(); if (enclosingExpr != null) { - checkForNullability(enclosingExpr, DEREFERENCE_OF_NULLABLE); + AnnotatedTypeMirror type = atypeFactory.getAnnotatedType(enclosingExpr); + checkForNullability(type, enclosingExpr, DEREFERENCE_OF_NULLABLE); } AnnotatedDeclaredType type = atypeFactory.getAnnotatedType(tree); ExpressionTree identifier = tree.getIdentifier(); diff --git a/docs/manual/contributors.tex b/docs/manual/contributors.tex index b37085c8f73..b2553a91e49 100644 --- a/docs/manual/contributors.tex +++ b/docs/manual/contributors.tex @@ -136,4 +136,5 @@ Vlastimil Dort, Weitian Xing, Werner Dietl, +Wuyue (Tony) Sun, Zhiping Cai. From 2da1c26111d660a3c68b00890180bb345523b69a Mon Sep 17 00:00:00 2001 From: "Wuyue (Tony) Sun" Date: Fri, 7 Apr 2023 12:05:40 -0400 Subject: [PATCH 2/3] Correct position of marker for nullness_dereference --- .../checker/nullness/NullnessVisitor.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java b/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java index 4189cb5b2e9..f01497ecad7 100644 --- a/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java +++ b/checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java @@ -272,7 +272,7 @@ public Void visitMemberSelect(MemberSelectTree tree, Void p) { // case 8. static member access || ElementUtils.isStatic(e))) { AnnotatedTypeMirror type = atypeFactory.getAnnotatedType(tree.getExpression()); - checkForNullability(type, tree.getExpression(), DEREFERENCE_OF_NULLABLE); + checkForNullability(type, tree, DEREFERENCE_OF_NULLABLE, tree.getExpression()); } return super.visitMemberSelect(tree, p); @@ -650,8 +650,22 @@ private boolean checkForNullability(ExpressionTree tree, @CompilerMessageKey Str */ private boolean checkForNullability( AnnotatedTypeMirror type, Tree tree, @CompilerMessageKey String errMsg) { + return checkForNullability(type, tree, errMsg, tree); + } + + /** + * Issues the error message if an expression with this type may be null. + * + * @param type annotated type + * @param tree the tree where the error is to reported + * @param errMsg the error message (must be {@link CompilerMessageKey}) + * @param errTree the tree to be used in the error message + * @return whether or not the check succeeded + */ + private boolean checkForNullability( + AnnotatedTypeMirror type, Tree tree, @CompilerMessageKey String errMsg, Tree errTree) { if (!type.hasEffectiveAnnotation(NONNULL)) { - checker.reportError(tree, errMsg, tree); + checker.reportError(tree, errMsg, errTree); return false; } return true; @@ -745,7 +759,7 @@ public Void visitNewClass(NewClassTree tree, Void p) { ExpressionTree enclosingExpr = tree.getEnclosingExpression(); if (enclosingExpr != null) { AnnotatedTypeMirror type = atypeFactory.getAnnotatedType(enclosingExpr); - checkForNullability(type, enclosingExpr, DEREFERENCE_OF_NULLABLE); + checkForNullability(type, tree, DEREFERENCE_OF_NULLABLE, enclosingExpr); } AnnotatedDeclaredType type = atypeFactory.getAnnotatedType(tree); ExpressionTree identifier = tree.getIdentifier(); From 537c38e47202534d9d4ada2e24be7bb0f6a8daee Mon Sep 17 00:00:00 2001 From: "Wuyue (Tony) Sun" Date: Fri, 7 Apr 2023 20:29:54 -0400 Subject: [PATCH 3/3] Change affected testcase --- checker/jtreg/nullness/Issue347-con.out | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checker/jtreg/nullness/Issue347-con.out b/checker/jtreg/nullness/Issue347-con.out index 644fabd8dae..ba77aa40631 100644 --- a/checker/jtreg/nullness/Issue347-con.out +++ b/checker/jtreg/nullness/Issue347-con.out @@ -1,2 +1,2 @@ -Issue347.java:32:9: compiler.err.proc.messager: [dereference.of.nullable] dereference of possibly-null reference nble +Issue347.java:32:13: compiler.err.proc.messager: [dereference.of.nullable] dereference of possibly-null reference nble 1 error