Compiler inconsistent reporting between Xcode and GitHub CI/CD

Hi!

Today I ran into a problem where the code below would work fine within Xcode 12, but on GitHub my CI/CD action would fail with "error: variable used within its own initial value".

    let levels = levels(of: tree, fillWithVoidNodes: true)

The func levels(...) is defined in another file, so I assume that the compiler got confused by that and assumed that my let levels assignment is circular. Or, something along those lines.

I managed to resolve this by changing the assignment let levelStack = levels(...). However, it would be great to get some feedback on this.

  1. Am I wrong in declaring levels = levels(...)?
  2. Is there a way to reproduce the GitHub action's error in Xcode or do I need to try compiling on the command line?
  3. Are there recommendations for ensuring that code is portable between different Swift environments (only Apple platforms in my case)?

Thanks!

Kim

Sounds like you may be using different versions of Xcode locally and in CI. Easiest way to ensure you code works in both environments is to ensure you match versions.

1 Like

The compiler has been complaining about this for as long as I can remember, even in Xcode, so I have simply learned never to write that. However, I can think of no reason why the compiler couldn’t be improved to support it. Indeed, if you can build it with Xcode, then I guess that very improvement was made recently.

Instead of renaming the variable, I usually dodge the error by making the scope explicit: self.level(...), WhateverType.level(...), WhateverModule.level(...), etc.

If the two are behaving differently, then they are using different versions of Swift. Synchronizing the versions should erase any differences.

Test it in each environment. If there is also e.g. macOS vs Linux at play, then even using the same patch number cannot guarantee that any platform related bugs will be triggered in the opposite environment.

1 Like

Thanks for the detailed feedback and for providing your solution. I'm not too keen on explicitly declaring the scope, but it seems like a straightforward solution. I think I'll adopt that method for now too.

Terms of Service

Privacy Policy

Cookie Policy