[DEV-7134] Propagate sorting errors

If a node is found while sorting that is not expected, we should show
the error to the user.
master
Joseph Frazer 2020-03-07 10:49:41 -05:00
parent 2a5551a04a
commit f373a00a80
3 changed files with 18 additions and 7 deletions

View File

@ -214,6 +214,8 @@ pub enum AsgError {
///
/// See [`Asg::set_fragment`] for more information.
BadFragmentDest(String),
/// The node was not expected in the current context
UnexpectedNode(String),
}
impl std::fmt::Display for AsgError {
@ -222,6 +224,9 @@ impl std::fmt::Display for AsgError {
Self::BadFragmentDest(msg) => {
write!(fmt, "bad fragment destination: {}", msg)
}
Self::UnexpectedNode(msg) => {
write!(fmt, "unexpected node: {}", msg)
}
}
}
}

View File

@ -186,7 +186,7 @@ mod graph;
mod ident;
mod object;
pub use graph::{Asg, AsgResult, ObjectRef};
pub use graph::{Asg, AsgError, AsgResult, ObjectRef};
pub use ident::{Dim, IdentKind};
pub use object::{FragmentText, Object, Source};

View File

@ -21,7 +21,9 @@
//! banished to its own file to try to make that more clear.
use crate::global;
use crate::ir::asg::{Asg, DefaultAsg, IdentKind, Object, ObjectRef, Source};
use crate::ir::asg::{
Asg, AsgError, DefaultAsg, IdentKind, Object, ObjectRef, Source,
};
use crate::obj::xmle::writer::{Sections, XmleWriter};
use crate::obj::xmlo::reader::{XmloError, XmloEvent, XmloReader};
use crate::sym::{DefaultInterner, Interner, Symbol};
@ -76,7 +78,7 @@ pub fn main(package_path: &str, output: &str) -> Result<(), Box<dyn Error>> {
.filter_map(|sym| depgraph.lookup(sym)),
);
let mut sorted = sort_deps(&depgraph, &roots);
let mut sorted = sort_deps(&depgraph, &roots)?;
//println!("Sorted ({}): {:?}", sorted.len(), sorted);
@ -239,7 +241,7 @@ fn load_xmlo<'a, 'i, I: Interner<'i>>(
fn sort_deps<'a, 'i>(
depgraph: &'a LinkerAsg<'i>,
roots: &Vec<LinkerObjectRef>,
) -> Sections<'a, 'i> {
) -> Result<Sections<'a, 'i>, Box<dyn Error>> {
// @type=meta, @preproc:elig-class-yields
// @type={ret}map{,:head,:tail}
@ -259,7 +261,7 @@ fn sort_deps<'a, 'i>(
// TODO: can we encapsulate NodeIndex?
while let Some(index) = dfs.next(&depgraph) {
let ident = depgraph.get(index).unwrap();
let ident = depgraph.get(index).expect("missing node");
match ident {
Object::Ident(_, kind, _)
@ -277,11 +279,15 @@ fn sort_deps<'a, 'i>(
| IdentKind::RetMapTail => deps.retmap.push_body(ident),
_ => deps.rater.push_body(ident),
},
_ => panic!("unexpected node: {:?}", ident),
_ => {
return Err(
AsgError::UnexpectedNode(format!("{:?}", ident)).into()
)
}
}
}
deps
Ok(deps)
}
fn get_interner_value<'a, 'i, I: Interner<'i>>(