Opened 5 years ago

Last modified 7 weeks ago

#185 new defect

Cannot declare generic struct with zero type parameters

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

Description (last modified by mlbrooks)

Wishing for:

forall ( | {void fun();} ) {

    struct thing {};

    void ?{}( thing & this ) {
        fun();
    }
}

void fun() {
    printf("this is fun\n");
}

int main() {
   thing x;
}

That is, the forall is used only to declare an assertion.

Expect: compile succeeds; run prints

this is fun

Actual: compiler error

Too few type arguments in generic type instance of struct thing with body 1

Workaround is to treat the generic type as if it had one type parameter, i.e.

forall ( dtype T | {void fun();} ) {

    struct thing ...

This workaround is

forall ( | {void fun();} ) {

    struct thing {};

    void ?{}( thing(float) & this ) {
        fun();
    }
}

void fun() {
    printf("this is fun\n");
}

int main() {
   thing(float) x;
}

Workaround Expected: compiler error

Too many type arguments in generic type instance of struct thing...

Workaround Actual: compile succeeds; run prints

this is fun

The cause (as originally captured in #222) of the issue is that the parser adds the extra parameter. To see it:

forall (T & ) {
  forall ( | {void foo (T *); }) struct bar_t { T * fld; };
}

Actual prefix of compiling -CFA -XCFA,-p,-Past:

struct bar_t with body
... with parameters
  T: data type
  data type
  ...

Note that bar_t has two parameters (the two lines following ... with parameters), the first named T, the second anonymous.

Expecting one parameter named T.

Change History (2)

comment:1 by mlbrooks, 5 years ago

Priority: majorminor

Fixing status; should have been opened as minor.

comment:2 by mlbrooks, 7 weeks ago

Description: modified (diff)

Was partly fixed by [119889f], but not reverse-linked at that time.
The fix applies to functions, but not types.
The original repro in here still fails.
Adding explanation of cause from #222.

Note: See TracTickets for help on using tickets.