Back to Baml

compiler-errors

beps/docs/proposals/BEP-006-compiler-errors/README.md

0.222.013.0 KB
Original Source

BEP-006: compiler-errors

Summary

Uniform user-facing experience for compiler errors.

  • Enumerate all errors
  • Give them unique 4-digit codes
  • Specify how each is rendered as a diagnostic message
  • Specify how each is turned into guidance

Motivation

We want to give users and coding agents a uniform experience when they encounter compiler errors.

Proposed Design

  • A top-level compiler_error module contains every error category.
    pub enum CompilerError<Ty> {
      ParseError(ParseError)
      TypeError(TypeError<Ty>)
    }
    
    (TypeError is parameterized because it's defined here, it needs to contain types, types are defined in another crate that depends on errors, and using types directly would lead to a circular crate dependency).
  • compiler_error has submodules errors pertinent to specific phases. For example type errors are defined in compiler_error::type_error::TypeError.
    pub enum TypeError<T> {
      TypeMismatch { expected: T, found: T, span: Span },
      UnknownType { name: String, span: Span }.
      ...
    }
    
  • compiler_error::error_format contains error_report_and_code(), which does a giant match over every error variant and specifies how that variant becomes an ariadne report builder and an error code. render_error() takes these two pieces of data and combines them into an ariadne Report.

This design is implemented and merged already, see PR 27651.

All the errors

The following is a list of every possible error. It is derived from both the new compiler (which follows the new scheme), and the legacy compiler (which has a more exhaustive set of errors due to its longer time in development).

Phase: compiler phase (Lexing, Parsing, NameResolution, Typing, Codegen) Error: very brief description in the form of an Enum Variant

CodePhaseErrorSpansNotes
E0001TypingTypeMismatch(1) error location, (2) type constraint originExpected X, found Y
E0002TypingUnknownTypetype reference spanType name not found in scope
E0003TypingUnknownVariablevariable reference spanVariable name not found in scope
E0004TypingInvalidOperatoroperator spanBinary or unary op invalid for types
E0005TypingArgumentCountMismatch(1) call site, (2) function definitionWrong number of args to function
E0006TypingNotCallablecall site spanCalling a non-function type
E0007TypingNoSuchField(1) field access span, (2) type definitionField doesn't exist on type
E0008TypingNotIndexableindex access spanType doesn't support indexing
E0009ParsingUnexpectedEofEOF location spanUnexpected end of file
E0010ParsingUnexpectedTokentoken spanExpected X, found Y
E0011NameResolutionDuplicateName(1) second definition, (2) first definitionSame name defined twice
E0012ParsingLiteralParserErrorliteral spanInvalid literal value
E0013ValidationArgumentNotFoundargument usage spanRequired argument missing
E0014ValidationAttributeArgumentNotFoundattribute spanAttribute missing required arg
E0015ValidationGeneratorArgumentNotFoundgenerator block spanGenerator missing required arg
E0016ValidationAttributeValidationErrorattribute spanAttribute parsing failed
E0017ValidationDuplicateAttributeattribute spanAttribute defined multiple times
E0018TypingIncompatibleNativeTypenative type annotation spanNative type incompatible
E0019TypingInvalidNativeTypeArgumentnative type argument spanInvalid arg for native type
E0020TypingInvalidNativeTypePrefixnative type prefix spanWrong prefix for native type
E0021ValidationNativeTypesNotSupportednative type spanConnector doesn't support native types
E0022TypingReservedScalarTypetype name spanUsing reserved type name
E0023NameResolutionDuplicateEnumDatabaseNameenum spanDuplicate DB name for enum
E0024NameResolutionDuplicateModelDatabaseName(1) new model span, (2) existing model spanDuplicate DB name for model
E0025NameResolutionDuplicateViewDatabaseName(1) new view span, (2) existing view spanDuplicate DB name for view
E0026NameResolutionDuplicateTest(1) test span, (2+) other test spansTest name already defined
E0027NameResolutionDuplicateTopLevel(1) new definition, (2+) existing definitionsTop-level name collision
E0028NameResolutionDuplicateConfigKeyconfig key spanKey already defined in config
E0029ValidationDuplicateArgumentargument spanArgument specified twice
E0030ValidationUnusedArgumentargument spanNo such argument exists
E0031ValidationDuplicateDefaultArgumentargument spanDefault arg already specified
E0032NameResolutionDuplicateFunctionfunction spanFunction already defined
E0033ParsingInvalidFunctionSyntaxfunction spanMalformed function definition
E0034NameResolutionDuplicateEnumValue(1) new value span, (2) existing value spanEnum value already defined
E0035NameResolutionDuplicateCompositeTypeField(1) new field span, (2) existing field spanField already on composite type
E0036NameResolutionDuplicateField(1) new field span, (2) existing field spanField already on model/class
E0037ValidationScalarListFieldsNotSupportedfield spanConnector doesn't support scalar lists
E0038ValidationModelValidationErrormodel spanGeneric model validation failure
E0039ValidationNameErrorname spanInvalid identifier name
E0040ValidationEnumValidationErrorenum spanGeneric enum validation failure
E0041ValidationCompositeTypeFieldValidationfield spanComposite type field validation
E0042ValidationFieldValidationErrorfield spanGeneric field validation failure
E0043ValidationSourceValidationErrordatasource spanDatasource validation failure
E0044ValidationDynamicTypeNotAllowed@dynamic attribute span@dynamic not allowed in type_builder
E0045ValidationValidationErrorrelevant spanGeneric validation error
E0046ParsingLegacyParserErrortoken spanLegacy parser catch-all
E0047TypingOptionalArgumentCountMismatchnative type spanWrong optional arg count
E0048ParsingParserErrortoken spanExpected one of: X, Y, Z
E0049TypingFunctionalEvaluationErrorexpression spanError evaluating expression
E0050NameResolutionNotFoundErrorreference spanGeneric not-found with suggestions
E0051TypingTypeNotUsedInPrompttype reference spanType not in function output
E0052NameResolutionClientNotFoundclient reference spanClient name not found
E0053NameResolutionTypeNotFoundtype reference spanType name not found
E0054ValidationAttributeNotKnownattribute spanUnknown attribute name
E0055ValidationPropertyNotKnownproperty spanUnknown property in block
E0056ValidationArgumentNotKnownargument spanUnknown argument name
E0057TypingValueParserErrorvalue spanExpected type X, found Y
E0058TypingTypeMismatchLegacyvalue spanLegacy type mismatch error
E0059ValidationMissingRequiredPropertyblock spanRequired property missing
E0060ValidationConfigPropertyMissingValueproperty spanProperty needs a value
E0061TypingTypeNotAllowedAsMapKeymap key spanInvalid map key type
E0062TypingNonExhaustiveMatchmatch expression spanMatch does not cover all cases
E0063TypingUnreachableMatchArmmatch arm spanMatch arm masked by previous patterns

Appendix: Runtime Errors (VM)

These errors occur during execution, not compilation. They are out of scope for this BEP.

CodeCategoryErrorNotes
R0001RuntimeStackOverflowCall stack exceeded
R0002RuntimeAssertionErrorUser assertion failed
R0003RuntimeNoSuchKeyInMapMap key not found
R0004RuntimeDivisionByZeroDivision by zero
R0005InternalInvalidArgumentCountWrong arg count (VM bug)
R0006InternalUnexpectedEmptyStackStack underflow (VM bug)
R0007InternalNotEnoughItemsOnStackStack depth error (VM bug)
R0008InternalInvalidObjectRefBad object reference (VM bug)
R0009InternalTypeErrorVM type error (VM bug)
R0010InternalCannotApplyBinOpInvalid binary op (VM bug)
R0011InternalCannotApplyCmpOpInvalid comparison (VM bug)
R0012InternalCannotApplyUnaryOpInvalid unary op (VM bug)
R0013RuntimeArrayIndexOutOfBoundsArray index too large
R0014RuntimeArrayIndexIsNegativeNegative array index
R0015InternalNegativeInstructionPtrBad instruction ptr (VM bug)