@@ -166,35 +166,42 @@ private CastedExpression asInt(CastedEval cast, BasicTypeID type) {
166166 long signed = negative ? -value : value ;
167167 switch (type ) {
168168 case SBYTE :
169- return castIfFits (cast , signed >= Byte .MIN_VALUE && signed <= Byte .MAX_VALUE , (position , value ) -> new ConstantSByteExpression (position , value .byteValue ()));
169+ return castIfFits (cast , type , signed >= Byte .MIN_VALUE && signed <= Byte .MAX_VALUE , (position , value ) -> new ConstantSByteExpression (position , value .byteValue ()));
170170 case BYTE :
171- return castIfFits (cast , !negative && value <= 0xFF , (position , value ) -> new ConstantByteExpression (position , value .intValue ()));
171+ return castIfFits (cast , type , !negative && value <= 0xFF , (position , value ) -> new ConstantByteExpression (position , value .intValue ()));
172172 case SHORT :
173- return castIfFits (cast , signed >= Short .MIN_VALUE && signed <= Short .MAX_VALUE , (position , value ) -> new ConstantShortExpression (position , value .shortValue ()));
173+ return castIfFits (cast , type , signed >= Short .MIN_VALUE && signed <= Short .MAX_VALUE , (position , value ) -> new ConstantShortExpression (position , value .shortValue ()));
174174 case USHORT :
175- return castIfFits (cast , !negative && value <= 0xFFFF , (position , value ) -> new ConstantUShortExpression (position , value .intValue ()));
175+ return castIfFits (cast , type , !negative && value <= 0xFFFF , (position , value ) -> new ConstantUShortExpression (position , value .intValue ()));
176176 case INT :
177- return castIfFits (cast , signed >= Integer .MIN_VALUE && signed <= Integer .MAX_VALUE , (position , value ) -> new ConstantIntExpression (position , value .intValue ()));
177+ return castIfFits (cast , type , signed >= Integer .MIN_VALUE && signed <= Integer .MAX_VALUE , (position , value ) -> new ConstantIntExpression (position , value .intValue ()));
178178 case UINT :
179- return castIfFits (cast , !negative && value <= 0xFFFFFFFFL , (position , value ) -> new ConstantUIntExpression (position , value .intValue ()));
179+ return castIfFits (cast , type , !negative && value <= 0xFFFFFFFFL , (position , value ) -> new ConstantUIntExpression (position , value .intValue ()));
180180 case USIZE :
181- return castIfFits (cast , !negative && value <= 0xFFFFFFFFL , ConstantUSizeExpression ::new );
181+ return castIfFits (cast , type , !negative && value <= 0xFFFFFFFFL , ConstantUSizeExpression ::new );
182182 case LONG :
183- return cast .of (new ConstantLongExpression (position , value ));
183+ return cast .of (levelFor ( type ), new ConstantLongExpression (position , value ));
184184 case ULONG :
185- return castIfFits (cast , !negative , ConstantULongExpression ::new );
185+ return castIfFits (cast , type , !negative , ConstantULongExpression ::new );
186186 case FLOAT :
187- return cast .of (new ConstantFloatExpression (position , value ));
187+ return cast .of (levelFor ( type ), new ConstantFloatExpression (position , value ));
188188 case DOUBLE :
189- return cast .of (new ConstantDoubleExpression (position , value ));
189+ return cast .of (levelFor ( type ), new ConstantDoubleExpression (position , value ));
190190 }
191191
192192 return cast .of (eval ());
193193 }
194194
195- private CastedExpression castIfFits (CastedEval cast , boolean fits , BiFunction <CodePosition , Long , Expression > expr ){
195+ // Only the literal's natural type (int, or long if it doesn't fit) is an exact match;
196+ // any other numeric type it fits into is inferred, so that e.g. 2.0 / 0 prefers the int overload
197+ private CastedExpression .Level levelFor (BasicTypeID type ) {
198+ BasicTypeID natural = value <= Integer .MAX_VALUE && value >= Integer .MIN_VALUE ? BasicTypeID .INT : BasicTypeID .LONG ;
199+ return type == natural ? CastedExpression .Level .EXACT : CastedExpression .Level .INFERRED ;
200+ }
201+
202+ private CastedExpression castIfFits (CastedEval cast , BasicTypeID type , boolean fits , BiFunction <CodePosition , Long , Expression > expr ){
196203 if (fits ) {
197- return cast .of (CastedExpression . Level . EXACT , expr .apply (this .position , this .value ));
204+ return cast .of (levelFor ( type ) , expr .apply (this .position , this .value ));
198205 } else if (cast .isExplicit ()) {
199206 return cast .of (CastedExpression .Level .EXPLICIT , expr .apply (this .position , this .value ));
200207 }
0 commit comments