Redeclaration of guard variable is ignored at top-level


(Martin R) #1

Hi,

I wonder why the Swift compiler does not complain about the
redeclaration of `number` after the guard-statement in top-level code:

    // main.swift
    import Swift

    guard let number = Int("1234") else { fatalError() }
    print(number) // Output: 1234
    let number = 5678
    print(number) // Output: 1234

It looks as if the statement `let number = 5678` is completely ignored.

However, doing the same inside a function causes a compiler error:

    func foo() {
        guard let number = Int("1234") else { fatalError() }
        print(number)
        let number = 5678 // error: definition conflicts with previous value
    }

Tested with
- Xcode 7.3.1, "Default" and "Snapshot 2016-06-06 (a)" toolchain
- Xcode 8 beta.

Am I overlooking something or is that a bug?

Martin


(Saagar Jha) #2

Looks like a bug…strangely, lldb’s giving number: Int = 5678.

···

On Thu, Jun 16, 2016 at 10:18 PM Martin R via swift-users < swift-users@swift.org> wrote:

Hi,

I wonder why the Swift compiler does not complain about the
redeclaration of `number` after the guard-statement in top-level code:

    // main.swift
    import Swift

    guard let number = Int("1234") else { fatalError() }
    print(number) // Output: 1234
    let number = 5678
    print(number) // Output: 1234

It looks as if the statement `let number = 5678` is completely ignored.

However, doing the same inside a function causes a compiler error:

    func foo() {
        guard let number = Int("1234") else { fatalError() }
        print(number)
        let number = 5678 // error: definition conflicts with previous
value
    }

Tested with
- Xcode 7.3.1, "Default" and "Snapshot 2016-06-06 (a)" toolchain
- Xcode 8 beta.

Am I overlooking something or is that a bug?

Martin
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha


(Mark Lacey) #3

Hi,

I wonder why the Swift compiler does not complain about the
redeclaration of `number` after the guard-statement in top-level code:

   // main.swift
   import Swift

   guard let number = Int("1234") else { fatalError() }
   print(number) // Output: 1234
   let number = 5678
   print(number) // Output: 1234

It looks as if the statement `let number = 5678` is completely ignored.

However, doing the same inside a function causes a compiler error:

   func foo() {
       guard let number = Int("1234") else { fatalError() }
       print(number)
       let number = 5678 // error: definition conflicts with previous value
   }

Tested with
- Xcode 7.3.1, "Default" and "Snapshot 2016-06-06 (a)" toolchain
- Xcode 8 beta.

Am I overlooking something or is that a bug?

Hi Martin,

Yes, this looks like a bug. Can you open a report at bugs.swift.org <http://bugs.swift.org/>?

Mark

···

On Jun 16, 2016, at 10:18 PM, Martin R via swift-users <swift-users@swift.org> wrote:

Martin
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Martin R) #4

Filed as https://bugs.swift.org/browse/SR-1804.

···

2016-06-17 7:17 GMT-07:00 Mark Lacey <mark.lacey@apple.com>:

On Jun 16, 2016, at 10:18 PM, Martin R via swift-users > <swift-users@swift.org> wrote:

Hi,

I wonder why the Swift compiler does not complain about the
redeclaration of `number` after the guard-statement in top-level code:

   // main.swift
   import Swift

   guard let number = Int("1234") else { fatalError() }
   print(number) // Output: 1234
   let number = 5678
   print(number) // Output: 1234

It looks as if the statement `let number = 5678` is completely ignored.

However, doing the same inside a function causes a compiler error:

   func foo() {
       guard let number = Int("1234") else { fatalError() }
       print(number)
       let number = 5678 // error: definition conflicts with previous value
   }

Tested with
- Xcode 7.3.1, "Default" and "Snapshot 2016-06-06 (a)" toolchain
- Xcode 8 beta.

Am I overlooking something or is that a bug?

Hi Martin,

Yes, this looks like a bug. Can you open a report at bugs.swift.org?

Mark

Martin
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #5

You should file a bug on the first sample.

Zhaoxin

···

On Fri, Jun 17, 2016 at 1:43 PM, Saagar Jha via swift-users < swift-users@swift.org> wrote:

Looks like a bug…strangely, lldb’s giving number: Int = 5678.

On Thu, Jun 16, 2016 at 10:18 PM Martin R via swift-users < > swift-users@swift.org> wrote:

Hi,

I wonder why the Swift compiler does not complain about the
redeclaration of `number` after the guard-statement in top-level code:

    // main.swift
    import Swift

    guard let number = Int("1234") else { fatalError() }
    print(number) // Output: 1234
    let number = 5678
    print(number) // Output: 1234

It looks as if the statement `let number = 5678` is completely ignored.

However, doing the same inside a function causes a compiler error:

    func foo() {
        guard let number = Int("1234") else { fatalError() }
        print(number)
        let number = 5678 // error: definition conflicts with previous
value
    }

Tested with
- Xcode 7.3.1, "Default" and "Snapshot 2016-06-06 (a)" toolchain
- Xcode 8 beta.

Am I overlooking something or is that a bug?

Martin
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users