11

The attached screen shot says it all: I have a valid true Bool, I negate it with the bang operator, and (Xcode tells me) I have an invalid value.

It appears that this "invalid" value does behave as if it were false. But really, wtf?

enter image description here

Andrew Duncan
  • 2,972
  • 2
  • 21
  • 44
  • 1
    The memory for that bool either was never initialized, or has been corrupted in some way. The only valid values for `Bool` are `0x01` (`true`) and `0x00` (`false`) – Alexander Dec 15 '16 at 20:51
  • The Xcode debug window shows clearly that the bool was initialized. That's what line 72 does. Moreover, the `enabled` value can't be optimized out since it is used on line 73. However, this could just be an Xcode debugger bug... – Andrew Duncan Dec 15 '16 at 20:59
  • I suspect this code has been edited since the debugging session was started, and that the code doesn't reflect the state of the program – Alexander Dec 15 '16 at 21:00
  • 1
    On a side note, this is could be so much more simply written as `adFreeButton.isEnabled = !PurchaseState.isAdfree` – Alexander Dec 15 '16 at 21:01
  • The code was not edited since the debugging session was started. And yes, I did originally have the code as you suggested. I rewrote it in this way so that you could see clearly that !true did not equal false. I've been writing code now for 40 years; I do understand booleans. – Andrew Duncan Dec 15 '16 at 21:14
  • 1
    Easy there, no need to get defensive lol – Alexander Dec 15 '16 at 21:29
  • This is really strange behaviour. Could you try to write a [minimal, complete, and verfiable example?](http://stackoverflow.com/help/mcve) – Alexander Dec 15 '16 at 21:30
  • Why couldn't `enabled` be optimized away even if it's used? Can't it just get replaced, depending on the optimization level? – Andreas Dec 15 '16 at 21:35
  • Andreas' comment could be the answer. FWIW the most minimal example, i.e. in a Playground, does not exhibit the behavior. I will see about trying to create an example, but as per Andreas, this might be very context-sensitive. – Andrew Duncan Dec 15 '16 at 22:29
  • I'll just put it in an answer then – Andreas Dec 16 '16 at 18:15

3 Answers3

1

I've had this issue in Xcode 8.3.1 and Swift 3.1 https://github.com/onmyway133/notes/issues/278

I tried

  • Clean build folder and delete derived data folder
  • Delete the app
  • Reset simulator
  • Restart Xcode
  • Restart Mac

But does not work. The workaround is to

let enabled = disable ? false : true
onmyway133
  • 38,911
  • 23
  • 231
  • 237
0

I'm no LLVM expert but I wouldn't be surprised about this behavior at all, unless optimization is set to Onone in which case it should have left your code alone. The intermediate variable is just asking to be optimized away, after all.

Andreas
  • 2,486
  • 1
  • 26
  • 35
  • However (and i should have specified this) both enabled and disabled are used several times later in the method. (This is part of why I have *both* enabled and disabled defined: the code reads better and it is harder to inadvertently omit a bang negation symbol.) In fact, the debugger is paused at precisely one of the places where the (apparently bogus) value is being used. Finally, in debug mode, my optimize setting *is* -Onone. – Andrew Duncan Dec 16 '16 at 20:31
  • I also declare variables liberally, but no matter how much I use them I still expect them to disappear and get replaced by something much better. If your screenshot is in fact from a debug run, which you are implying, then I'm afraid you'll have to wait for someone more knowledgeable. – Andreas Dec 16 '16 at 20:51
  • I agree that the compiler is free to rearrange local variables if provably correct. (I'm a compiler writer. You'll sympathize with the number of times I have had to explain to students that "No, fewer locals does not mean more efficient code.") But yes the screenshot is of the Xcode debugger. Could be a harmless bug in the Swift runtime. Harmless because 0xfe (aka -2) was indeed handled as false by the control flow. – Andrew Duncan Dec 16 '16 at 21:17
  • I think you misunderstand what I mean by debug run — it's not a question of whether the screenshot is of the debugger, but rather which build configuration is used when running from Xcode. `debug` config (with `Onone`) is default, but not necessary. – Andreas Dec 16 '16 at 21:25
  • I see. It was the debug config with -Onone. – Andrew Duncan Dec 17 '16 at 04:36
0

Got the same issue, with correct value for add code like print(theBoolValue).

But when use p in swift command line. Or just check the value in debug stack, the value become <invalid>(Oxfe).

JerryZhou
  • 3,359
  • 3
  • 27
  • 44