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

0 is a valid SubstringIndexFor if the argument is not longer than the receiver #207

Open
panacekcz opened this issue Dec 26, 2017 · 0 comments

Comments

@panacekcz
Copy link

If the argument (the substring searched for) is empty, then 0 is a valid @SubstringIndexFor.
More generally, 0 is valid if the argument is not longer than the receiver. This would require knowing about sequence length inequalities (#202).

Code, containing generalized examples:

import org.checkerframework.checker.index.qual.LTLengthOf;
import org.checkerframework.checker.index.qual.SubstringIndexFor;

public class ReturnSubstringIndexZero {
	public @SubstringIndexFor(value="#1", offset="#2.length()-1") int substringIndexOfEmpty(String a, String b) {
		if(b.length() == 0)
			return 0;
		else
			throw new RuntimeException();
	}
	
	public @SubstringIndexFor(value="#1", offset="#2.length()-1") int substringIndexOfShorter(String a, String b) {
		if(b.length() <= a.length())
			return 0;
		else
			throw new RuntimeException();
	}
	
	public @SubstringIndexFor(value="#1", offset="#2.length()-1") int substringIndexOfShorterConst(String a, String b) {
		if(b.length() + 4 <= a.length())
			return 4;
		else
			throw new RuntimeException();
	}
	
	public @LTLengthOf(value="#1", offset="#2.length-1") int ltLengthOfEmpty(int[] a, int[] b) {
		if(b.length == 0)
			return 0;
		else
			throw new RuntimeException();
	}
	
	public @LTLengthOf(value="#1", offset="#2.length-1") int ltLengthOfShorter(int[] a, int[] b) {
		if(b.length <= a.length)
			return 0;
		else
			throw new RuntimeException();
	}
}

Output:

ReturnSubstringIndexZero.java:7: error: [return.type.incompatible] incompatible types in return.
			return 0;
			       ^
  found   : @SubstringIndexUnknown int
  required: @SubstringIndexFor(value="a", offset="b.length() - 1") int
ReturnSubstringIndexZero.java:14: error: [return.type.incompatible] incompatible types in return.
			return 0;
			       ^
  found   : @SubstringIndexUnknown int
  required: @SubstringIndexFor(value="a", offset="b.length() - 1") int
ReturnSubstringIndexZero.java:21: error: [return.type.incompatible] incompatible types in return.
			return 4;
			       ^
  found   : @SubstringIndexUnknown int
  required: @SubstringIndexFor(value="a", offset="b.length() - 1") int
ReturnSubstringIndexZero.java:28: error: [return.type.incompatible] incompatible types in return.
			return 0;
			       ^
  found   : @UpperBoundUnknown int
  required: @LTLengthOf(value="a", offset="b.length - 1") int
ReturnSubstringIndexZero.java:35: error: [return.type.incompatible] incompatible types in return.
			return 0;
			       ^
  found   : @UpperBoundUnknown int
  required: @LTLengthOf(value="a", offset="b.length - 1") int
5 errors

This issue has been originally tracked as panacekcz#25.

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

No branches or pull requests

1 participant