Opened 3 years ago

#245 new defect

Reference-to-reference misses required implicit dereference

Reported by: mlbrooks Owned by:
Priority: minor Component: cfa-cc
Version: 1.0 Keywords:
Cc:

Description

A lot of double-reference behaviour does work. Here is a detail that does not.

When calling a function that takes a single-reference parameter, using a double-reference variable as the argument, the resolver allows the type coercion (&& to &), which ought to imply a dereference, but no such dereference occurs.

The example also provides a workaround.

#ifdef POLY
  #define POLY_DECL(T) forall( T & )
#else
  #define POLY_DECL(T)
  #define T float
#endif

POLY_DECL(T)
void helper(T & x) {

    printf("helper sees var at %p\n", & x);

    // cast is working correctly, meaning consistently with the address just printed
    // cast is redundant on non-POLY version
    float val = (float &) x;
    printf("helper sees pi  as %f\n", val);
}

POLY_DECL(T)
void worker(T && x) {

    printf("worker sees var at %p\n", & x);
    printf("worker sees ref at %p\n", && x);

    printf("no workaround:\n");
    helper(x);

    printf("with workaround:\n");
    T & xx = x;
    helper(xx);
}

int main() {
    float var = 3.14;
    float & ref = var;

    printf("main   sees pi  as %f\n", var);
    printf("main   sees pi  as %f\n", ref);
    printf("main   sees var at %p\n", & var);
    printf("main   sees var at %p\n", & ref);
    printf("main   sees ref at %p\n", && ref);
    worker(ref);
}

The -DPOLY control only influences whether or not GCC issues a warning about the problem. The problem is the same in both cases.

Actual, plain: GCC warning: passing argument 1 of _X6helperFv_f1 from incompatible pointer type ... float . Result runs with this output:

main   sees pi  as 3.140000
main   sees pi  as 3.140000
main   sees var at 0x7fffffffe318
main   sees var at 0x7fffffffe318
main   sees ref at 0x7fffffffe320
worker sees var at 0x7fffffffe318
worker sees ref at 0x7fffffffe320
no workaround:
helper sees var at 0x7fffffffe320
helper sees pi  as -nan
with workaround:
helper sees var at 0x7fffffffe318
helper sees pi  as 3.140000

Actual, -DPOLY: No warning. Same output as plain.

Expected, both:

main   sees pi  as 3.140000
main   sees pi  as 3.140000
main   sees var at 0x7fffffffe318
main   sees var at 0x7fffffffe318
main   sees ref at 0x7fffffffe320
worker sees var at 0x7fffffffe318
worker sees ref at 0x7fffffffe320
no workaround:
helper sees var at 0x7fffffffe318
helper sees pi  as 3.140000
with workaround:
helper sees var at 0x7fffffffe318
helper sees pi  as 3.140000

Change History (0)

Note: See TracTickets for help on using tickets.