# Why the Swift compiler allow literal expression that result in runtime crash, related: why are some literal expressions are not allowed?

**URL:** <https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500>\
**Category:** Using Swift\
**Created:** [January 10, 2023, 6:21pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500 "2023-01-10T18:21:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [January 10, 2023, 6:21pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/1 "2023-01-10T18:21:13Z")

</div>

```swift
let range = 10...1
print(range) // what does this print?

```

```swift
let foo = 1 << 9

print(foo) // what does this print?

```

```swift
enum Foo: Int {
  case bar = 1 << 9
}

print(Foo.bar) // what does this print?

```

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [January 10, 2023, 6:46pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/2 "2023-01-10T18:46:18Z")

</div>

> [@young](#):
>
> ```swift
> enum Foo: Int {
> case bar = 1 << 9
> }
> 
> print(Foo.bar) // what does this print?
> 
> ```

Nothing, because it doesn't compile, the case needs to be a compile time constant:

 ![image](https://global.discourse-cdn.com/swift/original/3X/c/a/ca24bf47ded9051c38916f1eb50de403d86a95d7.png)

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [January 10, 2023, 6:48pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/3 "2023-01-10T18:48:42Z")

</div>

I know. I'm asking why can't it be a compile time check for all these cases? Like dictionary literal duplicate key compile time check introduced recently to Swift?

And why this:

```swift
1 << 9

```

sometime compile time error, sometime not?

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [January 10, 2023, 6:50pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/4 "2023-01-10T18:50:20Z")

</div>

> [@young](#):
>
> I know. I'm asking why can't it be a compile time check? Like dictionary literal duplicate key compile time check introduced recently to Swift?

Ah, I misunderstood the question then, I don't know unfortunately, if I had to guess I'd say it's because parsing operators would've been a pain, though I'd really like to see what you mentioned implemented

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [January 10, 2023, 6:52pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/5 "2023-01-10T18:52:43Z")

</div>

> [@young](#):
>
> And why this:
> 
> ```swift
> 1 << 9
> 
> ```
> 
> sometime compile time error, sometime not?

The case you mentioned to be a compile time error was an enum case with a rawValue,  
however, in one of the other examples you showed that doesn't error out:

> [@young](#):
>
> ```swift
> let foo = 1 << 9
> 
> print(foo) // what does this print?
> 
> ```
> 
> ```swift
> 
> ```

This doesn't error out because 1 \<\< 9 is evaluated at runtime, not compile time, due to `foo` not needing to be known at compile time

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [January 10, 2023, 6:53pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/6 "2023-01-10T18:53:59Z")

</div>

> [@NSXPCConnection](#):
>
> I'd say it's because parsing operators would've been a pain

But Swift does compile `1<<9`. It’s just that the result is sometime compile time error like in enum/case definition. But other time, it compile and run. Though I don’t know when the final value is calculated I would guess is runtime.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [January 10, 2023, 6:56pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/7 "2023-01-10T18:56:01Z")

</div>

Yeah, I know. Some complain why this literally constant fully know at compile time expression is not fully evaluated at compile time and people are resorting to manually doing code gen.

Swift is not C: it does no lexical preprocessing. Although the recent Expression Macro proposal looks to change this and modernizes macro.

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [January 10, 2023, 6:59pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/8 "2023-01-10T18:59:16Z")

</div>

> [@young](#):
>
> But Swift does compile `1<<9`

Indeed it _compiles_, however it doesn't parse the operators to calculate the final value at compile time, as it stands right now, for anything needing to be known at compile time, Swift requires the value to be a literal, ie with `_const let`:

 ![Screenshot 2023-01-10 at 9.58.04 PM](https://global.discourse-cdn.com/swift/original/3X/a/5/a5c3aa061978330da1f75e6385a9a33064784634.png)

 ![image](https://global.discourse-cdn.com/swift/original/3X/b/0/b0af6aab04f2cc2f6d95b0450ae97e4c6792c517.png)

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [January 10, 2023, 7:00pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/9 "2023-01-10T19:00:45Z")

</div>

And will Expression Macro improve this so this is a compile time evaluation?

---

<div class="post-metadata">

**Author:** ![NSXPCConnection](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nsxpcconnection/32/31019_2.png) [@NSXPCConnection](https://forums.swift.org/u/NSXPCConnection)\
**Post date:** [January 10, 2023, 7:03pm UTC](https://forums.swift.org/t/why-the-swift-compiler-allow-literal-expression-that-result-in-runtime-crash-related-why-are-some-literal-expressions-are-not-allowed/62500/10 "2023-01-10T19:03:20Z")

</div>

Depends on the implementation, if it ends up being like C macros where it's just literal copy and paste done by a preprocessor then the answer is no, because what you'd see is just `case someCase = MACRO_STATEMENT`, but if `MACRO_STATEMENT` is `1 << 9`, then the only thing that the compiler would see is `case someCase = 1 << 9`, though I'm not familiar with how expression macros would(are going to?) be implemented
