# Swift 6 failing to implicitly open existentials?

**URL:** <https://forums.swift.org/t/swift-6-failing-to-implicitly-open-existentials/73188>\
**Category:** Using Swift\
**Tags:** metatype, existential\
**Created:** [July 16, 2024, 12:56am UTC](https://forums.swift.org/t/swift-6-failing-to-implicitly-open-existentials/73188 "2024-07-16T00:56:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 16, 2024, 12:56am UTC](https://forums.swift.org/t/swift-6-failing-to-implicitly-open-existentials/73188/1 "2024-07-16T00:56:32Z")

</div>

Hi! I'm experimenting with Implicit Open Existentials in Swift 6 mode. I'm seeing a confusing error and I need the same workaround I used for 5.10. I'm not sure why I need this workaround. Should this work out-of-the-box in Swift 6 mode?

```swift
struct S { }

struct Box<T> {
  
}

func box<T>(_: T.Type) -> Box<T> {
  Box<T>()
}

func main() {
  func body<Value>(_: Value.Type) -> Value.Type { Value.self }
  
  let metatype: Any.Type = S.self
  
  let _ = body(metatype)
  
  let _ = box(_openExistential(metatype, do: body))
  
  let _ = box(metatype)
}

```

Here are the errors:

```plaintext
error: generic parameter 'T' could not be inferred
 5 | }
 6 | 
 7 | func box<T>(_: T.Type) -> Box<T> {
   | `- note: in call to function 'box'
 8 | Box<T>()
 9 | }
   :
18 | let _ = box(_openExistential(metatype, do: body))
19 |   
20 | let _ = box(metatype)
   | `- error: generic parameter 'T' could not be inferred
21 | }
22 | 

error: cannot convert value of type 'any Any.Type' to expected argument type 'T.Type'
18 | let _ = box(_openExistential(metatype, do: body))
19 |   
20 | let _ = box(metatype)
   | `- error: cannot convert value of type 'any Any.Type' to expected argument type 'T.Type'
21 | }
22 | 

```

Why would the call to my `box` function fail when passing an existential metatype? Would this not be enabled already in Swift 6? Why would I still need the `_openExistential` workaround from 5.10 to compile?

There are some examples in [SE-0352](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md) where we can't open an existential… but I'm not sure I understand how my example would translate to those (known) limitations. Any ideas?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [July 16, 2024, 1:37am UTC](https://forums.swift.org/t/swift-6-failing-to-implicitly-open-existentials/73188/2 "2024-07-16T01:37:34Z")

</div>

> [@vanvoorden](#):
>
> Why would I still need the `_openExistential` workaround from 5.10 to compile?

This example minus the last statement (i.e., your workaround) _doesn't_ compile (on [godbolt.org](http://godbolt.org)) in 5.10 or in the nightly. The error in 5.10 is: "Found opened existential archetype Any outside enclosing OpenExistentialExpr"; and in the nightly: "Found local archetype outside its defining scope".

> [@vanvoorden](#):
>
> Why would the call to my `box` function fail when passing an existential metatype?

Isn't this what the proposal covers explicitly in the text quoted below?

> When `T` or a `T`-rooted associated type appears in a non-covariant position in the result type, **`T` cannot be bound to the underlying type of an existential value because there would be no way to represent the type-erased result.** This is essentially the same property as descibed for the parameter types that prevents opening of existentials, as described above. For example:
> 
> ```
> func cannotOpen7<T: P>(_ value: T) -> X<T> { /*...*/ }
> 
> ```

---

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [July 16, 2024, 3:08am UTC](https://forums.swift.org/t/swift-6-failing-to-implicitly-open-existentials/73188/3 "2024-07-16T03:08:48Z")

</div>

> [@xwu](#):
>
> This example minus the last statement (i.e., your workaround) _doesn't_ compile (on [godbolt.org](http://godbolt.org)) in 5.10 or in the nightly. The error in 5.10 is: "Found opened existential archetype Any outside enclosing OpenExistentialExpr"; and in the nightly: "Found local archetype outside its defining scope".

Hmm… I built from `Xcode_16_beta_3` and did not see that. Weird!

> [@xwu](#):
>
> Isn't this what the proposal covers explicitly in the text quoted below?
> 
> > When `T` or a `T`-rooted associated type appears in a non-covariant position in the result type, **`T` cannot be bound to the underlying type of an existential value because there would be no way to represent the type-erased result.** This is essentially the same property as descibed for the parameter types that prevents opening of existentials, as described above. For example:
> > 
> > ```swift
> > func cannotOpen7<T: P>(_ value: T) -> X<T> { /*...*/ }
> > 
> > ```

Ahh… correct! I see it now. Thanks!
