SpyglassMC / Spyglass

Development tools for vanilla Minecraft: Java Edition data pack developers.
https://spyglassmc.com
MIT License
303 stars 31 forks source link

Handle recursive types for mcdoc isAssignable checking #1228

Open NeunEinser opened 4 months ago

NeunEinser commented 4 months ago

the isAssignable logic should be able to handle recursive types. This may occur when for example comparing these two mcdoc definitions:

type Text = ( string | PlainTextStruct | TranslatedTextStruct | [Text] )

struct BaseTextStruct {
    color: string,
    bold: boolean,
    italic: boolean,
    strikethrough: boolean,
    extra: [Text],
}

struct PlainTextStruct {
    ...BaseTextStruct,
    text: string,
}

struct TranslatedTextStruct {
    ...BaseTextStruct,
    translate: string,
    placeholder?: string
    with?: [Text]
}

and

type FormattedPlainText = ( string | FormattedTextStruct | [FormattedPlainText] )

struct FormattedTextStruct {
    text: string,
    color: string,
    bold: boolean,
    italic: boolean,
    extra: [FormattedPlainText]
}

The difference between the two is that one only allows plain text, while the other also allows for translatable texts. FormattedText is fully assignable to Text, but the current mcdoc runtime checker logic will recurse indefinitely into the reference Text types.

To fix this, we need to keep track of reference types and what they have been compared against so far to bail out in this case.

misode commented 3 months ago

Moving this to the backlog for now, because we won't be adding data modify assignability checks in v4.0