1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! `OpErrors` type.

use core::ops;

use arithmetic_parser::{grammars::Grammar, Destructure, Spanned, SpannedExpr, SpannedLvalue};

use crate::{
    alloc::Vec,
    ast::TypeAst,
    error::{Error, ErrorContext, ErrorKind, ErrorPathFragment},
    PrimitiveType,
};

/// Error container tied to a particular top-level operation that has a certain span
/// and [context](ErrorContext).
///
/// Supplied as an argument to [`TypeArithmetic`] methods and [`Substitutions::unify()`].
///
/// [`TypeArithmetic`]: crate::arith::TypeArithmetic
/// [`Substitutions::unify()`]: crate::arith::Substitutions::unify()
#[derive(Debug)]
pub struct OpErrors<'a, Prim: PrimitiveType> {
    errors: Goat<'a, Vec<ErrorPrecursor<Prim>>>,
    current_path: Vec<ErrorPathFragment>,
}

impl<Prim: PrimitiveType> OpErrors<'_, Prim> {
    /// Adds a new `error` into this the error list.
    pub fn push(&mut self, kind: ErrorKind<Prim>) {
        self.errors.push(ErrorPrecursor {
            kind,
            path: self.current_path.clone(),
        });
    }

    /// Invokes the provided closure and returns `false` if new errors were
    /// added during the closure execution.
    pub fn check(&mut self, check: impl FnOnce(OpErrors<'_, Prim>)) -> bool {
        let error_count = self.errors.len();
        check(self.by_ref());
        self.errors.len() == error_count
    }

    /// Mutably borrows this container allowing to use it multiple times.
    pub fn by_ref(&mut self) -> OpErrors<'_, Prim> {
        OpErrors {
            errors: Goat::Borrowed(&mut *self.errors),
            current_path: self.current_path.clone(),
        }
    }

    /// Narrows down the path to the error.
    pub fn join_path(&mut self, path: impl Into<ErrorPathFragment>) -> OpErrors<'_, Prim> {
        let mut current_path = self.current_path.clone();
        current_path.push(path.into());
        OpErrors {
            errors: Goat::Borrowed(&mut *self.errors),
            current_path,
        }
    }

    pub(crate) fn push_path_fragment(&mut self, path: impl Into<ErrorPathFragment>) {
        self.current_path.push(path.into());
    }

    pub(crate) fn pop_path_fragment(&mut self) {
        self.current_path.pop().expect("Location is empty");
    }

    #[cfg(test)]
    pub(crate) fn into_vec(self) -> Vec<ErrorKind<Prim>> {
        let Goat::Owned(errors) = self.errors else {
            panic!("Attempt to call `into_vec` for borrowed errors");
        };
        errors.into_iter().map(|err| err.kind).collect()
    }
}

impl<Prim: PrimitiveType> OpErrors<'static, Prim> {
    pub(crate) fn new() -> Self {
        Self {
            errors: Goat::Owned(Vec::new()),
            current_path: Vec::new(),
        }
    }

    pub(crate) fn contextualize<T: Grammar>(
        self,
        span: &SpannedExpr<'_, T>,
        context: impl Into<ErrorContext<Prim>>,
    ) -> Vec<Error<Prim>> {
        let context = context.into();
        self.do_contextualize(|item| item.into_expr_error(context.clone(), span))
    }

    fn do_contextualize(
        self,
        map_fn: impl Fn(ErrorPrecursor<Prim>) -> Error<Prim>,
    ) -> Vec<Error<Prim>> {
        let Goat::Owned(errors) = self.errors else {
            unreachable!()
        };
        errors.into_iter().map(map_fn).collect()
    }

    pub(crate) fn contextualize_assignment<'a>(
        self,
        span: &SpannedLvalue<'a, TypeAst<'a>>,
        context: &ErrorContext<Prim>,
    ) -> Vec<Error<Prim>> {
        if self.errors.is_empty() {
            Vec::new()
        } else {
            self.do_contextualize(|item| item.into_assignment_error(context.clone(), span))
        }
    }

    pub(crate) fn contextualize_destructure<'a>(
        self,
        span: &Spanned<'a, Destructure<'a, TypeAst<'a>>>,
        create_context: impl FnOnce() -> ErrorContext<Prim>,
    ) -> Vec<Error<Prim>> {
        if self.errors.is_empty() {
            Vec::new()
        } else {
            let context = create_context();
            self.do_contextualize(|item| item.into_destructure_error(context.clone(), span))
        }
    }
}

/// Analogue of `Cow` with a mutable ref.
#[derive(Debug)]
enum Goat<'a, T> {
    Owned(T),
    Borrowed(&'a mut T),
}

impl<T> ops::Deref for Goat<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Owned(value) => value,
            Self::Borrowed(mut_ref) => mut_ref,
        }
    }
}

impl<T> ops::DerefMut for Goat<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            Self::Owned(value) => value,
            Self::Borrowed(mut_ref) => mut_ref,
        }
    }
}

#[derive(Debug)]
struct ErrorPrecursor<Prim: PrimitiveType> {
    kind: ErrorKind<Prim>,
    path: Vec<ErrorPathFragment>,
}

impl<Prim: PrimitiveType> ErrorPrecursor<Prim> {
    fn into_expr_error<T: Grammar>(
        self,
        context: ErrorContext<Prim>,
        root_expr: &SpannedExpr<'_, T>,
    ) -> Error<Prim> {
        Error {
            inner: ErrorPathFragment::walk_expr(&self.path, root_expr)
                .copy_with_extra(self.kind)
                .into(),
            root_location: root_expr.with_no_extra().into(),
            context,
            path: self.path,
        }
    }

    fn into_assignment_error<'a>(
        self,
        context: ErrorContext<Prim>,
        root_lvalue: &SpannedLvalue<'a, TypeAst<'a>>,
    ) -> Error<Prim> {
        Error {
            inner: ErrorPathFragment::walk_lvalue(&self.path, root_lvalue)
                .copy_with_extra(self.kind)
                .into(),
            root_location: root_lvalue.with_no_extra().into(),
            context,
            path: self.path,
        }
    }

    fn into_destructure_error<'a>(
        self,
        context: ErrorContext<Prim>,
        root_destructure: &Spanned<'a, Destructure<'a, TypeAst<'a>>>,
    ) -> Error<Prim> {
        Error {
            inner: ErrorPathFragment::walk_destructure(&self.path, root_destructure)
                .copy_with_extra(self.kind)
                .into(),
            root_location: root_destructure.with_no_extra().into(),
            context,
            path: self.path,
        }
    }
}