Swift Boolean value <invalid>
Asked Answered
S

3

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

Sonority answered 15/12, 2016 at 20:45 Comment(10)
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)Honesty
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...Sonority
I suspect this code has been edited since the debugging session was started, and that the code doesn't reflect the state of the programHonesty
On a side note, this is could be so much more simply written as adFreeButton.isEnabled = !PurchaseState.isAdfreeHonesty
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.Sonority
Easy there, no need to get defensive lolHonesty
This is really strange behaviour. Could you try to write a minimal, complete, and verfiable example?Honesty
Why couldn't enabled be optimized away even if it's used? Can't it just get replaced, depending on the optimization level?Stoical
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.Sonority
I'll just put it in an answer thenStoical
G
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
Glossematics answered 10/5, 2017 at 10:34 Comment(0)
S
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.

Stoical answered 16/12, 2016 at 18:14 Comment(5)
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.Sonority
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.Stoical
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.Sonority
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.Stoical
I see. It was the debug config with -Onone.Sonority
F
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).

Freddiefreddy answered 14/9, 2017 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.