Private here or private there? bug or something strange

I tried this:

extension USART3 {
   
    @Register(bitWidth: 32)
    public struct CR1 {
        /// USART enable
        @ReadWrite(bits: 0 ..< 1)
        public var ue: UE
    }
   /// more stuff
}

it works. But

public extension USART3 {
   
    @Register(bitWidth: 32)
     struct CR1 {
        /// USART enable
        @ReadWrite(bits: 0 ..< 1)
        public var ue: UE
    }
   /// more stuff
}

causes an error:

 error: property cannot be declared public because its type uses an internal type
 55 | public extension USART3 {
 56 |    
 57 |     @Register(bitWidth: 32)
    |     `- note: in expansion of macro 'Register' on struct 'CR1' here
 58 |     struct CR1 {
 59 |         /// USART enable
 60 |         @ReadWrite(bits: 0 ..< 1)
 61 |         public var ue: UE
    |                    `- error: property cannot be declared public because its type uses an internal type
 62 | 
 63 |         /// Receiver enable
    :
 96 |         @ReadWrite(bits: 10 ..< 11)
 97 |         public var pce: PCE
 98 |     }
    +--- macro expansion @Register -------------------------------------
    |  5 | private var _never: Never
    |  6 | 
    |  7 | enum UE: ContiguousBitField {
    |    |      `- note: type declared here
    |  8 |   typealias Storage = UInt32
    |  9 |   typealias Projection = Never
    +-------------------------------------------------------------------

Is it a bug or I don't know something?

And this works:

         /// Parity error
         @ReadOnly(bits: 0 ..< 1)
         public var pe: PE 

but this does not:

         @ReadOnly(bits: 0 ..< 1)  // Parity error
         public var pe: PE 

Funny.

Looks to me that the macro handles the visibility to the expanded code correctly when annotated properly (the first code snippet). The problem with the second snippet is the struct CR1 visibility is inferred to be internal by default (as Swift intends) by the macro because the extension has that context instead of the declaration itself (making introduced code internal). This is a limitation when working with macros, and good ones have solved this problem when introducing code. But not all solutions work for all contexts, like the second snippet.

As for the other 2 snippets, I don't have an explanation to offer (lacking context; I haven't done any embedded development in Swift yet).

Both of these are "just" bugs in my macro implementations. The first one should be a pretty easy fix, the second is very odd. Could you file two bugs on swift-mmio for these?

1 Like