Skip to content

Commit de0e469

Browse files
committed
Add Term::error smart constructor
1 parent 666b716 commit de0e469

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

fathom/src/core.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ impl<'arena> Term<'arena> {
283283
pub fn is_error(&self) -> bool {
284284
matches!(self, Term::Prim(_, Prim::ReportedError))
285285
}
286+
287+
pub fn error(span: impl Into<Span>) -> Term<'arena> {
288+
Term::Prim(span.into(), Prim::ReportedError)
289+
}
286290
}
287291

288292
macro_rules! def_prims {

fathom/src/surface/elaboration.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::sync::Arc;
2626

2727
use scoped_arena::Scope;
2828

29-
use super::ExprField;
3029
use crate::alloc::SliceVec;
3130
use crate::core::semantics::{self, ArcValue, Head, Telescope, Value};
3231
use crate::core::{self, prim, Const, Plicity, Prim, UIntStyle};
@@ -668,7 +667,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
668667
expected: to,
669668
error,
670669
});
671-
core::Term::Prim(span, Prim::ReportedError)
670+
core::Term::error(span)
672671
}
673672
},
674673
}
@@ -1052,7 +1051,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
10521051
.check_record_fields(*range, expr_fields, |field| field.label, labels)
10531052
.is_err()
10541053
{
1055-
return core::Term::Prim(file_range.into(), Prim::ReportedError);
1054+
return core::Term::error(file_range);
10561055
}
10571056

10581057
let mut types = types.clone();
@@ -1119,7 +1118,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
11191118
.check_tuple_fields(*range, elem_exprs, |term| term.range(), labels)
11201119
.is_err()
11211120
{
1122-
return core::Term::Prim(file_range.into(), Prim::ReportedError);
1121+
return core::Term::error(file_range);
11231122
}
11241123

11251124
let mut types = types.clone();
@@ -1153,16 +1152,14 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
11531152
Some((Prim::Array64Type, [App(_, len), App(_, elem_type)])) => {
11541153
(Some(len), elem_type)
11551154
}
1156-
Some((Prim::ReportedError, _)) => {
1157-
return core::Term::Prim(file_range.into(), Prim::ReportedError)
1158-
}
1155+
Some((Prim::ReportedError, _)) => return core::Term::error(file_range),
11591156
_ => {
11601157
let expected_type = self.pretty_print_value(&expected_type);
11611158
self.push_message(Message::ArrayLiteralNotSupported {
11621159
range: file_range,
11631160
expected_type,
11641161
});
1165-
return core::Term::Prim(file_range.into(), Prim::ReportedError);
1162+
return core::Term::error(file_range);
11661163
}
11671164
};
11681165

@@ -1173,7 +1170,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
11731170
Some(Value::ConstLit(Const::U32(len, _))) => Some(*len as u64),
11741171
Some(Value::ConstLit(Const::U64(len, _))) => Some(*len),
11751172
Some(Value::Stuck(Head::Prim(Prim::ReportedError), _)) => {
1176-
return core::Term::Prim(file_range.into(), Prim::ReportedError);
1173+
return core::Term::error(file_range)
11771174
}
11781175
_ => None,
11791176
};
@@ -1199,7 +1196,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
11991196
expected_len,
12001197
});
12011198

1202-
core::Term::Prim(file_range.into(), Prim::ReportedError)
1199+
return core::Term::error(file_range);
12031200
}
12041201
}
12051202
}
@@ -1226,7 +1223,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
12261223

12271224
match constant {
12281225
Some(constant) => core::Term::ConstLit(file_range.into(), constant),
1229-
None => core::Term::Prim(file_range.into(), Prim::ReportedError),
1226+
None => core::Term::error(file_range),
12301227
}
12311228
}
12321229
(Term::NumberLiteral(range, lit), _) => {
@@ -1248,19 +1245,19 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
12481245
range: file_range,
12491246
expected_type,
12501247
});
1251-
return core::Term::Prim(file_range.into(), Prim::ReportedError);
1248+
return core::Term::error(file_range);
12521249
}
12531250
};
12541251

12551252
match constant {
12561253
Some(constant) => core::Term::ConstLit(file_range.into(), constant),
1257-
None => core::Term::Prim(file_range.into(), Prim::ReportedError),
1254+
None => core::Term::error(file_range),
12581255
}
12591256
}
12601257
(Term::BinOp(range, lhs, op, rhs), _) => {
12611258
self.check_bin_op(*range, lhs, *op, rhs, &expected_type)
12621259
}
1263-
(Term::ReportedError(_), _) => core::Term::Prim(file_range.into(), Prim::ReportedError),
1260+
(Term::ReportedError(_), _) => core::Term::error(file_range),
12641261
(_, _) => {
12651262
let surface_range = surface_term.range();
12661263
let (synth_term, synth_type) = self.synth(surface_term);
@@ -1633,9 +1630,8 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
16331630
// There's been an error when elaborating the head of
16341631
// the projection, so avoid trying to elaborate any
16351632
// further to prevent cascading type errors.
1636-
(core::Term::Prim(_, Prim::ReportedError), _)
1637-
| (_, Value::Stuck(Head::Prim(Prim::ReportedError), _)) => {
1638-
return self.synth_reported_error(*range);
1633+
(expr, r#type) if expr.is_error() || r#type.is_error() => {
1634+
return self.synth_reported_error(*range)
16391635
}
16401636
// The head expression was not a record type.
16411637
// Fallthrough with an error.
@@ -1774,7 +1770,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
17741770
self.coerce(range, expr, &type_value, expected_type)
17751771
}
17761772
Value::Stuck(Head::Prim(Prim::ReportedError), _) => {
1777-
core::Term::Prim(file_range.into(), Prim::ReportedError)
1773+
core::Term::error(file_range)
17781774
}
17791775
_ => {
17801776
self.push_message(Message::UnexpectedParameter {
@@ -1783,7 +1779,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
17831779
// TODO: For improved error recovery, bind the rest of
17841780
// the parameters, and check the body of the function
17851781
// literal using the expected body type.
1786-
core::Term::Prim(file_range.into(), Prim::ReportedError)
1782+
core::Term::error(file_range)
17871783
}
17881784
}
17891785
}
@@ -2091,7 +2087,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
20912087

20922088
fn synth_reported_error(&mut self, range: ByteRange) -> (core::Term<'arena>, ArcValue<'arena>) {
20932089
let file_range = self.file_range(range);
2094-
let expr = core::Term::Prim(file_range.into(), Prim::ReportedError);
2090+
let expr = core::Term::error(file_range);
20952091
let r#type = self.push_unsolved_type(MetaSource::ReportedErrorType(file_range));
20962092
(expr, r#type)
20972093
}
@@ -2353,7 +2349,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
23532349
CheckedPattern::ReportedError(range) => {
23542350
self.check(body_expr, &match_info.expected_type);
23552351
self.elab_match_unreachable(match_info, equations);
2356-
core::Term::Prim(range.into(), Prim::ReportedError)
2352+
core::Term::error(range)
23572353
}
23582354
}
23592355
}
@@ -2440,7 +2436,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
24402436
}
24412437
CheckedPattern::ReportedError(range) => {
24422438
(self.local_env).push_param(None, match_info.scrutinee.r#type.clone());
2443-
let default_expr = core::Term::Prim(range.into(), Prim::ReportedError);
2439+
let default_expr = core::Term::error(range);
24442440
default_branch = (None, self.scope.to_scope(default_expr) as &_);
24452441
self.local_env.pop();
24462442
}
@@ -2498,10 +2494,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
24982494
scrutinee_expr_range: self.file_range(match_info.scrutinee.range),
24992495
});
25002496
}
2501-
core::Term::Prim(
2502-
self.file_range(match_info.range).into(),
2503-
Prim::ReportedError,
2504-
)
2497+
core::Term::error(self.file_range(match_info.range))
25052498
}
25062499
}
25072500

0 commit comments

Comments
 (0)