Commit Graph

5 Commits

Author SHA1 Message Date
Arnold Schwaighofer
6eca97add6 Codesign test/Interpreter 2018-08-10 06:58:40 -07:00
Jordan Rose
357c7d6c05 Handle unexpected raw values for @objc enums in derived conformances (#17836)
Because people put all sorts of nonsense into @objc enums (most
reasonably, "private cases", which represent valid values that are not
API), the Swift-synthesized implementation of 'hash(into:)' needs to
not expect a switch statement to be exhaustive. And since
Swift-defined @objc enums are supposed to behave enough like C-defined
enums, they should at least handle simple method calls with an invalid
raw value, which means that 'rawValue' likewise should not use a
switch.

This patch provides alternate implementations that look like this:

extension ImportedEnum {
  public var rawValue: Int {
    return unsafeBitCast(self, to: Int.self)
  }

  public func hash(into hasher: inout Hasher) {
    hasher.combine(self.rawValue)
  }
}

rdar://problem/41913284
2018-07-11 08:44:03 -07:00
David Zarzycki
995dec5d82 [Sema] Error if ObjC interop is needed when disabled 2018-05-07 14:43:04 -04:00
Jordan Rose
9be6519f5a [SILGen] Show a message when an unexpected enum value is switched on (#15614)
Builds on 36eae9d4f6 to emit a message instead of just trapping
when a switch over a non-frozen enum ends up not matching anything.
If the enum is known to be an @objc enum, the message is

  unexpected enum case 'MyEnum(rawValue: -42)'

and if it's anything else (a Swift enum, a tuple containing enums,
whatever), it's a more opaque

  unexpected enum case while switching on value of type 'MyEnum'

The reason for this is to avoid calling String(describing:) or
String(reflecting:) an arbitrary value when the enum might conform to
CustomStringConvertible and therefore /itself/ have a switch that's
going to fall off the end. By handling plain @objc enums (using a
bitcast), we've at least covered the 90% case.

rdar://problem/37728359
2018-04-03 11:21:36 -07:00
Jordan Rose
36eae9d4f6 [SILGen] Generate a trap for unexpected cases in all @objc enums
(both C enums and Swift enums declared @objc), because of the
"feature" in C of treating a value not declared as a case as a valid
value of an enum.  No more undefined behavior here!

This bit can go in separately from all the work on exhaustive/frozen
enums, which is still being discussed and will come later.

rdar://problem/20420436
2018-02-21 10:34:59 -08:00