# Is this a compiler bug?

**URL:** <https://forums.swift.org/t/is-this-a-compiler-bug/57157>\
**Category:** Compiler\
**Tags:** literals, numeric\
**Created:** [May 5, 2022, 12:24pm UTC](https://forums.swift.org/t/is-this-a-compiler-bug/57157 "2022-05-05T12:24:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [May 5, 2022, 12:24pm UTC](https://forums.swift.org/t/is-this-a-compiler-bug/57157/1 "2022-05-05T12:24:11Z")

</div>

I expected this code to compile:

```swift
extension Sequence where Element: Numeric {
    
    func product () -> Element {
        self.reduce(into: .init(integerLiteral: 1)) { $0 *= $1 }
    }
}

```

But I'm hit with this error:

 ![Screen Shot 2022-05-05 at 2.22.10 PM](https://global.discourse-cdn.com/swift/original/3X/3/f/3f3746e2b0d2863e09ced66f3e23d616a8f14864.png)

Why would the `Element` itself need to conform to `_ExpressibleByBuiltinIntegerLiteral` when the protocol `ExpressibleByIntegerLiteral` requires only that the `IntegerLiteral` associated type conforms to it?

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [May 5, 2022, 12:43pm UTC](https://forums.swift.org/t/is-this-a-compiler-bug/57157/2 "2022-05-05T12:43:21Z")

</div>

That initializer requires its argument to be of the associated type `IntegerLiteralType`, which conforms to the underscored protocol.

The initializer is declared as a protocol requirement [here](https://github.com/apple/swift/blob/ff211b715ec5f15f9a11a4285b909719acb2217d/stdlib/public/core/CompilerProtocols.swift#L318) in CompilerProtocols.swift.

A default implementation is provided [here](https://github.com/apple/swift/blob/ff211b715ec5f15f9a11a4285b909719acb2217d/stdlib/public/core/Integers.swift#L18-L24) in Integers.swift, but only when `Self` conforms to the underscored protocol.

Now, the underscored protocol does _not_ refine `ExpressibleByIntegerLiteral`. In other words, there is no requirement that a type which conforms to the underscored protocol needs to be expressible by an integer literal.

In your example, `Element.IntegerLiteralType` conforms to the underscored protocol, but it is not constrained to conform to `ExpressibleByIntegerLiteral`.

So when the compiler sees that you are attempting to pass the integer literal `1` into the initializer, the only candidate it finds is the default implementation. But that is not a match, for the reason shown in the error message.

In summary, this is not a compiler error. Try writing this instead:

```swift
extension Sequence where Element: Numeric {
  func product () -> Element {
    reduce(into: 1, *=)
  }
}

```

Or this:

```swift
extension Sequence where Element: Numeric {
  func product () -> Element {
    reduce(1, *)
  }
}

```

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [May 5, 2022, 12:54pm UTC](https://forums.swift.org/t/is-this-a-compiler-bug/57157/3 "2022-05-05T12:54:05Z")

</div>

Awesome, thanks a lot for updated code! That's really all I need, but out of curiosity:

I was assuming that if a type conforms to `_ExpressibleByBuiltinIntegerLiteral` then I would be able to pass the literal `1` anywhere that a value of that type is required and it would work.

> [@Nevin](#):
>
> Now, the underscored protocol does _not_ refine `ExpressibleByIntegerLiteral` . In other words, there is no requirement that a type which conforms to the underscored protocol needs to be expressible by an integer literal.

Does this sentence of yours mean precisely that what I was assuming is not true?

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [May 5, 2022, 1:13pm UTC](https://forums.swift.org/t/is-this-a-compiler-bug/57157/4 "2022-05-05T13:13:12Z")

</div>

Indeed.
