typetools / typetools/checker-framework

Misleading cast.unsafe error

Open
#2,731 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Consider the following code:

class cast_unsafe{
	static final float b = 0.75f;
       void casting(){
    	int c = (int)(b);
    }
}

Run it by the following command: javac -processor index cast_unsafe.java

Output:

cast_unsafe.java:4: warning: [cast.unsafe] "@DoubleVal(0.75) float" may not be casted to the type "@IntVal(0) int"
    	int c = (int)(b);
    	        ^
cast_unsafe.java:4: warning: [cast.unsafe] "@LowerBoundUnknown float" may not be casted to the type "@NonNegative int"
    	int c = (int)(b);
    	        ^
2 warnings

However, the cast is safe and c evaluates to 0.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning with the supplied cast_unsafe.java example and javac -processor index cast_unsafe.java, then trace the checker entry point that reports the cast.unsafe diagnostic. Done means the safe float-to-int cast no longer produces misleading warnings and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.