1#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
5pub enum Severity {
6 Error,
8 Warning,
10}
11
12#[non_exhaustive]
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum DiagCode {
16 UnexpectedToken,
18 UnterminatedString,
20 InvalidEscape,
22 InvalidNumber,
24 FullWidthSpace,
26 UnknownName,
28 UnknownCommand,
30 UnknownLabel,
32 UnknownType,
34 TypeMismatch,
36 ConditionNotBool,
38 ArgCount,
40 UnknownArgument,
42 DuplicateArgument,
44 MissingArgument,
46 ArgumentOutOfOrder,
48 PositionalAfterNamed,
50 RequiredAfterDefault,
52 DuplicateName,
54 DuplicateStableId,
56 InvalidStableId,
58 UnboundSugar,
60 UnterminatedBlock,
62 UnexpectedClose,
64 ElseWithoutIf,
66 StatementOutsideLabel,
68 NotConstant,
70 NotYetSupported,
73 TooDeep,
75 UnexpectedEol,
77 BlockTooDeep,
79 TooManyRegisters,
81 InvalidOperation,
84 DeclarationNotAllowed,
86 OutsideLoop,
88 ShadowedBuiltin,
90 ImportNotFirst,
92 UnknownModule,
94 ModuleNotImported,
96 DuplicateModule,
98 InvalidModuleName,
100 PackageAlias,
102 DuplicateImport,
104 UnusedImport,
106 UnusedVariable,
108 JumpToCallableLabel,
110 JumpInReturningLabel,
113 CallInExpression,
116 CallWithoutValue,
118 MissingReturn,
120 MissingReturnValue,
122 UnexpectedReturnValue,
124 LabelFallsThrough,
126 UnterminatedQuote,
128 UnclosedBracket,
130 TextAfterQuote,
132 OnceWithoutId,
134 EmptyChoice,
136 SchemaNameOutsideMigrations,
138 InvalidSchemaFile,
140 SchemaHashMismatch,
142 DuplicateMigrate,
144 InvalidMigrateVersion,
146 InvalidMinSaveVersion,
148 UnknownSchemaName,
150 MigrateKindMismatch,
152 MigrateTypeMismatch,
154 DuplicateMigrateTarget,
156 MigrateImpureExpr,
158 MissingFrozenSchema,
160 MissingMigrate,
162 UnhandledTypeChange,
164 SchemaChangedSinceFreeze,
166 AmbiguousIdentity,
169 IdentityTypo,
172 UnknownPosition,
174 UnknownLabelLiteral,
176 InvalidCommandUse,
181 AssignToReadOnly,
183 NonExhaustiveMatch,
186 UnreachablePattern,
188 UnknownField,
191 UnknownVariant,
194 MissingField,
196 DuplicateField,
198 RecursiveType,
200 CannotInferType,
203 UnknownMethod,
205 StatementMethodInExpression,
208 UnsupportedElementType,
212 InvalidMapKey,
215 NotAllowedInFn,
219 AssignInFn,
222 UnknownFunction,
225 MissingTextId,
228 DeprecatedCommandName,
231 FnNotSaveable,
235 ReplNotAllowed,
238}
239
240impl DiagCode {
241 pub const ALL: &'static [DiagCode] = &[
243 DiagCode::UnexpectedToken,
244 DiagCode::UnterminatedString,
245 DiagCode::InvalidEscape,
246 DiagCode::InvalidNumber,
247 DiagCode::FullWidthSpace,
248 DiagCode::UnknownName,
249 DiagCode::UnknownCommand,
250 DiagCode::UnknownLabel,
251 DiagCode::UnknownType,
252 DiagCode::TypeMismatch,
253 DiagCode::ConditionNotBool,
254 DiagCode::ArgCount,
255 DiagCode::UnknownArgument,
256 DiagCode::DuplicateArgument,
257 DiagCode::MissingArgument,
258 DiagCode::ArgumentOutOfOrder,
259 DiagCode::PositionalAfterNamed,
260 DiagCode::RequiredAfterDefault,
261 DiagCode::DuplicateName,
262 DiagCode::DuplicateStableId,
263 DiagCode::InvalidStableId,
264 DiagCode::UnboundSugar,
265 DiagCode::UnterminatedBlock,
266 DiagCode::UnexpectedClose,
267 DiagCode::ElseWithoutIf,
268 DiagCode::StatementOutsideLabel,
269 DiagCode::NotConstant,
270 DiagCode::NotYetSupported,
271 DiagCode::TooDeep,
272 DiagCode::UnexpectedEol,
273 DiagCode::BlockTooDeep,
274 DiagCode::TooManyRegisters,
275 DiagCode::InvalidOperation,
276 DiagCode::DeclarationNotAllowed,
277 DiagCode::OutsideLoop,
278 DiagCode::ShadowedBuiltin,
279 DiagCode::ImportNotFirst,
280 DiagCode::UnknownModule,
281 DiagCode::ModuleNotImported,
282 DiagCode::DuplicateModule,
283 DiagCode::InvalidModuleName,
284 DiagCode::PackageAlias,
285 DiagCode::DuplicateImport,
286 DiagCode::UnusedImport,
287 DiagCode::UnusedVariable,
288 DiagCode::JumpToCallableLabel,
289 DiagCode::JumpInReturningLabel,
290 DiagCode::CallInExpression,
291 DiagCode::CallWithoutValue,
292 DiagCode::MissingReturn,
293 DiagCode::MissingReturnValue,
294 DiagCode::UnexpectedReturnValue,
295 DiagCode::LabelFallsThrough,
296 DiagCode::UnterminatedQuote,
297 DiagCode::UnclosedBracket,
298 DiagCode::TextAfterQuote,
299 DiagCode::OnceWithoutId,
300 DiagCode::EmptyChoice,
301 DiagCode::SchemaNameOutsideMigrations,
302 DiagCode::InvalidSchemaFile,
303 DiagCode::SchemaHashMismatch,
304 DiagCode::DuplicateMigrate,
305 DiagCode::InvalidMigrateVersion,
306 DiagCode::InvalidMinSaveVersion,
307 DiagCode::UnknownSchemaName,
308 DiagCode::MigrateKindMismatch,
309 DiagCode::MigrateTypeMismatch,
310 DiagCode::DuplicateMigrateTarget,
311 DiagCode::MigrateImpureExpr,
312 DiagCode::MissingFrozenSchema,
313 DiagCode::MissingMigrate,
314 DiagCode::UnhandledTypeChange,
315 DiagCode::SchemaChangedSinceFreeze,
316 DiagCode::AmbiguousIdentity,
317 DiagCode::IdentityTypo,
318 DiagCode::UnknownPosition,
319 DiagCode::UnknownLabelLiteral,
320 DiagCode::InvalidCommandUse,
321 DiagCode::AssignToReadOnly,
322 DiagCode::NonExhaustiveMatch,
323 DiagCode::UnreachablePattern,
324 DiagCode::UnknownField,
325 DiagCode::UnknownVariant,
326 DiagCode::MissingField,
327 DiagCode::DuplicateField,
328 DiagCode::RecursiveType,
329 DiagCode::CannotInferType,
330 DiagCode::UnknownMethod,
331 DiagCode::StatementMethodInExpression,
332 DiagCode::UnsupportedElementType,
333 DiagCode::InvalidMapKey,
334 DiagCode::NotAllowedInFn,
335 DiagCode::AssignInFn,
336 DiagCode::UnknownFunction,
337 DiagCode::MissingTextId,
338 DiagCode::DeprecatedCommandName,
339 DiagCode::ReplNotAllowed,
340 DiagCode::FnNotSaveable,
341 ];
342
343 pub fn severity(self) -> Severity {
345 match self {
346 DiagCode::IdentityTypo
347 | DiagCode::UnknownPosition
348 | DiagCode::UnknownLabelLiteral
349 | DiagCode::UnreachablePattern
350 | DiagCode::ShadowedBuiltin
351 | DiagCode::LabelFallsThrough
352 | DiagCode::UnusedImport
353 | DiagCode::UnusedVariable
354 | DiagCode::UnhandledTypeChange
355 | DiagCode::SchemaChangedSinceFreeze
356 | DiagCode::DeprecatedCommandName => Severity::Warning,
357 _ => Severity::Error,
358 }
359 }
360
361 pub fn arg_names(self) -> &'static [&'static str] {
364 match self {
365 DiagCode::UnexpectedToken | DiagCode::InvalidEscape => &["found"],
366 DiagCode::InvalidNumber => &["text"],
367 DiagCode::UnknownName
368 | DiagCode::UnknownFunction
369 | DiagCode::UnknownCommand
370 | DiagCode::UnknownLabel
371 | DiagCode::UnknownModule => &["name", "suggestions"],
372 DiagCode::UnknownField | DiagCode::UnknownVariant | DiagCode::UnknownMethod => {
373 &["name", "type", "suggestions"]
374 }
375 DiagCode::IdentityTypo => &["name", "suggestions"],
376 DiagCode::UnknownType
377 | DiagCode::DuplicateName
378 | DiagCode::DuplicateField
379 | DiagCode::RecursiveType
380 | DiagCode::StatementMethodInExpression
381 | DiagCode::ModuleNotImported
382 | DiagCode::DuplicateModule
383 | DiagCode::InvalidModuleName
384 | DiagCode::PackageAlias
385 | DiagCode::DuplicateImport
386 | DiagCode::UnusedImport
387 | DiagCode::UnusedVariable
388 | DiagCode::JumpToCallableLabel
389 | DiagCode::JumpInReturningLabel
390 | DiagCode::CallWithoutValue
391 | DiagCode::MissingReturn
392 | DiagCode::AssignToReadOnly
393 | DiagCode::AssignInFn
394 | DiagCode::ShadowedBuiltin
395 | DiagCode::LabelFallsThrough => &["name"],
396 DiagCode::SchemaNameOutsideMigrations
397 | DiagCode::MigrateKindMismatch
398 | DiagCode::DuplicateMigrateTarget => &["name"],
399 DiagCode::InvalidSchemaFile => &["path"],
400 DiagCode::SchemaHashMismatch
401 | DiagCode::DuplicateMigrate
402 | DiagCode::InvalidMigrateVersion
403 | DiagCode::InvalidMinSaveVersion
404 | DiagCode::MissingFrozenSchema
405 | DiagCode::MissingMigrate
406 | DiagCode::SchemaChangedSinceFreeze => &["version"],
407 DiagCode::UnknownSchemaName => &["name", "version", "suggestions"],
408 DiagCode::MigrateTypeMismatch => &["name", "expected", "found"],
409 DiagCode::MigrateImpureExpr => &["what"],
410 DiagCode::UnclosedBracket => &["open"],
411 DiagCode::UnhandledTypeChange => &["name", "version"],
412 DiagCode::MissingField => &["name", "type"],
413 DiagCode::UnsupportedElementType => &["method", "type"],
414 DiagCode::NonExhaustiveMatch => &["missing"],
415 DiagCode::TypeMismatch => &["expected", "found"],
416 DiagCode::ConditionNotBool => &["found"],
417 DiagCode::ArgCount => &["name", "expected", "found"],
418 DiagCode::UnknownArgument
419 | DiagCode::DuplicateArgument
420 | DiagCode::MissingArgument
421 | DiagCode::ArgumentOutOfOrder
422 | DiagCode::RequiredAfterDefault => &["name", "arg"],
423 DiagCode::PositionalAfterNamed => &["name"],
424 DiagCode::InvalidOperation => &["op", "type"],
425 DiagCode::DuplicateStableId | DiagCode::InvalidStableId => &["id"],
426 DiagCode::UnboundSugar | DiagCode::MissingTextId => &["kind"],
427 DiagCode::InvalidCommandUse => &["name", "kind"],
428 DiagCode::DeclarationNotAllowed
429 | DiagCode::OutsideLoop
430 | DiagCode::NotAllowedInFn
431 | DiagCode::ReplNotAllowed
432 | DiagCode::NotYetSupported => &["what"],
433 DiagCode::InvalidMapKey => &["type"],
434 DiagCode::FnNotSaveable => &["type", "what"],
435 DiagCode::TooDeep | DiagCode::BlockTooDeep | DiagCode::TooManyRegisters => &["limit"],
436 DiagCode::MissingReturnValue => &["expected"],
437 DiagCode::AmbiguousIdentity => &["name", "candidates"],
438 DiagCode::UnknownPosition => &["position"],
439 DiagCode::UnknownLabelLiteral => &["label"],
440 DiagCode::DeprecatedCommandName => &["name", "new"],
441 DiagCode::UnterminatedString
442 | DiagCode::FullWidthSpace
443 | DiagCode::UnterminatedBlock
444 | DiagCode::UnexpectedClose
445 | DiagCode::ElseWithoutIf
446 | DiagCode::StatementOutsideLabel
447 | DiagCode::NotConstant
448 | DiagCode::UnexpectedEol
449 | DiagCode::ImportNotFirst
450 | DiagCode::CallInExpression
451 | DiagCode::UnexpectedReturnValue
452 | DiagCode::UnterminatedQuote
453 | DiagCode::TextAfterQuote
454 | DiagCode::OnceWithoutId
455 | DiagCode::EmptyChoice
456 | DiagCode::UnreachablePattern
457 | DiagCode::CannotInferType => &[],
458 }
459 }
460
461 pub fn as_str(self) -> &'static str {
463 match self {
464 DiagCode::UnexpectedToken => "unexpected_token",
465 DiagCode::UnterminatedString => "unterminated_string",
466 DiagCode::InvalidEscape => "invalid_escape",
467 DiagCode::InvalidNumber => "invalid_number",
468 DiagCode::FullWidthSpace => "full_width_space",
469 DiagCode::UnknownName => "unknown_name",
470 DiagCode::UnknownCommand => "unknown_command",
471 DiagCode::UnknownLabel => "unknown_label",
472 DiagCode::UnknownType => "unknown_type",
473 DiagCode::TypeMismatch => "type_mismatch",
474 DiagCode::ConditionNotBool => "condition_not_bool",
475 DiagCode::ArgCount => "arg_count",
476 DiagCode::UnknownArgument => "unknown_argument",
477 DiagCode::DuplicateArgument => "duplicate_argument",
478 DiagCode::MissingArgument => "missing_argument",
479 DiagCode::ArgumentOutOfOrder => "argument_out_of_order",
480 DiagCode::PositionalAfterNamed => "positional_after_named",
481 DiagCode::RequiredAfterDefault => "required_after_default",
482 DiagCode::DuplicateName => "duplicate_name",
483 DiagCode::DuplicateStableId => "duplicate_stable_id",
484 DiagCode::InvalidStableId => "invalid_stable_id",
485 DiagCode::UnboundSugar => "unbound_sugar",
486 DiagCode::UnterminatedBlock => "unterminated_block",
487 DiagCode::UnexpectedClose => "unexpected_close",
488 DiagCode::ElseWithoutIf => "else_without_if",
489 DiagCode::StatementOutsideLabel => "statement_outside_label",
490 DiagCode::NotConstant => "not_constant",
491 DiagCode::NotYetSupported => "not_yet_supported",
492 DiagCode::TooDeep => "too_deep",
493 DiagCode::UnexpectedEol => "unexpected_eol",
494 DiagCode::BlockTooDeep => "block_too_deep",
495 DiagCode::TooManyRegisters => "too_many_registers",
496 DiagCode::InvalidOperation => "invalid_operation",
497 DiagCode::DeclarationNotAllowed => "declaration_not_allowed",
498 DiagCode::OutsideLoop => "outside_loop",
499 DiagCode::ShadowedBuiltin => "shadowed_builtin",
500 DiagCode::ImportNotFirst => "import_not_first",
501 DiagCode::UnknownModule => "unknown_module",
502 DiagCode::ModuleNotImported => "module_not_imported",
503 DiagCode::DuplicateModule => "duplicate_module",
504 DiagCode::InvalidModuleName => "invalid_module_name",
505 DiagCode::PackageAlias => "package_alias",
506 DiagCode::DuplicateImport => "duplicate_import",
507 DiagCode::UnusedImport => "unused_import",
508 DiagCode::UnusedVariable => "unused_variable",
509 DiagCode::JumpToCallableLabel => "jump_to_callable_label",
510 DiagCode::JumpInReturningLabel => "jump_in_returning_label",
511 DiagCode::CallInExpression => "call_in_expression",
512 DiagCode::CallWithoutValue => "call_without_value",
513 DiagCode::MissingReturn => "missing_return",
514 DiagCode::MissingReturnValue => "missing_return_value",
515 DiagCode::UnexpectedReturnValue => "unexpected_return_value",
516 DiagCode::LabelFallsThrough => "label_falls_through",
517 DiagCode::UnterminatedQuote => "unterminated_quote",
518 DiagCode::UnclosedBracket => "unclosed_bracket",
519 DiagCode::TextAfterQuote => "text_after_quote",
520 DiagCode::OnceWithoutId => "once_without_id",
521 DiagCode::SchemaNameOutsideMigrations => "schema_name_outside_migrations",
522 DiagCode::InvalidSchemaFile => "invalid_schema_file",
523 DiagCode::SchemaHashMismatch => "schema_hash_mismatch",
524 DiagCode::DuplicateMigrate => "duplicate_migrate",
525 DiagCode::InvalidMigrateVersion => "invalid_migrate_version",
526 DiagCode::InvalidMinSaveVersion => "invalid_min_save_version",
527 DiagCode::UnknownSchemaName => "unknown_schema_name",
528 DiagCode::MigrateKindMismatch => "migrate_kind_mismatch",
529 DiagCode::MigrateTypeMismatch => "migrate_type_mismatch",
530 DiagCode::DuplicateMigrateTarget => "duplicate_migrate_target",
531 DiagCode::MigrateImpureExpr => "migrate_impure_expr",
532 DiagCode::MissingFrozenSchema => "missing_frozen_schema",
533 DiagCode::MissingMigrate => "missing_migrate",
534 DiagCode::UnhandledTypeChange => "unhandled_type_change",
535 DiagCode::SchemaChangedSinceFreeze => "schema_changed_since_freeze",
536 DiagCode::EmptyChoice => "empty_choice",
537 DiagCode::AmbiguousIdentity => "ambiguous_identity",
538 DiagCode::IdentityTypo => "identity_typo",
539 DiagCode::UnknownPosition => "unknown_position",
540 DiagCode::UnknownLabelLiteral => "unknown_label_literal",
541 DiagCode::InvalidCommandUse => "invalid_command_use",
542 DiagCode::AssignToReadOnly => "assign_to_read_only",
543 DiagCode::NonExhaustiveMatch => "non_exhaustive_match",
544 DiagCode::UnreachablePattern => "unreachable_pattern",
545 DiagCode::UnknownField => "unknown_field",
546 DiagCode::UnknownVariant => "unknown_variant",
547 DiagCode::MissingField => "missing_field",
548 DiagCode::DuplicateField => "duplicate_field",
549 DiagCode::RecursiveType => "recursive_type",
550 DiagCode::CannotInferType => "cannot_infer_type",
551 DiagCode::UnknownMethod => "unknown_method",
552 DiagCode::StatementMethodInExpression => "statement_method_in_expression",
553 DiagCode::UnsupportedElementType => "unsupported_element_type",
554 DiagCode::InvalidMapKey => "invalid_map_key",
555 DiagCode::NotAllowedInFn => "not_allowed_in_fn",
556 DiagCode::AssignInFn => "assign_in_fn",
557 DiagCode::UnknownFunction => "unknown_function",
558 DiagCode::MissingTextId => "missing_text_id",
559 DiagCode::DeprecatedCommandName => "deprecated_command_name",
560 DiagCode::ReplNotAllowed => "repl_not_allowed",
561 DiagCode::FnNotSaveable => "fn_not_saveable",
562 }
563 }
564}
565
566#[derive(Debug, Clone, PartialEq, Eq)]
568pub enum DiagArg {
569 Str(String),
571 Int(i64),
573 List(Vec<String>),
575}
576
577impl From<&str> for DiagArg {
578 fn from(s: &str) -> Self {
579 DiagArg::Str(s.to_string())
580 }
581}
582
583impl From<String> for DiagArg {
584 fn from(s: String) -> Self {
585 DiagArg::Str(s)
586 }
587}
588
589impl From<i64> for DiagArg {
590 fn from(n: i64) -> Self {
591 DiagArg::Int(n)
592 }
593}
594
595impl From<usize> for DiagArg {
596 fn from(n: usize) -> Self {
598 DiagArg::Int(i64::try_from(n).unwrap_or(i64::MAX))
599 }
600}
601
602impl std::fmt::Display for DiagArg {
605 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
606 match self {
607 DiagArg::Str(s) => write!(f, "{s}"),
608 DiagArg::Int(n) => write!(f, "{n}"),
609 DiagArg::List(l) => write!(f, "{}", l.join(",")),
610 }
611 }
612}
613
614#[non_exhaustive]
616#[derive(Debug, Clone, PartialEq, Eq)]
617pub struct Diagnostic {
618 pub module: String,
620 pub line: u32,
622 pub col: u32,
624 pub severity: Severity,
626 pub code: DiagCode,
628 pub args: Vec<(&'static str, DiagArg)>,
630}
631
632impl Diagnostic {
633 pub fn arg_list(&self, name: &str) -> Option<&[String]> {
635 self.args.iter().find_map(|(n, v)| match v {
636 DiagArg::List(l) if *n == name => Some(l.as_slice()),
637 _ => None,
638 })
639 }
640
641 pub fn arg_str(&self, name: &str) -> Option<&str> {
643 self.args.iter().find_map(|(n, v)| match v {
644 DiagArg::Str(s) if *n == name => Some(s.as_str()),
645 _ => None,
646 })
647 }
648}