Skip to content

Commit

Permalink
One more error
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Apr 21, 2024
1 parent 8e901c2 commit 538de7e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_ -> #0 #0 (#0 Type)
2 changes: 1 addition & 1 deletion prism-compiler/src/lang/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl TcEnv {
write!(w, " ")?;
self.display(b, w, Base)?;
}
PartialExpr::Free => write!(w, "_")?,
PartialExpr::Free => write!(w, "{{{}}}", i.0)?,
PartialExpr::Shift(..) => unreachable!(),
}

Expand Down
42 changes: 36 additions & 6 deletions prism-compiler/src/lang/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ pub enum TypeError {
arg_type: UnionIndex,
},
IndexOutOfBound(UnionIndex),
InfiniteType(UnionIndex),
InfiniteType(UnionIndex, UnionIndex),
BadInfer {
free_var: UnionIndex,
inferred_var: UnionIndex,
},
}

impl TcEnv {
pub fn report(&mut self, error: &TypeError) -> Report<'static, Span> {
pub fn report(&mut self, error: &TypeError) -> Option<Report<'static, Span>> {
let report = Report::build(ReportKind::Error, (), 0);
match error {
Some(match error {
TypeError::ExpectType(i) => {
let ValueOrigin::TypeOf(j) = self.value_origins[i.0] else {
unreachable!()
Expand Down Expand Up @@ -106,9 +106,39 @@ impl TcEnv {
.with_label(label_fn)
.finish()
}
TypeError::InfiniteType(_) => report.finish(),
TypeError::InfiniteType(left, right) => {
let (left_span, left_description) = self.label_value(*left)?;
let (right_span, right_description) = self.label_value(*left)?;

report.with_message("Constraint creates an infinite type")
.with_label(Label::new(left_span).with_message(format!("Left side of constraint from {left_description}: {}", self.index_to_sm_string(*left))))
.with_label(Label::new(right_span).with_message(format!("Right side of constraint from {right_description}: {}", self.index_to_sm_string(*right))))
.with_help("If this doesn't obviously create an infinite type, I'm sorry. This is probably because of hidden constraints.")
.finish()
},
TypeError::BadInfer { .. } => report.finish(),
}
})
}

fn label_value(&self, mut value: UnionIndex) -> Option<(Span, &'static str)> {
let mut origin_description = "this value";
let span = loop {
match self.value_origins[value.0] {
ValueOrigin::SourceCode(span) => break span,
ValueOrigin::TypeOf(sub_value) => {
assert_eq!(origin_description, "this value");
origin_description = "type of this value";
value = sub_value;
}
ValueOrigin::FreeSub(v) => {
origin_description = "type of this value";
value = v
},
ValueOrigin::FreeValueFailure(_) => return None,
ValueOrigin::FreeTypeFailure(_) => return None,
}
};
Some((span, origin_description))
}
}

Expand All @@ -119,7 +149,7 @@ pub struct AggregatedTypeError {
impl AggregatedTypeError {
pub fn eprint(&self, env: &mut TcEnv, input: &str) -> io::Result<()> {
let mut input = Source::from(input);
for report in self.errors.iter().map(|err| env.report(err)) {
for report in self.errors.iter().flat_map(|err| env.report(err)) {
report.eprint(&mut input)?;
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions prism-compiler/src/lang/expect_beq_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ impl TcEnv {
(i2, s2, var_map2): (UnionIndex, &Env, &mut HashMap<UniqueVariableId, usize>),
) -> bool {
debug_assert!(matches!(self.values[i2.0], PartialExpr::Free));

if self.toxic_values.contains(&i1) {
self.errors.push(TypeError::InfiniteType(i1));
self.errors.push(TypeError::InfiniteType(i1, i2));
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion prism-compiler/tests/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ fn test_fail([test]: [&str; 1]) {
env.index_to_sm_string(typ),
env.index_to_br_string(typ));
}
Err(_) => return,
Err(errs) => {
errs.eprint(&mut env, test).unwrap()
},
}
}

Expand Down

0 comments on commit 538de7e

Please sign in to comment.