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

[SR-5298] Swift 3.2 source breakage on C enum import #47873

Closed
swift-ci opened this issue Jun 24, 2017 · 7 comments
Closed

[SR-5298] Swift 3.2 source breakage on C enum import #47873

swift-ci opened this issue Jun 24, 2017 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.0

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5298
Radar rdar://problem/33007372
Original Reporter florent.bruneau (JIRA User)
Type Bug
Status Resolved
Resolution Won't Do
Environment

Linux

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.0Regression, ClangImporter
Assignee None
Priority Medium

md5: 358d6880992e49579a58a90bae318431

Issue Description:

I import the following C enum in Swift:

typedef enum ic__ic_priority__t {
    IC_PRIORITY_LOW,
    IC_PRIORITY_NORMAL,
    IC_PRIORITY_HIGH,
} ic__ic_priority__t;

With Swift 3.1, the enum is imported as a RawRepresable with RawValue = Int32, while in Swift 3.2, the RawValue becomes an UInt32 causing compilation error even when enforcing the -swift-version 3 flag.

@belkadan
Copy link
Contributor

I'm afraid we'll need more context than this. Where is this enum defined? What are the definitions of the IC_* constants on your version of the OS?

@swift-ci
Copy link
Collaborator Author

Comment by Florent Bruneau (JIRA)

In order to reproduce you can use those three files:

module.map:

module blah {
    header "blah.h"
}

blah.h:

typedef enum ic__ic_priority__t {
    IC_PRIORITY_LOW,
    IC_PRIORITY_NORMAL,
    IC_PRIORITY_HIGH,
} ic__ic_priority__t;

and blah.swift:

protocol MyEnumWrapper : RawRepresentable {
    typealias RawValue = Int32
}

extension ic__ic_priority__t : MyEnumWrapper { }

Then run the following command in the directory where you put the three files:

swiftc -swift-version 3 -I. -emit-module -module-name blah -import-underlying-module blah.swift

With swift 3.1, this works without any warning.
With swift 3.2, I got the following output:

blah.swift:2:15: warning: typealias overriding associated type 'RawValue' from protocol 'RawRepresentable' is better expressed as same-type constraint on the protocol
    typealias RawValue = Int32
    ~~~~~~~~~~^~~~~~~~~~~~~~~~

Swift.RawRepresentable:94:20: note: 'RawValue' declared here
    associatedtype RawValue
                   ^
blah.swift:5:1: error: 'MyEnumWrapper' requires the types 'ic__ic_priority__t.RawValue' (aka 'UInt32') and 'Int32' be equivalent
extension ic__ic_priority__t : MyEnumWrapper { }
^
blah.swift:5:1: note: requirement specified as 'Self.RawValue' == 'Int32' [with Self = ic__ic_priority__t]
extension ic__ic_priority__t : MyEnumWrapper { }
^

@belkadan
Copy link
Contributor

Huh. Thanks, Florent!

@swift-ci create

@belkadan
Copy link
Contributor

Okay, as far as I can tell nothing has changed in how we import the enum—i.e. it's always had a UInt32 raw type. So what's changed is that we've gotten better at telling that all your associated types need to be consistent.

@belkadan
Copy link
Contributor

@slavapestov, do you remember where we had this discussion? I found SR-4382, but I remember us talking somewhere else as well.

@belkadan
Copy link
Contributor

Florent, you can confirm that the old behavior used UInt32 with this code:

protocol MyEnumWrapper : RawRepresentable {
    typealias RawValue = Int32
}

extension ic__ic_priority__t : MyEnumWrapper { }

func getRawType<T: RawRepresentable>(_: T.Type) -> T.RawValue.Type {
  return T.RawValue.self
}
print(ic__ic_priority__t.RawValue.self)
print(getRawType(ic__ic_priority__t.self))

@swift-ci
Copy link
Collaborator Author

Comment by Florent Bruneau (JIRA)

Hi Jordan,

You're right, the RawValue is indeed an UInt32 with Swift 3.1

% swiftc -swift-version 3 -I. -module-name blah -import-underlying-module blah.swift
% ./blah
UInt32
UInt32

Unless the enum contains some negative values, in which case it switches to Int32.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.0
Projects
None yet
Development

No branches or pull requests

3 participants