c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Anonymous types make some errors misleading

Open 31a05b9c opened this issue 2 years ago • 3 comments

When a type needs to be named in an error, anonymous types are just represented by the field's name. This has some overlooked consequences. For example the following code:

struct Foo
{
    bitstruct anon : char
    {
         bool a : 0;
         char b : 1..7;
    }
}

fn void test()
{
      Foo foo;
      char x = 32;
      foo.anon = x;  // Implicit cast not allowed
}

produces an error suggesting a syntactically incorrect fix

Error: Implicitly casting 'char' to 'anon' is not permitted, 
but you may do an explicit cast by placing '(anon)' before the expression

Where it should just say that casts to anonymous types are forbidden.

Other error messages might also be affected, although i have not found any.

31a05b9c avatar Sep 21 '24 10:09 31a05b9c

Could you have a look if it's better now?

lerno avatar Sep 24 '24 16:09 lerno

@lerno Using the example code:

struct Foo
{
    bitstruct anon : char
    {
        bool a : 0;
        char b : 1..7;
    }
}

fn void test()
{
    Foo foo;
    char x = 32;
    foo.anon = x;  // Implicit cast not allowed
}

Now results in a nicer error message: Error: Implicitly casting 'char' to the inner type 'Foo.anon' is not permitted, but you may do an explicit cast by placing '($typefrom(Foo.anon.typeid))' before the expression.

When using the suggestion, it will then proceed to compile as expected.

Caleb-rg avatar Sep 25 '24 15:09 Caleb-rg

Thank you for checking!

lerno avatar Sep 25 '24 19:09 lerno