krzysztofzablocki / Sourcery

Meta-programming for Swift, stop writing boilerplate code.
http://merowing.info
MIT License
7.65k stars 616 forks source link

Generated code doesn't compile if returning type starts with the "any" keyword #1310

Closed mol-ideals closed 6 months ago

mol-ideals commented 6 months ago

// Given

// sourcery: AutoMockable
protocol Repository {
  func loadData() -> any Publisher<String, Error>
}

// Generated code:

class RepositoryMock: Repository {
  ...
  var loadDataReturnValue: any Publisher<String, Error>!
  func loadData() -> any Publisher<String, Error> {
  ...
  }
}

This line var loadDataReturnValue: any Publisher<String, Error>! doesn't compile. The type needs to be placed inside the round brackets. This does compile - var loadDataReturnValue: (any Publisher<String, Error>)!

art-divin commented 6 months ago

Thank you very much for reporting @mol-ideals 👍🏻

Will be fixed for 2.1.9 release.

art-divin commented 6 months ago

Hello @mol-ideals ,

it seems like you are using an older version of Sourcery. Since a couple of versions - specifically, since 2.0.3 release, generated code:

  1. supports any (mostly thanks to @paul1893 's work ❤️)
  2. and since 2.1.8, Sourcery supports CVarArg in methods, and so, the naming of generated methods and variables was changed and now includes types, i.e. in 2.1.8 variable loadDataReturnValue is named loadDataAnyPublisherStringErrorReturnValue and indeed, return value is (any Publisher<String, Error>)!

Feel free to try using a later version of Sourcery and re-open this issue, in case the issue would persist.

mol-ideals commented 6 months ago

@art-divin That's very strange. I was using 2.1.8 to reproduce it. At least I think so.

Screenshot 2024-03-28 at 21 49 47

The title says it's generated via 2.1.8. For clarity I've tried reinstalling sorcery but it didn't change anything

Screenshot 2024-03-28 at 21 54 12

If 2.1.8 generates a different output for you the only theory I have is that somehow my older version is still alive.. but I can't see anywhere. I installed sorcery via Homebrew if it matters

art-divin commented 6 months ago

@mol-ideals ,

there's nothing strange in particular, but it seems like the stencil file which is used in your setup is from an older version of Sourcery. Please check that one file, and update it. The newer version of this file is located under Templates/AutoMockable.stencil in the distributed release archive.

mol-ideals commented 6 months ago

Thank you @art-divin It worked!