Catch and warn demotion of double in assignments

This commit is contained in:
Bill Currie 2020-02-15 17:24:53 +09:00
parent 08ca59d0df
commit c5ce18591f
4 changed files with 25 additions and 1 deletions

View file

@ -151,6 +151,10 @@ check_types_compatible (expr_t *dst, expr_t *src)
if (type_assignable (dst_type, src_type)) {
if (is_scalar (dst_type) && is_scalar (src_type)) {
if (is_double (src_type)) {
warning (dst, "assignment of double to %s (use a cast)\n",
dst_type->name);
}
// the types are different but cast-compatible
expr_t *new = cast_expr (dst_type, src);
// the cast was a no-op, so the types are compatible at the

View file

@ -61,8 +61,10 @@ test_progs_dat=\
fail_progs_dat=
test_build_errors=\
double-demote-int.r \
double-demote-float.r \
double-demote-float-linit.r \
double-demote-int.r \
double-demote-int-linit.r \
double-int-compare.r \
double-float-compare.r
@ -161,6 +163,12 @@ double-demote-int.run$(EXEEXT): double-demote-int.r Makefile build-compile-fail-
double-demote-float.run$(EXEEXT): double-demote-float.r Makefile build-compile-fail-run
$(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $<
double-demote-int-linit.run$(EXEEXT): double-demote-int-linit.r Makefile build-compile-fail-run
$(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $<
double-demote-float-linit.run$(EXEEXT): double-demote-float-linit.r Makefile build-compile-fail-run
$(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $<
double-int-compare.run$(EXEEXT): double-int-compare.r Makefile build-compile-fail-run
$(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $<

View file

@ -0,0 +1,6 @@
double a;
int main ()
{
float b = a;
return 1; // test fails if compile succeeds
}

View file

@ -0,0 +1,6 @@
double a;
int main ()
{
int b = a;
return 1; // test fails if compile succeeds
}