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 method to deal with matrix and fix comment #34

Merged
merged 12 commits into from Jan 9, 2023

Conversation

LoiNguyenCS
Copy link
Collaborator

Professor @kelloggm, I have updated the removing curly braces method that we discussed, I have also changed the way to ignore comments, instead of finding the index of "\ ", I check if it's in a string literal. However, I am receiving some warnings from Checker Framework. Please help me take a look. Thank you.

 C:\Users\nguye\Desktop\wpi-paper-main\experiments\inferred-annos-counter\app\src\main\java\org\checkerframework\wholeprograminference\inferredannoscounter\InferredAnnosCounter.java:336: error: [argument] incompatible argument for parameter Index of checkInString.
      if (checkInString(indexOfClose, result)){
                        ^
  found   : @LTEqLengthOf("result") int
  required: @LTLengthOf("result") int 
  
  
C:\Users\nguye\Desktop\wpi-paper-main\experiments\inferred-annos-counter\app\src\main\java\org\checkerframework\wholeprograminference\inferredannoscounter\InferredAnnosCounter.java:369: error: [argument] incompatible argument for parameter Index of checkInString.
      } else if (checkInString(indexDash, line)) {
                               ^
  found   : @LTEqLengthOf("line") int
  required: @LTLengthOf("line") int

Copy link
Owner

@kelloggm kelloggm left a comment

Choose a reason for hiding this comment

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

Notes/suggestions on what to do about Index Checker warnings.

* This method is used to format annotation that contains arguments, such
* as @EnsuresNonNull("tz1"). This method has two parts. First, we check if the annotation
* contains a matrix and format the matrix if we found one. Second, we'll remove all curly braces
* in that annotation.
Copy link
Owner

Choose a reason for hiding this comment

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

This Javadoc comment should explain why we need to do this - i.e., what the problem was with how we were doing it before.

@LoiNguyenCS
Copy link
Collaborator Author

LoiNguyenCS commented Dec 25, 2022

Professor, I have modified the codes based on your second approach. I have updated the checkInString method to throw an exception if the index given is not valid. I think this approach will make the whole program more logically connected than suppressing a warning. Please take a look. Thank you.

Copy link
Owner

@kelloggm kelloggm left a comment

Choose a reason for hiding this comment

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

Adding dynamic index checking to these methods is a clever way to subvert the Index Checker, but it doesn't actually make our code that much safer: we will just get a RuntimeException instead of an IndexOutOfBoundsException if there is an invalid index. That would still cause the program to crash, which is undesirable.

It is more useful to try to avoid passing a bad index into these routines rather than crashing if such an index is passed in.

private static boolean checkInString(@IndexFor("#2") int Index, String line) {
private static boolean checkInString(int index, String line) {
if (index >= line.length() || index < 0) {
throw new RuntimeException("The index provided for checkInString is not valid");
Copy link
Owner

Choose a reason for hiding this comment

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

While adding this check here isn't a bad thing to do, the specification of checkInString should still be that the index parameter ought to be @IndexFor("#2"): the goal of the checker is to prevent crashes, and the change you've made here just turns an IndexOutOfBoundsException into a RuntimeException.

We should retain the @IndexFor annotation on line 275 above: we still need to deal with the possibility that the index used at some call sites might not be valid (ie, the checker warnings).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Got it. So then I should stick to first approach, which is to suppress the warning and provide a link to the bug report. Is that correct, Professor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Professor, I tried to build a simple test case for the bug report. But then the test case appeared to work, here is the test case:


import org.checkerframework.checker.index.qual.IndexFor;

public class InferredAnnosCounter
{
  /**
   * This method checks if a particular index in a line belongs to a string literal
   *
   * @param line a line
   * @param index index of line
   * @return false if index belongs to a string literal, true otherwise
   */
  private static boolean checkInString(@IndexFor("#2") int index, String line) {
    int before = 0;
    int after = 0;
    for (int i = 0; i < index; i++) {
      if (line.charAt(i) == '\"') {
        before++;
      }
    }
    for (int i = index + 1; i < line.length(); i++) {
      if (line.charAt(i) == '\"') {
        after++;
      }
    }
    if (before % 2 == 0 && after % 2 == 0) {
      return true;
    }
    return false;
  }

  public static void main(String[] args) {
    String x = "Hello@World, this is our new program";
    int indexToCheck = x.indexOf("d, ");
    if (indexToCheck != -1 && checkInString(indexToCheck, x))
      System.out.println("TRUE");
  }
}

So I guess maybe there're some problems with my codes that I'm not aware of, I will try to investigate more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

UPDATE: Professor, I have found a test case where we would receive the warning. Here is the test case.

import org.checkerframework.checker.index.qual.IndexFor;
public class SimpleTestCase
{
  private static boolean atTheBeginning(@IndexFor("#2") int index, String line) {
    return (index==0);
  }

  private static boolean hasDoubleQuestionMarkAtTheBeginning (String line) {
    int i = line.indexOf("??");
    if (i!=-1) {
      return (atTheBeginning(i, line));
    }
    return false;
  }

  public static void main(String[] args) {
    String x = "Hello?World, this is our new program";
    if (hasDoubleQuestionMarkAtTheBeginning(x))
      System.out.println("TRUE");
  }
}

and the warning is

return (atTheBeginning(i, line));
                             ^
  found   : @LTEqLengthOf("line") int
  required: @LTLengthOf("line") int

I have made a bug report, here is the link: typetools/checker-framework#5471

Copy link
Owner

Choose a reason for hiding this comment

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

FYI I've fixed the bug, so when we make the next Checker Framework release we'll be able to remove these warning suppressions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you Professor. In that case, should I close the issue that I have made?

@LoiNguyenCS
Copy link
Collaborator Author

Professor, I have added some SuppressWarnings statement, but I'm still not able to suppress the warning on line 346. Please help me with this case. Thank you.

Here is the warning that I received:

C:\Users\nguye\Desktop\wpi-paper-main\experiments\inferred-annos-counter\app\src\main\java\org\checkerframework\wholeprograminference\inferredannoscounter\InferredAnnosCounter.java:346: error: [assignment] incompatible types in assignment.
        indexOfClose = result.indexOf("},");
                                     ^
  found   : @LTEqLengthOf("result") int
  required: @LTLengthOf("result") int

@kelloggm
Copy link
Owner

Professor, I have added some SuppressWarnings statement, but I'm still not able to suppress the warning on line 346. Please help me with this case. Thank you.

Here is the warning that I received:

C:\Users\nguye\Desktop\wpi-paper-main\experiments\inferred-annos-counter\app\src\main\java\org\checkerframework\wholeprograminference\inferredannoscounter\InferredAnnosCounter.java:346: error: [assignment] incompatible types in assignment.
        indexOfClose = result.indexOf("},");
                                     ^
  found   : @LTEqLengthOf("result") int
  required: @LTLengthOf("result") int

This is the same issue (i.e., typetools/checker-framework#5471), but there's no declaration to attach the warning suppression to, because this re-assigns a variable. In this case, the right thing to do is to create a temporary variable with the correct type, and add the warning suppression on its declaration. So, change line 346 to the following:

@SuppressWarnings("index:assignment") // https://github.com/typetools/checker-framework/issues/5471
@LTLengthOf("result") int temp = result.indexOf("},");
indexOfClose = temp;

@kelloggm
Copy link
Owner

kelloggm commented Dec 28, 2022 via email

@kelloggm kelloggm merged commit 4a05acc into Update-to-format-anno Jan 9, 2023
@kelloggm kelloggm deleted the matrix-and-comments branch January 9, 2023 15:48
kelloggm added a commit that referenced this pull request Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants