带泛型的类在Eclipse编译通过但GitHub Actions编译失败
GitHub Actions CI编译错误(Eclipse无报错):泛型类型参数边界不兼容
我在修复MultiMachineBuilder项目的GitHub Actions构建CI错误时遇到了问题:代码在Eclipse中可正常编译,但在GitHub Actions使用Maven编译时抛出泛型相关错误。
相关代码片段
ChestCore类
public abstract class ChestCore<Tinv extends SaveInventory> extends PartEntity implements BlockCore<ChestCore<Tinv>>{ ... }
CoreSlot类
public class CoreSlot<Tblock extends ModularBlock<Tblock, ?, Tcore, ?>, Tcore extends BlockCore<Tcore>> extends Slot<Tcore>{ ... }
ModularChest类
import mmb.engine.inv.SaveInventory; public final class ModularChest extends BlockEntityData implements ModularBlock<ModularChest, BlockModuleUniversal, ChestCore<? extends SaveInventory>, Object> { //Core @NN private static Class<ChestCore<? extends SaveInventory>> cls1 = MMBUtils.classcast(ChestCore.class); @NN private final CoreSlot<ModularChest, ChestCore<? extends SaveInventory>> core = new CoreSlot<>(this, cls1); @NN private final SidedSlotHelper<ModularChest, BlockModuleUniversal> ssh = new SidedSlotHelper<>(BlockModuleUniversal.class, this); }
错误日志
Error: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project MMB: Compilation failure: Compilation failure: Error: /home/runner/work/MultiMachineBuilder/MultiMachineBuilder/src/mmb/content/modular/chest/ModularChest.java:[31,59] type argument mmb.content.modular.chest.ChestCore<? extends mmb.engine.inv.SaveInventory> is not within bounds of type-variable Tcore Error: /home/runner/work/MultiMachineBuilder/MultiMachineBuilder/src/mmb/content/modular/chest/ModularChest.java:[50,59] type argument mmb.content.modular.chest.ChestCore<? extends mmb.engine.inv.SaveInventory> is not within bounds of type-variable Tcore Error: /home/runner/work/MultiMachineBuilder/MultiMachineBuilder/src/mmb/content/modular/chest/ModularChest.java:[50,93] cannot infer type arguments for mmb.content.modular.Slot.CoreSlot<> Error: reason: inference variable Tcore has incompatible equality constraints mmb.content.modular.chest.ChestCore<capture#1 of ? extends mmb.engine.inv.SaveInventory>,mmb.content.modular.chest.ChestCore<? extends mmb.engine.inv.SaveInventory> Error: /home/runner/work/MultiMachineBuilder/MultiMachineBuilder/src/mmb/content/stn/block/STNFramework.java:[38,59] type argument mmb.content.modular.chest.ChestCore<? extends mmb.engine.inv.SaveInventory> is not within bounds of type-variable Tcore Error: /home/runner/work/MultiMachineBuilder/MultiMachineBuilder/src/mmb/content/stn/block/STNFramework.java:[69,59] type argument mmb.content.modular.chest.ChestCore<? extends mmb.engine.inv.SaveInventory> is not within bounds of type-variable Tcore Error: /home/runner/work/MultiMachineBuilder/MultiMachineBuilder/src/mmb/content/stn/block/STNFramework.java:[69,93] cannot infer type arguments for mmb.content.modular.Slot.CoreSlot<> Error: reason: inference variable Tcore has incompatible equality constraints mmb.content.modular.chest.ChestCore<capture#2 of ? extends mmb.engine.inv.SaveInventory>,mmb.content.modular.chest.ChestCore<? extends mmb.engine.inv.SaveInventory>
问题分析
报错提示ChestCore<? extends SaveInventory>不符合Tcore的类型边界,但从泛型约束来看,ChestCore<Tinv>实现了BlockCore<ChestCore<Tinv>>,理论上应该满足Tcore extends BlockCore<Tcore>的要求。另外,报错中提到的ChestCore<capture#n of ? extends SaveInventory>和ChestCore<? extends SaveInventory>看起来兼容,但Maven使用的javac编译器无法识别这种兼容性,而Eclipse的ecj编译器可以正常处理。我已经尝试过调整泛型的约束写法(带约束/无约束),但错误依然存在。
内容的提问来源于stack exchange,提问作者Monniasza
相关产品推荐
相关产品推荐

