SwiftyLab / MetaCodable

Supercharge Swift's Codable implementations with macros meta-programming.
https://swiftpackageindex.com/SwiftyLab/MetaCodable/main/documentation/metacodable
MIT License
593 stars 20 forks source link

SwiftData class: Method does not override any method from its superclass #56

Open ashleydw opened 7 months ago

ashleydw commented 7 months ago

If a SwiftData model annotation is added to a class, the build will fail with "Method does not override any method from its superclass".

It can be reproduced using a clean SwiftData project, adding the SPM package from 1.2.1, and the following class:

import Foundation
import SwiftData
import MetaCodable
import HelperCoders

protocol MyProtocol {

}

@Codable
@Model
final class Item : MyProtocol {
    @CodedAt("timestamp")
    var timestamp: Date? = nil

    init() { }
}

Expanding the macro actually shows it doesn't add the super.* calls:

image

I have however managed to produce one expansion with the error:

image

soumyamahunt commented 7 months ago

If a SwiftData model annotation is added to a class, the build will fail with "Method does not override any method from its superclass".

It can be reproduced using a clean SwiftData project, adding the SPM package from 1.2.1, and the following class:

import Foundation
import SwiftData
import MetaCodable
import HelperCoders

protocol MyProtocol {

}

@Codable
@Model
final class Item : MyProtocol {
    @CodedAt("timestamp")
    var timestamp: Date? = nil

    init() { }
}

Are you sure you are able to reproduce this with this code snippet, for me this code snippet doesn't produce error. Can you provide a sample project for this?

I have however managed to produce one expansion with the error:

image

Can you provide sample project for this, I can think of one scenario where this error would come:

[!NOTE]
If you are manually conforming to Codable protocol in the class, please remove the conformance as @Codable macro itself generates the conformance.

Anyway I can't do much here with the screenshots, please provide a minimal reproducible sample.

ashleydw commented 7 months ago

Here's a repo: https://github.com/ashleydw/metacodable-min-ex/blob/main/test/Item.swift

If you build you should get the same error on Item.swift. Commenting out @Codable and CodedAt should build successfully and run.

soumyamahunt commented 7 months ago

Thanks @ashleydw for providing sample, I can reproduce your issue. The issue doesn't seem to be combining @Codable with SwiftData class, in fact the project builds successfully if you comment out following lines:

https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L6 https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L11-L18 https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L50 https://github.com/ashleydw/metacodable-min-ex/blob/c0bd383995ca3c12938f704b79429e2c9c7b0c9b/test/ContentView.swift#L58

Seems like with these line present Swift is not providing protocol conformance data to @Codable. It would be better if you create a discussion on Swift forum or Apple developer forum as the issue doesn't seem to be MetaCodable related.

Willian-Zhang commented 7 months ago

Same issue here,

problem magically disappear when #Preview section commented out of some random View (no @ State, no @Query no anything), other #Preview that contains a @Query not affected by this

Willian-Zhang commented 7 months ago

And it happens to me when I move @Bindable var someEnvObject = someEnvObject in scope of a body of a View that takes and EnvroementObject (check below for doc), to 1 ZStack level inside...

// @Bindable var someEnvObject = someEnvObject <- from here
ZStack {
  @Bindable var someEnvObject = someEnvObject <- to here
  // some other code using `someEnvObject`
}
image

(Apple doc)[https://developer.apple.com/documentation/swiftui/bindable]

soumyamahunt commented 7 months ago

@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?

I am afraid this issue is not related to MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.

Willian-Zhang commented 7 months ago

@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum?

I am afraid this issue is not related to MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.

I haven't, I wasn't able to identify where the problem lies to. The only thing I can be sure about is that it happens with this use case and reporting this exact error from @Codable generated code.

soumyamahunt commented 7 months ago

@ashleydw @Willian-Zhang has any of you created discussion on Swift forum or Apple developer forum? I am afraid this issue is not related to MetaCodanle, taking the discussion to Swift team/Apple is the only solution to this.

I haven't, I wasn't able to identify where the problem lies to. The only thing I can be sure about is that it happens with this use case and reporting this exact error from @Codable generated code.

Simple overview of the class implementation generation by @Codable:

-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`)
    -> Yes
        -> Are `Decodable`/`Encodable` conformance implemented i.e. `init(from:)`/`encode(to:)` method implementation exist?
            -> Yes
                -> Skip generating implementation
            -> No
                 -> Assume that super class conforms to `Decodable`/`Encodable` and provide generated implementation.
    -> No
        -> No super class conforms to `Decodable`/`Encodable` and provide generated implementation.

The strange thing here is without those lines the Swift compiler invokes the following flow:

-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`)
    -> No
        -> No super class conforms to `Decodable`/`Encodable` and provide generated implementation.

and with those lines present the Swift compiler invokes the following flow:

-> Does Class conform to `Decodable`/`Encodable`? (passed from Swift compiler to `MetaCodable`)
    -> Yes
        -> Are `Decodable`/`Encodable` conformance implemented i.e. `init(from:)`/`encode(to:)` method implementation exist?
            -> No
                 -> Assume that super class conforms to `Decodable`/`Encodable` and provide generated implementation.

which is a strange behaviour from compiler, as of why compiler provides wrong data in some cases, only Swift compiler team can answer.

Willian-Zhang commented 7 months ago

@soumyamahunt great insight! @soumyamahunt @ashleydw Are you available to fire a bug report or discussion as you said to apple by any chance? I don't think I'm qualified enough for this as I'm new to SwiftLang let alone any insight on how swift macro works.

If not, do you think there is way to go around with this problem? I am very interested in this cross usage on SwiftData+MetaCodable, as it would vastly simplify my code in a project that I started recently. (Actually I'm current on a track giving up on #Preview capability just to have this working)

soumyamahunt commented 5 months ago

@ashleydw @Willian-Zhang new macro Inherits has been added (only in main branch, not released yet) to workaround the issue until this is fixed from compiler.

Willian-Zhang commented 5 months ago

having Inherits tested, I can confirm this solved my problem perfectly.

rursache commented 5 months ago

not sure what i'm doing wrong. using the main branch and this code:

@Codable
@Model
@Inherits(decodable: false, encodable: false)
final class TestModel: Codable, Identifiable {
  var id: Int = 0
  var url: String = ""
  var name: String = ""

  init(id: Int, url: String, name: String) {
    self.id = id
    self.url = url
    self.name = name
  }
}

Fails with Redundant conformance of 'EpisodeModel' to protocol 'Decodable' Redundant conformance of 'EpisodeModel' to protocol 'Encodable'

EDIT: solved by removing the Codable, Identifiable conformance, my bad!

soumyamahunt commented 5 months ago

@rursache you have to remove the manual Codable conformance you are adding, due to this addition compiler is also generating Codable implementation which is conflicting with this library's generated implementation.

ashleydw commented 5 months ago

@soumyamahunt I can confirm it works in my project, thanks!

bfalcinelli commented 1 week ago

Please, release a new version with this fix