Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes the unused braces warning in the autogenerated From implementations. #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

diseraluca
Copy link

@diseraluca diseraluca commented May 27, 2021

Resolves #66.

Previously, From implementations may have expanded to code similar to the
following:

...
where
    [(); { Size }]:
        ::modular_bitfield::private::IsU32Compatible,
...

Where Size is a constant expression that reduces to a usize.
With Size being an expression and the context in which it is used, the braces
around it are unneeded, producing a warning.

The braces are inserted in impl::bitfield::expand::generate_bitfield_size,
surrounding the returned sum-expression.

To avoid the warning, generate_bitfield_size was modified to return a naked
expression.

Places where brackets were needed to avoid an incorrect reading of the
expression were modified to add the brackets in place.
Similarly, places where the brackets were not needed were simplified.


  • Tests, even on a clean download of the repository, seems to be currently broken, at least on my system, with multiple failing testes and a certain amount of panics.

    This means that I was unable to use the tests to avoid regressions or prescribed errors. I cannot, hence, be sure about the correctness of each case.

If this is intended to be accepted, please let me know what I should do meet quality control, be it adding tests or something else.

Previously, `From` implementations may have expanded to code similar to the
following:

```
...
where
    [(); { Size }]:
        ::modular_bitfield::private::IsU32Compatible,
...
```

Where `Size` is a constant expression that reduces to a `usize`.
With `Size` being an expression and the context in which it is used, the braces
around it are unneeded, producing a warning.

The braces are inserted in `impl::bitfield::expand::generate_bitfield_size`,
surrounding the returned sum-expression.

To avoid the warning, `generate_bitfield_size` was modified to return a naked
expression.

Places where brackets were needed to avoid an incorrect reading of the
expression were modified to add the brackets in place.
Similarly, places where the brackets were not needed were simplified.
@diseraluca diseraluca changed the title Removes the unused braces warning from the macro expansion. Removes the unused braces warning in the autogenerated From implementations. May 27, 2021
diseraluca added a commit to diseraluca/uavcan that referenced this pull request May 27, 2021
The modular bitfield crate produces an unneccesary braces warning when `From`
implementations are generated.

See: Robbepop/modular-bitfield#66

A pull request ( Robbepop/modular-bitfield#68 ) to
resolve this was provided.

Since it is not yet accepted mainstream, the `modular-bitfield` dependency was
temporarily moved to a custom fork to move forward with the removal of
compilation warnings.
Copy link
Owner

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!
I think it is really important to silence this annoying warning.
Could you please try to add a parentheses around #lhs + #rhs in the generate_bitfield_size function just to be sure about expression ordering. This should ideally not result in warnings anymore but still be more explicit about expression ordering (not relying on implicit orderings).

@diseraluca
Copy link
Author

HI @Robbepop,

Thank you for taking the time to check this.

I've added your suggestion in 910b17d.

@Robbepop
Copy link
Owner

HI @Robbepop,

Thank you for taking the time to check this.

I've added your suggestion in 910b17d.

Can you also confirm that this still fixes the issues with the warnings?

Furthermore, the documentation for the function was modified to be consistently aligned.
@diseraluca
Copy link
Author

diseraluca commented May 28, 2021

HI @Robbepop,
Thank you for taking the time to check this.
I've added your suggestion in 910b17d.

Can you also confirm that this still fixes the issues with the warnings?

Sorry I've been sloppy with this as I wasn't at my usual computer and tested only on a small playground.rs.

I've now tested with my library and, indeed, the parenthesis generate a warning. This was, for now, reverted in b9b81df.

A similar case to the one for the from implementation was identified in next_divisible_by_8. The brace were similarly removed. See d707fe2.
I've checked the 4 places where the expression is used and there should be no problem with parenthesization. I'm not sure if a different format should be tried to ease the reading of the expanded expressions.

Furthermore, testing in another library I identified a case that was broken by the original change, in expand_byte_conversion_impls. This was currently fixed in 735e841.

I expect a warning to be generated by the new parenthesization when genenerate_target_or_actual_bitfield_value does not generate a sum but only a single value.
I wasn't able to test this, currently, as I'm not sure how to generate that case. If you can provide an example to test with I would be glad to do it.

Recently, `generate_bitfield_size` was modified to return an expression not
contained in a block, to avoid generating an `unused_braces` warning.
See: Robbepop#66

To simplify the reading of the generated code and avoid ambiguities with
operators' precedence, further parenthesization was added to the expression
itself.

The new parenthesization generates an `unused_parenthesis` warning and is now removed.
Similarly to the recent changes to `generate_bitfield_size`, the block
surrounding the expression returned by `next_divisible_by_8` was removed to
avoid the generation of an `unused_braces` warning.
After the recent changes to `generate_bitfield_size`, the returned expression is
not enclosed in a block.

In `expand_byte_conversion_impls`, when `filled` is not enabled, an expression
of the following form is generated:

```
if bytes[...#next_divisible_by_8 - #size)))
```

Where `#size` is a value returned by `generate_bitfield_size`.
Since `#size` may take the form of a sum, it is now possible to generate an
expression of the form `#next_divisible_by_8 - size_1 + size_2 + ... + size_n`
which is not equivalent to the previous behavior, where `#size` was first
reduced to a single value and the subtracted, and results in a bigger result.

To avoid this issue, `#size` is now parenthesized in the expression to perform
the correct reduction.
@jumpnbrownweasel
Copy link

I can't find a way to suppress this short of adding #[allow(unused_braces)] for the entire module. Adding it above the struct doesn't work.

There are also unused warnings generated.

warning: associated function is never used: `into_bytes`
warning: associated function is never used: `from_bytes`

For this I had to add #[allow(dead_code)] for the entire module.

For now I'll use the bitfield crate instead. I would be happy to use this instead if it looks like it is being actively maintained. I think the API is quite good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove the unnecessary braces around From implementations.
3 participants