-
Notifications
You must be signed in to change notification settings - Fork 376
KeyFor Checker: Convert annotations with parse errors to top for method invocations. #7052
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
523ed7e
Converted parse errors to top for method invocations.
smillst 661ff3f
Fix some errors.
smillst e4d676d
Corrections.
smillst fa9a5db
Only do this on KeyFor.
smillst 0adda6d
Add comments.
smillst c705d7c
Revert some changes.
smillst e85b693
More more code to KeyForDependentTypesHelper.
smillst c9f7d7a
Fix errors.
smillst 241afcb
Merge branch 'master' into unknownJavaExpressions
smillst 391ce01
Add not about unsoundness.
smillst 43abe21
Merge branch 'unknownJavaExpressions' of github.com:smillst/checker-f…
smillst 3c2af29
Merge ../checker-framework-branch-master into unknownJavaExpressions
mernst 0bf38a7
Improve grammar
mernst d97a299
Merge branch 'unknownJavaExpressions' of github.com:smillst/checker-f…
mernst b9bce37
Move comment
mernst 7741a6d
Address code review.
smillst f711669
Merge ../checker-framework-branch-master into unknownJavaExpressions
mernst e7d2b81
Rename variable
mernst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package open.falsepos; | ||
|
||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
class Issue6520 { | ||
|
||
private record Data(String value) {} | ||
|
||
Issue6520(Stream<Data> data) { | ||
data.collect(Collectors.groupingBy(Data::value)).entrySet().stream() | ||
.filter(entry -> entry.getValue().size() > 1); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package open.falsepos; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import org.checkerframework.checker.nullness.qual.KeyFor; | ||
|
||
public record Issue6750(String type) { | ||
|
||
void needKeyFor(@KeyFor("#2") String s, Map<String, String> map) { | ||
throw new RuntimeException(); | ||
} | ||
|
||
@KeyFor("#1") String returnKeyFor(Map<String, String> map) { | ||
throw new RuntimeException(); | ||
} | ||
|
||
Map<String, String> getMap(Function<String, String> s) { | ||
throw new RuntimeException(); | ||
} | ||
|
||
void use() { | ||
// :: error: (argument) | ||
needKeyFor("", getMap(String::toString)); | ||
// :: error: (expression.unparsable) :: error: (assignment) | ||
@KeyFor("getMap(String::toString)") String s = returnKeyFor(new HashMap<>(getMap(String::toString))); | ||
} | ||
|
||
void method(List<Issue6750> externals) { | ||
externals.stream().collect(Collectors.groupingBy(Issue6750::type)).entrySet().stream() | ||
.forEach( | ||
values -> { | ||
// :: error: (assignment) | ||
@KeyFor({}) String b = values.getKey(); | ||
}); | ||
} | ||
|
||
void test(List<Issue6750> externals) { | ||
externals.stream().collect(Collectors.groupingBy(Issue6750::type)).entrySet().stream() | ||
.forEach( | ||
values -> { | ||
throw new RuntimeException(""); | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; | ||
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType; | ||
import org.checkerframework.framework.type.AnnotatedTypeParameterBounds; | ||
import org.checkerframework.framework.type.QualifierHierarchy; | ||
import org.checkerframework.framework.type.treeannotator.TreeAnnotator; | ||
import org.checkerframework.framework.type.visitor.AnnotatedTypeScanner; | ||
import org.checkerframework.framework.type.visitor.DoubleAnnotatedTypeScanner; | ||
|
@@ -119,6 +120,12 @@ public class DependentTypesHelper { | |
/** This scans an annotated type and returns a list of {@link DependentTypesError}. */ | ||
private final ExpressionErrorCollector expressionErrorCollector = new ExpressionErrorCollector(); | ||
|
||
/** | ||
* This scans the annotated type and replaces any dependent type annotation that has a parse error | ||
* with the top annotation in the hierarchy. | ||
*/ | ||
protected final ErrorAnnoReplacer errorAnnoReplacer; | ||
|
||
/** | ||
* A scanner that applies a function to each {@link AnnotationMirror} and replaces it in the given | ||
* {@code AnnotatedTypeMirror}. (This side-effects the {@code AnnotatedTypeMirror}.) | ||
|
@@ -129,7 +136,7 @@ public class DependentTypesHelper { | |
* Copies annotations that might have been viewpoint adapted from the visited type (the first | ||
* formal parameter of {@code ViewpointAdaptedCopier#visit}) to the second formal parameter. | ||
*/ | ||
private final ViewpointAdaptedCopier viewpointAdaptedCopier = new ViewpointAdaptedCopier(); | ||
protected final ViewpointAdaptedCopier viewpointAdaptedCopier = new ViewpointAdaptedCopier(); | ||
|
||
/** The type mirror for java.lang.Object. */ | ||
protected final TypeMirror objectTM; | ||
|
@@ -141,7 +148,7 @@ public class DependentTypesHelper { | |
*/ | ||
public DependentTypesHelper(AnnotatedTypeFactory factory) { | ||
this.factory = factory; | ||
|
||
this.errorAnnoReplacer = new ErrorAnnoReplacer(factory.getQualifierHierarchy()); | ||
this.annoToElements = new HashMap<>(); | ||
for (Class<? extends Annotation> expressionAnno : factory.getSupportedTypeQualifiers()) { | ||
List<ExecutableElement> elementList = | ||
|
@@ -1198,6 +1205,35 @@ private ExpressionErrorCollector() { | |
} | ||
} | ||
|
||
/** | ||
* Replaces a dependent type annotation with a parser error with the top qualifier in the | ||
* hierarchy. | ||
*/ | ||
protected class ErrorAnnoReplacer extends SimpleAnnotatedTypeScanner<Void, Void> { | ||
|
||
/** | ||
* Create ErrorAnnoReplacer | ||
* | ||
* @param qh qualifier hierarchy | ||
*/ | ||
private ErrorAnnoReplacer(QualifierHierarchy qh) { | ||
super( | ||
(AnnotatedTypeMirror type, Void aVoid) -> { | ||
AnnotationMirrorSet newAnnos = new AnnotationMirrorSet(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does a lot of allocation. I suggest first determining whether creating a new AnnotationMirrorSet is necessary, and then doing the work only if so. This requires two passes over the set, but I think it's worth it given how often this will be called. |
||
for (AnnotationMirror am : type.getPrimaryAnnotations()) { | ||
if (isExpressionAnno(am) && !errorElements(am).isEmpty()) { | ||
newAnnos.add(qh.getTopAnnotation(am)); | ||
} else { | ||
newAnnos.add(am); | ||
} | ||
} | ||
type.clearPrimaryAnnotations(); | ||
type.addAnnotations(newAnnos); | ||
return null; | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Appends list2 to list1 in a new list. If either list is empty, returns the other. Thus, the | ||
* result may be aliased to one of the arguments and the client should only read, not write into, | ||
|
@@ -1226,7 +1262,11 @@ private static List<DependentTypesError> concatenate( | |
* visited type to the second formal parameter except for annotations on types that have been | ||
* substituted. | ||
*/ | ||
private class ViewpointAdaptedCopier extends DoubleAnnotatedTypeScanner<Void> { | ||
protected class ViewpointAdaptedCopier extends DoubleAnnotatedTypeScanner<Void> { | ||
|
||
/** Create a ViewpointAdaptedCopier. */ | ||
private ViewpointAdaptedCopier() {} | ||
|
||
@Override | ||
protected Void scan(AnnotatedTypeMirror from, AnnotatedTypeMirror to) { | ||
if (from == null || to == null) { | ||
|
@@ -1241,6 +1281,7 @@ protected Void scan(AnnotatedTypeMirror from, AnnotatedTypeMirror to) { | |
} | ||
} | ||
to.replaceAnnotations(replacements); | ||
|
||
if (from.getKind() != to.getKind() | ||
|| (from.getKind() == TypeKind.TYPEVAR | ||
&& TypesUtils.isCapturedTypeVariable(to.getUnderlyingType()))) { | ||
|
@@ -1277,7 +1318,7 @@ protected Void defaultAction(AnnotatedTypeMirror type1, AnnotatedTypeMirror type | |
* @param atm a type | ||
* @return true if {@code atm} has any dependent type annotations | ||
*/ | ||
private boolean hasDependentType(AnnotatedTypeMirror atm) { | ||
protected boolean hasDependentType(AnnotatedTypeMirror atm) { | ||
if (atm == null) { | ||
return false; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.