Skip to content

Commit 4a8b754

Browse files
authored
Merge pull request scala#41 from noti0na1/dotty-explicit-nulls
Change the way of creating ClassSymbol
2 parents 946a7a9 + 4cd1114 commit 4a8b754

File tree

6 files changed

+59
-15
lines changed

6 files changed

+59
-15
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -816,19 +816,21 @@ class Definitions {
816816
// A list of annotations that are commonly used to indicate that a field/method argument or return
817817
// type is not null. These annotations are used by the nullification logic in JavaNullInterop to
818818
// improve the precision of type nullification.
819-
@tu lazy val NotNullAnnots: List[ClassSymbol] =
820-
("javax.annotation.Nonnull" ::
821-
"edu.umd.cs.findbugs.annotations.NonNull" ::
822-
"androidx.annotation.NonNull" ::
823-
"android.support.annotation.NonNull" ::
824-
"android.annotation.NonNull" ::
825-
"com.android.annotations.NonNull" ::
826-
"org.eclipse.jdt.annotation.NonNull" ::
827-
"org.checkerframework.checker.nullness.qual.NonNull" ::
828-
"org.checkerframework.checker.nullness.compatqual.NonNullDecl" ::
829-
"org.jetbrains.annotations.NotNull" ::
830-
"lombok.NonNull" ::
831-
"io.reactivex.annotations.NonNull" :: Nil).map(ctx.requiredClass(_))
819+
// We don't require that any of these annotations be present in the class path, but we want to
820+
// create Symbols for the ones that are present, so they can be checked during nullification.
821+
@tu lazy val NotNullAnnots: List[ClassSymbol] = ctx.getClassesIfDefined(
822+
"javax.annotation.Nonnull" ::
823+
"edu.umd.cs.findbugs.annotations.NonNull" ::
824+
"androidx.annotation.NonNull" ::
825+
"android.support.annotation.NonNull" ::
826+
"android.annotation.NonNull" ::
827+
"com.android.annotations.NonNull" ::
828+
"org.eclipse.jdt.annotation.NonNull" ::
829+
"org.checkerframework.checker.nullness.qual.NonNull" ::
830+
"org.checkerframework.checker.nullness.compatqual.NonNullDecl" ::
831+
"org.jetbrains.annotations.NotNull" ::
832+
"lombok.NonNull" ::
833+
"io.reactivex.annotations.NonNull" :: Nil map PreNamedString)
832834

833835
// convenient one-parameter method types
834836
def methOfAny(tp: Type): MethodType = MethodType(List(AnyType), tp)

compiler/src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@ trait Symbols { this: Context =>
383383
.requiredSymbol("class", name, generateStubs = false)(_.isClass)
384384
}
385385

386+
/** Get a List of ClassSymbols which are either defined in current compilation
387+
* run or present on classpath.
388+
*/
389+
def getClassesIfDefined(pathes: List[PreName]): List[ClassSymbol] =
390+
pathes.foldLeft(List.empty){ case (acc, path) => getClassIfDefined(path) match {
391+
case cls: ClassSymbol => cls :: acc
392+
case _ => acc
393+
}}
394+
386395
/** Get ClassSymbol if package is either defined in current compilation run
387396
* or present on classpath.
388397
* Returns NoSymbol otherwise. */
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.*;
2+
import notnull.NotNull;
3+
4+
public class J {
5+
6+
private static String getK() {
7+
return "k";
8+
}
9+
10+
@NotNull
11+
public static final String k = getK();
12+
13+
@NotNull
14+
public static String l = "l";
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package notnull;
2+
3+
import java.lang.annotation.*;
4+
5+
// A NotNull Annotation not in the list
6+
@Retention(value = RetentionPolicy.RUNTIME)
7+
public @interface NotNull {
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test that NotNull annotations not in the list are not working in Java files.
2+
3+
class S {
4+
def kk: String = J.k // error: k doesn't have a constant type and the NotNull annotation is not in the list
5+
6+
def ll: String = J.l // error: the NotNull annotation is not in the list
7+
}

tests/explicit-nulls/pos/notnull/J.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
public class J {
55

6+
private static String getK() {
7+
return "k";
8+
}
9+
610
@Nonnull
7-
// JavaParser will never assign ConstantType to fields currently.
8-
public static final String k = "k";
11+
public static final String k = getK();
912

1013
@Nonnull
1114
public static String l = "l";

0 commit comments

Comments
 (0)