Skip to content

Commit bb92d7b

Browse files
committed
oslc: slight adjustment to assignment typechecking (#993)
Consider int=float to be a warning, not an error. This is partly because C allows it and we ran accross some examples where it seems more strict than necessary. (Of course it works without warning if you use a cast, showing that any trucation is intentional.)
1 parent 096005a commit bb92d7b

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/liboslcomp/typecheck.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,13 @@ ASTvariable_declaration::typecheck (TypeSpec expected)
131131

132132
// Expression must be of a type assignable to the lvalue
133133
if (! assignable (vt, et)) {
134-
error ("Cannot assign %s %s = %s",
135-
type_c_str(vt), name(), type_c_str(et));
134+
// Special case: for int=float, it's just a warning.
135+
if (vt.is_int() && et.is_float())
136+
warning ("Assignment may lose precision: %s %s = %s",
137+
type_c_str(vt), name(), type_c_str(et));
138+
else
139+
error ("Cannot assign %s %s = %s",
140+
type_c_str(vt), name(), type_c_str(et));
136141
return m_typespec;
137142
}
138143

@@ -360,8 +365,13 @@ ASTassign_expression::typecheck (TypeSpec expected)
360365

361366
// Expression must be of a type assignable to the lvalue
362367
if (! assignable (vt, et)) {
363-
error ("Cannot assign %s %s = %s", type_c_str(vt), varname,
364-
type_c_str(et));
368+
// Special case: for int=float, it's just a warning.
369+
if (vt.is_int() && et.is_float())
370+
warning ("Assignment may lose precision: %s %s = %s",
371+
type_c_str(vt), varname, type_c_str(et));
372+
else
373+
error ("Cannot assign %s %s = %s",
374+
type_c_str(vt), varname, type_c_str(et));
365375
return m_typespec;
366376
}
367377

testsuite/oslc-err-assignmenttypes/ref/out.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
test.osl:17: error: Cannot assign int i = float
2-
test.osl:18: error: Cannot assign int i = float
1+
test.osl:17: warning: Assignment may lose precision: int i = float
2+
test.osl:18: warning: Assignment may lose precision: int i = float
33
test.osl:26: error: Cannot assign closure color cc = int
44
test.osl:32: error: Cannot initialize struct vector4 v4 = struct vector2
55
test.osl:34: error: Cannot assign struct vector2 v2 = struct vector4

0 commit comments

Comments
 (0)