# Why #warning() not allowed in some source location?

**URL:** <https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643>\
**Category:** Using Swift\
**Created:** [August 23, 2020, 9:38pm UTC](https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643 "2020-08-23T21:38:38Z")\
**Posts on this page:** 6\
**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:** [August 23, 2020, 9:38pm UTC](https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643/1 "2020-08-23T21:38:38Z")

</div>

I use `#warning` when I file bug report so I need to pin point where the bug is, but it's sometime not allowed (compile error) in certain code location:

```swift
import SwiftUI

struct BugHereSomewhere: View {
    var body: some View {
        #warning("#warning here is allowed")
        VStack {
            Text("Hello")
        }
        #warning("#warning here is not allowed!")
        .ignoresSafeArea()
    }
}

```

why can't I put `#warning` anywhere I want?

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [August 23, 2020, 9:52pm UTC](https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643/4 "2020-08-23T21:52:30Z")

</div>

#warning doesn’t get preprocessed out the way it does in C. Unfortunately that means it breaks up expressions like this and makes it so the `.ignoresSafeArea()` doesn’t get applied to the VStack, it tries to get applied to the #warning, which isn’t an expression. This may be worth a bug on [bugs.swift.org](http://bugs.swift.org).

---

<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:** [August 23, 2020, 11:31pm UTC](https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643/5 "2020-08-23T23:31:26Z")

</div>

👌[https://bugs.swift.org/browse/SR-13436](https://bugs.swift.org/browse/SR-13436)

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [August 24, 2020, 12:15am UTC](https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643/6 "2020-08-24T00:15:08Z")

</div>

Looks like we don't allow this in general too (I was thinking it was specific to function builders...). For example:

```swift
func foo() {
    #warning("Works")
    "Hello, world!"
    #warning("Does not work")
    .description
}

```

leads to an error: `reference to member 'description' cannot be resolved without a contextual type`.

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [September 2, 2020, 8:21pm UTC](https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643/7 "2020-09-02T20:21:44Z")

</div>

> [@young](#):
>
> 👌[https://bugs.swift.org/browse/SR-13436](https://bugs.swift.org/browse/SR-13436)

FYI, I'll be returning this as "behaves correctly". Swift (intentionally) doesn't have the C-preprocessor-like behavior of letting # directives go anywhere: `#warning` is a declaration, so it cannot be in the middle of a postfix-expression chain.

Doug

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [September 2, 2020, 8:37pm UTC](https://forums.swift.org/t/why-warning-not-allowed-in-some-source-location/39643/8 "2020-09-02T20:37:50Z")

</div>

Could still be worth a diagnostic special case, though: if a line starts with an implicit member reference that can't be resolved, check what the previous statement was?
