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-5671] Compile failure for Type-Casting Pattern of Array downcasting #48241

Open
rintaro mannequin opened this issue Aug 10, 2017 · 9 comments
Open

[SR-5671] Compile failure for Type-Casting Pattern of Array downcasting #48241

rintaro mannequin opened this issue Aug 10, 2017 · 9 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@rintaro
Copy link
Mannequin

rintaro mannequin commented Aug 10, 2017

Previous ID SR-5671
Radar rdar://problem/33828871
Original Reporter @rintaro
Type Bug
Environment

Version 9.0 beta 4 (9M189t)
Apple Swift version 4.0 (swiftlang-900.0.54.11 clang-900.0.31)
Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 3
Component/s Compiler
Labels Bug
Assignee @CodaFi
Priority Medium

md5: 6d61b5b6e53a73e776bbfc702cfae310

Issue Description:

class Animal {}
class Cat : Animal {}
class Dog : Animal {}

func check(_ arry: [Animal]) {
  switch arry {
  case is [Cat]:
    ()
  case let dogs as [Dog]:
    ()
  default:
    ()
  }
}

results:

test.swift:7:8: error: optional type '[Cat]?' cannot be used as a boolean; test for '!= nil' instead
  case is [Cat]:
       ^
       ((      ) != nil)
test.swift:9:17: error: downcast pattern value of type '[Dog]' cannot be used
  case let dogs as [Dog]:
                ^

This should compile.

@belkadan
Copy link
Contributor

This is strange. CodaFi (JIRA User), any insights?

@belkadan
Copy link
Contributor

@swift-ci create

@CodaFi
Copy link
Member

CodaFi commented Aug 11, 2017

This is the remnant of an ancient would-be feature that even extends to bridging the collection through Objective-C. It is converting the 'is' check into an as? check, but isn't including the comparator value so we're diagnosing it. I'm torn as to whether this can be removed altogether since it doesn't seem like it ever worked...

@DougGregor?

@CodaFi
Copy link
Member

CodaFi commented Aug 11, 2017

More concerning to me is that asking the runtime

[Dog]() is [Cat]

returns true despite a diagnosis to the contrary:

<REPL Input>:1:9: warning: cast from '[Dog]' to unrelated type '[Cat]' always fails
[Dog]() is [Cat]
~~~~~~~ ^  ~~~~~
// r1 : Bool = true

@belkadan
Copy link
Contributor

That last sounds like the problem with nil Optionals being freely convertible. @swiftix, @jckarter?

@swiftix
Copy link
Mannequin

swiftix mannequin commented Aug 11, 2017

I checked that the cast is not eliminated by the optimizer. Thus is must be an issue with the runtime. @jckarter could you have a look?

@CodaFi
Copy link
Member

CodaFi commented Aug 11, 2017

At least to get the diagnostic QoI side of this #11441

@jckarter
Copy link
Member

A [Dog] is a [Cat] because arrays are covariant. The runtime is correct, but the warning is wrong. The errors from the original bug report might be remnants from when array covariance wasn't fully integrated with the runtime dynamic cast system, so weren't supported in pattern matches, but that shouldn't be a problem anymore, and we should accept the patterns.

@CodaFi
Copy link
Member

CodaFi commented Aug 14, 2017

The runtime issue is unrelated to the switch issue. I just factored the 'is' check the pattern effectively performs into its own distinct expression

[Dog]() is [Cat]

If covariance applied to this expression, we could derive a contradiction.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

3 participants