Skip to content

Conversation

@rauhul
Copy link
Collaborator

@rauhul rauhul commented Dec 15, 2024

Validates the bit ranges passed to bit field macros do not overlap nor
do they extend past the register bounds. In either case, diagnostics are
emitted indicating the offending attribute and offending range[s].

Given the example bit field:

@BitField(bits: 0..<24, 8..<32, 16..<48, 36..<44)
var field: Field

The ranges visually look like:

0       8       16      24      32  36      44  48
╎       ╎       ╎       ╎       ╎   ╎       ╎   ╎
•───────────────────────◦       ╎   ╎       ╎   ╎
╎       •───────────────────────◦   ╎       ╎   ╎
╎       ╎       •───────────────────────────────◦
╎       ╎       ╎       ╎       ╎   •───────◦   ╎
╎       ╎       ╎       ╎       ╎   ╎       ╎   ╎
0       8       16      24      32  36      44  48

The following diagnostics will be emitted:

<location> error: overlapping bit ranges in '@BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)'
@BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
 ^~~~~~~~

<location> note: bit range '0..<24' overlaps bit ranges '8..<32' and '16..<48' over subrange '8..<24'
@BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
                ^~~~~~

<location> note: bit range '8..<32' overlaps bit ranges '0..<24' and '16..<48'
@BitField(bits: 0..<24, 8..<32, 16..<48, 36..<44)
                        ^~~~~~

<location> note: bit range '16..<48' overlaps bit ranges '0..<24', '8..<32', and '36..<44' over anges '16..<32' and '36..<44'
@BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
                                ^~~~~~~

<location> note: bit range '36..<44' overlaps bit range '16..<48'
@BitField(bits: 0..<24, 8..<40, 16..<48, 36..<44)
                                         ^~~~~~~

@rauhul rauhul requested a review from shahmishal as a code owner December 15, 2024 19:30
@rauhul rauhul force-pushed the bitrange-diagnostics branch from 4c22222 to 26a0e6a Compare December 15, 2024 19:37
Validates the bit ranges passed to bit field macros do not overlap nor
do they extend past the register bounds. In either case, diagnostics are
emitted indicating the offending attribute and offending range[s].

Given the example bit field:
```swift
@bitfield(bits: 0..<24, 8..<32, 16..<48, 36..<44)
var field: Field
```

The ranges visually look like:
```
0       8       16      24      32  36      44  48
╎       ╎       ╎       ╎       ╎   ╎       ╎   ╎
•───────────────────────◦       ╎   ╎       ╎   ╎
╎       •───────────────────────◦   ╎       ╎   ╎
╎       ╎       •───────────────────────────────◦
╎       ╎       ╎       ╎       ╎   •───────◦   ╎
╎       ╎       ╎       ╎       ╎   ╎       ╎   ╎
0       8       16      24      32  36      44  48
```

The following diagnostics will be emitted:
```
<location> error: overlapping bit ranges in '@bitfield(bits: 0..<24, 8..<40, 16..<48, 36..<44)'
@bitfield(bits: 0..<24, 8..<40, 16..<48, 36..<44)
 ^~~~~~~~

<location> note: bit range '0..<24' overlaps bit ranges '8..<32' and '16..<48' over subrange '8..<24'
@bitfield(bits: 0..<24, 8..<40, 16..<48, 36..<44)
                ^~~~~~

<location> note: bit range '8..<32' overlaps bit ranges '0..<24' and '16..<48'
@bitfield(bits: 0..<24, 8..<32, 16..<48, 36..<44)
                        ^~~~~~

<location> note: bit range '16..<48' overlaps bit ranges '0..<24', '8..<32', and '36..<44' over anges '16..<32' and '36..<44'
@bitfield(bits: 0..<24, 8..<40, 16..<48, 36..<44)
                                ^~~~~~~

<location> note: bit range '36..<44' overlaps bit range '16..<48'
@bitfield(bits: 0..<24, 8..<40, 16..<48, 36..<44)
                                         ^~~~~~~
```
@rauhul rauhul force-pushed the bitrange-diagnostics branch from 26a0e6a to c48bb6f Compare December 16, 2024 07:37
@rauhul rauhul merged commit 58d4935 into main Dec 16, 2024
11 checks passed
@rauhul rauhul deleted the bitrange-diagnostics branch December 16, 2024 19:01
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.

2 participants