[fine] Environment cycles are internal compiler errors
Re-work the failure to add official ICE support, and extract the reporting from the tests into the semantics, so everybody benefits.
This commit is contained in:
parent
3877c6e547
commit
2f71946d21
2 changed files with 74 additions and 53 deletions
|
|
@ -344,18 +344,16 @@ impl<'a> Semantics<'a> {
|
|||
|
||||
pub fn environment_of(&self, t: TreeRef) -> EnvironmentRef {
|
||||
{
|
||||
let state = &mut self.environments.borrow_mut()[t.index()];
|
||||
// I want to make sure that this borrow is dropped after this block.
|
||||
let mut borrow = self.environments.borrow_mut();
|
||||
let state = &mut borrow[t.index()];
|
||||
match state {
|
||||
Incremental::None => (),
|
||||
Incremental::Complete(e) => return e.clone(),
|
||||
Incremental::InProgress => {
|
||||
// eprintln!("environment_of circular => {t:?}");
|
||||
self.report_error_tree_ref(
|
||||
t,
|
||||
"INTERNAL COMPILER ERROR: Circular dependency detected: environment",
|
||||
);
|
||||
*state = Incremental::Complete(self.empty_environment.clone());
|
||||
return self.empty_environment.clone();
|
||||
drop(borrow);
|
||||
self.internal_compiler_error(Some(t), "circular environment dependency");
|
||||
}
|
||||
}
|
||||
*state = Incremental::InProgress;
|
||||
|
|
@ -759,4 +757,66 @@ impl<'a> Semantics<'a> {
|
|||
self.report_error_tree(tree, format!("cannot find value {id} here"));
|
||||
Some(Type::Error)
|
||||
}
|
||||
|
||||
pub fn dump_compiler_state(&self, tr: Option<TreeRef>) {
|
||||
eprintln!("Parsed the tree as:");
|
||||
eprintln!("\n{}", self.syntax_tree.dump(true));
|
||||
|
||||
{
|
||||
let errors = self.errors.borrow();
|
||||
if errors.len() == 0 {
|
||||
eprintln!("There were no errors reported during checking.\n");
|
||||
} else {
|
||||
eprintln!(
|
||||
"{} error{} reported during checking:",
|
||||
errors.len(),
|
||||
if errors.len() == 1 { "" } else { "s" }
|
||||
);
|
||||
for error in errors.iter() {
|
||||
eprintln!(" Error: {error}");
|
||||
}
|
||||
eprintln!();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(tr) = tr {
|
||||
eprintln!("This is about the tree: {:?}", &self.syntax_tree[tr]);
|
||||
eprintln!("The logical parent chain of the tree was:\n");
|
||||
let mut current = Some(tr);
|
||||
while let Some(c) = current {
|
||||
let t = &self.syntax_tree[c];
|
||||
eprintln!(" {:?} [{}-{})", t.kind, t.start_pos, t.end_pos);
|
||||
current = self.logical_parents[c.index()];
|
||||
}
|
||||
eprintln!("\nThe environment of the tree was:");
|
||||
let mut environment = Some(self.environment_of(tr));
|
||||
while let Some(env) = environment {
|
||||
for (k, v) in env.declarations.iter() {
|
||||
eprintln!(" {k}: {:?}", v.declaration_type);
|
||||
}
|
||||
environment = env.parent.clone();
|
||||
}
|
||||
eprintln!();
|
||||
}
|
||||
}
|
||||
|
||||
fn internal_compiler_error(&self, tr: Option<TreeRef>, message: &str) -> ! {
|
||||
eprintln!("Internan compiler error: {message}!");
|
||||
self.dump_compiler_state(tr);
|
||||
panic!("INTERNAL COMPILER ERROR: {message}")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::parser::parse;
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "INTERNAL COMPILER ERROR: oh no")]
|
||||
pub fn ice() {
|
||||
let (tree, lines) = parse("1 + 1");
|
||||
let semantics = Semantics::new(&tree, &lines);
|
||||
semantics.internal_compiler_error(tree.root(), "oh no");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue