Skip to content

Commit 9448d62

Browse files
committed
report error on lying type hints rather than emit unverifiable code. see CLJ-1846
1 parent 69b89b5 commit 9448d62

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/jvm/clojure/lang/Compiler.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ else if(primc == double.class && parameterTypes[i] == float.class)
14241424
}
14251425
catch(Exception e1)
14261426
{
1427-
e1.printStackTrace(RT.errPrintWriter());
1427+
throw Util.sneakyThrow(e1);
14281428
}
14291429

14301430
}
@@ -1606,7 +1606,7 @@ public boolean hasJavaClass(){
16061606
}
16071607

16081608
public Class getJavaClass() {
1609-
return tag != null ? HostExpr.tagToClass(tag) : method.getReturnType();
1609+
return retType((tag!=null)?HostExpr.tagToClass(tag):null, (method!=null)?method.getReturnType():null);
16101610
}
16111611
}
16121612

@@ -1819,7 +1819,7 @@ public boolean hasJavaClass(){
18191819
}
18201820

18211821
public Class getJavaClass() {
1822-
return tag != null ? HostExpr.tagToClass(tag) : method.getReturnType();
1822+
return retType((tag!=null)?HostExpr.tagToClass(tag):null, (method!=null)?method.getReturnType():null);
18231823
}
18241824
}
18251825

@@ -3465,7 +3465,7 @@ public boolean hasJavaClass() {
34653465
}
34663466

34673467
public Class getJavaClass() {
3468-
return tag != null ? HostExpr.tagToClass(tag) : retClass;
3468+
return retType((tag!=null)?HostExpr.tagToClass(tag):null, retClass);
34693469
}
34703470

34713471
public boolean canEmitPrimitive(){
@@ -8319,6 +8319,27 @@ public void emit(ObjExpr obj, ClassVisitor cv){
83198319
}
83208320
}
83218321

8322+
static boolean inty(Class c){
8323+
return c == int.class
8324+
|| c == short.class
8325+
|| c == byte.class
8326+
|| c == char.class;
8327+
}
8328+
8329+
static Class retType(Class tc, Class ret){
8330+
if(tc == null)
8331+
return ret;
8332+
if(ret == null)
8333+
return tc;
8334+
if(ret.isPrimitive() && tc.isPrimitive()){
8335+
if((inty(ret) && inty(tc)) || (ret == tc))
8336+
return tc;
8337+
throw new UnsupportedOperationException("Cannot coerce " + ret +
8338+
" to " + tc + ", use a cast instead");
8339+
}
8340+
return tc;
8341+
}
8342+
83228343
static Class primClass(Symbol sym){
83238344
if(sym == null)
83248345
return null;

0 commit comments

Comments
 (0)