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

Add type as part of target location #972

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
public enum TypeUseLocation {

/**
* Apply default annotations to unannotated top-level types of class, interfaces, enums and
* record *
*/
TYPE,

/** Apply default annotations to unannotated top-level types of fields. */
FIELD,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ void checkRep(String aliasName) {
* Mapping from an Element to its annotated type; before defaults are applied, just what the
* programmer wrote.
*/
private final Map<Element, AnnotatedTypeMirror> elementCache;
public final Map<Element, AnnotatedTypeMirror> elementCache;

/** Mapping from an Element to the source Tree of the declaration. */
private final Map<Element, Tree> elementToTreeCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,21 @@ public Void scan(@FindDistinct AnnotatedTypeMirror t, AnnotationMirror qual) {
// Some defaults only apply to the top level type.
boolean isTopLevelType = t == outer.type;
switch (outer.location) {
case TYPE:
if (outer.scope != null && outer.scope.getKind().isClass() && isTopLevelType) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, another question is should we apply the class level default only for toplevel? Or nested classes as well?

AnnotationMirror annotation =
outer.qualHierarchy.findAnnotationInHierarchy(
atypeFactory.elementCache.get(outer.scope).getAnnotations(),
qual);
if (annotation == null
|| outer.qualHierarchy.isSubtypeQualifiersOnly(qual, annotation)) {
outer.addAnnotation(t, qual);
atypeFactory.elementCache.put(outer.scope, t);
} else {
// should report error;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the annotation supplied is incompatible with the one in DefaultQualifier, should we report error or we should keep the explicit qualifier? That is to say, in the following code, should we keep @B or should we report error?

@DefaultQualifier(
        value = A.class,
        locations = {TypeUseLocation.TYPE})
@B
public class DefaultQualifierTest {
    @B DefaultQualifierTest() {}
}

}
}
break;
case FIELD:
if (outer.scope != null
&& outer.scope.getKind() == ElementKind.FIELD
Expand Down
12 changes: 12 additions & 0 deletions framework/tests/viewpointtest/DefaultQualifierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import org.checkerframework.framework.qual.DefaultQualifier;
import org.checkerframework.framework.qual.TypeUseLocation;

import viewpointtest.quals.*;

@DefaultQualifier(
value = A.class,
locations = {TypeUseLocation.TYPE})
public class DefaultQualifierTest {
// :: error: (type.invalid.annotations.on.use)
@B DefaultQualifierTest() {}
}
Loading