infer enum types
This commit is contained in:
@@ -533,9 +533,21 @@ public class TypeChecker
|
||||
|
||||
private TypedNodeExpressionEnumLiteral CheckExpressionEnumLiteral(NodeExpressionEnumLiteral expression, NubType? expectedType)
|
||||
{
|
||||
// todo(nub31): Infer type of enum variant
|
||||
var type = NubTypeEnumVariant.Get(NubTypeEnum.Get(expression.Module.Ident, expression.EnumName.Ident), expression.VariantName.Ident);
|
||||
var value = CheckExpression(expression.Value, null);
|
||||
var type = ResolveType(expression.Type);
|
||||
if (type is not NubTypeEnumVariant variantType)
|
||||
throw BasicError("Expected enum variant type", expression.Type);
|
||||
|
||||
if (!moduleGraph.TryResolveType(variantType.EnumType.Module, variantType.EnumType.Name, variantType.EnumType.Module == currentModule, out var info))
|
||||
throw BasicError($"Type '{variantType.EnumType}' not found", expression.Type);
|
||||
|
||||
if (info is not Module.TypeInfoEnum enumInfo)
|
||||
throw BasicError($"Type '{variantType.EnumType}' is not an enum", expression.Type);
|
||||
|
||||
var variant = enumInfo.Variants.FirstOrDefault(x => x.Name == variantType.Variant);
|
||||
if (variant == null)
|
||||
throw BasicError($"Enum '{variantType.EnumType}' does not have a variant named '{variantType.Variant}'", expression.Type);
|
||||
|
||||
var value = CheckExpression(expression.Value, variant.Type);
|
||||
return new TypedNodeExpressionEnumLiteral(expression.Tokens, type, value);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user