Skip to main content

hir_ty/lower/
diagnostics.rs

1//! This files contains the declaration of diagnostics kinds for ty and path lowering.
2
3use hir_def::{GenericDefId, GenericParamId, type_ref::TypeRefId};
4
5use crate::Span;
6
7#[derive(Debug, PartialEq, Eq, Clone)]
8pub enum TyLoweringDiagnostic {
9    PathDiagnostic { source: TypeRefId, diag: PathLoweringDiagnostic },
10    InferVarsNotAllowed { source: Span },
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum GenericArgsProhibitedReason {
15    Module,
16    TyParam,
17    SelfTy,
18    PrimitiveTy,
19    Const,
20    Static,
21    LocalVariable,
22    /// When there is a generic enum, within the expression `Enum::Variant`,
23    /// either `Enum` or `Variant` are allowed to have generic arguments, but not both.
24    EnumVariant,
25}
26
27/// A path can have many generic arguments: each segment may have one associated with the
28/// segment, and in addition, each associated type binding may have generic arguments. This
29/// enum abstracts over both.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum PathGenericsSource {
32    /// Generic arguments directly on the segment.
33    Segment(u32),
34    /// Generic arguments on an associated type, e.g. `Foo<Assoc<A, B> = C>` or `Foo<Assoc<A, B>: Bound>`.
35    AssocType { segment: u32, assoc_type: u32 },
36}
37
38#[derive(Debug, PartialEq, Eq, Clone)]
39pub enum PathLoweringDiagnostic {
40    GenericArgsProhibited {
41        segment: u32,
42        reason: GenericArgsProhibitedReason,
43    },
44    ParenthesizedGenericArgsWithoutFnTrait {
45        segment: u32,
46    },
47    /// The expected lifetimes & types and consts counts can be found by inspecting the `GenericDefId`.
48    IncorrectGenericsLen {
49        generics_source: PathGenericsSource,
50        provided_count: u32,
51        expected_count: u32,
52        kind: IncorrectGenericsLenKind,
53        def: GenericDefId,
54    },
55    IncorrectGenericsOrder {
56        generics_source: PathGenericsSource,
57        param_id: GenericParamId,
58        arg_idx: u32,
59        /// Whether the `GenericArgs` contains a `Self` arg.
60        has_self_arg: bool,
61    },
62    ElidedLifetimesInPath {
63        generics_source: PathGenericsSource,
64        def: GenericDefId,
65        expected_count: u32,
66        hard_error: bool,
67    },
68    /// An elided lifetimes was used (either implicitly, by not specifying lifetimes, or explicitly, by using `'_`),
69    /// but lifetime elision could not find a lifetime to replace it with.
70    ElisionFailure {
71        generics_source: PathGenericsSource,
72        def: GenericDefId,
73        expected_count: u32,
74    },
75    MissingLifetime {
76        generics_source: PathGenericsSource,
77        def: GenericDefId,
78        expected_count: u32,
79    },
80    /// Generic defaults are not allowed to refer to `Self`.
81    GenericDefaultRefersToSelf {
82        /// Index of the `Self` segment.
83        segment: u32,
84    },
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub enum IncorrectGenericsLenKind {
89    Lifetimes,
90    TypesAndConsts,
91}