Catch double demotion in global initializers

Local initializers are handled by regular assignments
This commit is contained in:
Bill Currie 2020-02-15 17:42:46 +09:00
parent c5ce18591f
commit 6ce99afa5b
4 changed files with 27 additions and 1 deletions

View file

@ -575,7 +575,8 @@ initialize_def (symbol_t *sym, expr_t *init, defspace_t *space,
init_elements (sym->s.def, init);
sym->s.def->initialized = 1;
} else {
if (!type_assignable (sym->type, get_type (init))) {
type_t *init_type = get_type (init);
if (!type_assignable (sym->type, init_type)) {
error (init, "type mismatch in initializer");
return;
}
@ -609,6 +610,11 @@ initialize_def (symbol_t *sym, expr_t *init, defspace_t *space,
reloc_def_field (init->e.value->v.pointer.def, sym->s.def);
} else {
ex_value_t *v = init->e.value;
if (is_double (init_type)
&& (is_integral (sym->type) || is_float (sym->type))) {
warning (init, "assigning double to %s in initializer "
"(use a cast)", sym->type->name);
}
if (is_scalar (sym->type))
v = convert_value (v, sym->type);
if (v->lltype == ev_string) {

View file

@ -62,8 +62,10 @@ fail_progs_dat=
test_build_errors=\
double-demote-float.r \
double-demote-float-ginit.r \
double-demote-float-linit.r \
double-demote-int.r \
double-demote-int-ginit.r \
double-demote-int-linit.r \
double-int-compare.r \
double-float-compare.r
@ -163,6 +165,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-ginit.run$(EXEEXT): double-demote-int-ginit.r Makefile build-compile-fail-run
$(srcdir)/build-compile-fail-run $@ $(QFCC) $(QCFLAGS) $<
double-demote-float-ginit.run$(EXEEXT): double-demote-float-ginit.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) $<

View file

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

View file

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