tamer: asg::graph::object::new_rel_dyn: Use Option

Rather than panicing at this level, let's panic at the caller, simplifying
impls and keeping them total.

This can't occur now, but an upcoming change introducing a package type will
allow for such a thing.

DEV-13159
main
Mike Gerwitz 2023-01-30 11:27:40 -05:00
parent e6abd996b7
commit 2f08985111
2 changed files with 38 additions and 7 deletions

View File

@ -502,6 +502,18 @@ impl Asg {
*edge.weight(),
ObjectIndex::<Object>::new(edge.target(), oi),
)
.diagnostic_unwrap(|| {
vec![
oi.internal_error(format!(
"encountered invalid outgoing edge type {:?}",
edge.weight()
)),
oi.help(
"this means that Asg did not enforce edge invariants \
during construction, which is a significant bug",
),
]
})
})
}

View File

@ -566,8 +566,21 @@ pub trait ObjectRelatable: ObjectKind {
/// Represent a relation to another [`ObjectKind`] that cannot be
/// statically known and must be handled at runtime.
///
/// A value of [`None`] means that the provided [`ObjectRelTy`] is not
/// valid for [`Self`].
/// If the caller is utilizing edge data that is already present on the graph,
/// then this means that the system is not properly upholding edge
/// invariants
/// (the graph's ontology)
/// and the system ought to panic;
/// this is a significant bug representing a problem with the
/// correctness of the system.
///
/// See [`ObjectRel`] for more information.
fn new_rel_dyn(ty: ObjectRelTy, oi: ObjectIndex<Object>) -> Self::Rel;
fn new_rel_dyn(
ty: ObjectRelTy,
oi: ObjectIndex<Object>,
) -> Option<Self::Rel>;
}
/// A relationship to another [`ObjectKind`].
@ -650,10 +663,13 @@ impl ObjectRelatable for Ident {
ObjectRelTy::Ident
}
fn new_rel_dyn(ty: ObjectRelTy, oi: ObjectIndex<Object>) -> IdentRel {
fn new_rel_dyn(
ty: ObjectRelTy,
oi: ObjectIndex<Object>,
) -> Option<IdentRel> {
match ty {
ObjectRelTy::Ident => IdentRel::Ident(oi.must_narrow_into()),
ObjectRelTy::Expr => IdentRel::Expr(oi.must_narrow_into()),
ObjectRelTy::Ident => Some(IdentRel::Ident(oi.must_narrow_into())),
ObjectRelTy::Expr => Some(IdentRel::Expr(oi.must_narrow_into())),
}
}
}
@ -698,10 +714,13 @@ impl ObjectRelatable for Expr {
ObjectRelTy::Expr
}
fn new_rel_dyn(ty: ObjectRelTy, oi: ObjectIndex<Object>) -> ExprRel {
fn new_rel_dyn(
ty: ObjectRelTy,
oi: ObjectIndex<Object>,
) -> Option<ExprRel> {
match ty {
ObjectRelTy::Ident => ExprRel::Ident(oi.must_narrow_into()),
ObjectRelTy::Expr => ExprRel::Expr(oi.must_narrow_into()),
ObjectRelTy::Ident => Some(ExprRel::Ident(oi.must_narrow_into())),
ObjectRelTy::Expr => Some(ExprRel::Expr(oi.must_narrow_into())),
}
}
}