Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions crates/smlib/src/core/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(super) fn status(e: SolidError) -> Error {
e if is_topology_failure(&e) => Error::Status(e.sm_status().unwrap_or(solidcore::error::SM_ERR)),
e if is_curve_failure(&e) => Error::Status(e.sm_status().unwrap_or(solidcore::error::SM_ERR)),
e if is_surface_failure(&e) => Error::Status(e.sm_status().unwrap_or(solidcore::error::SM_ERR)),
e if is_offset_failure(&e) => Error::Status(e.sm_status().unwrap_or(solidcore::error::SM_ERR)),
SolidError::InvalidInput { .. } => Error::Status(INVALID_INPUT),
SolidError::Unsupported { reason, .. } if reason == MIXED_CORNER_RAILS_CROSS => Error::Unported(MIXED_CORNER_UNSUPPORTED),
e if is_boolean_failure(&e) => boolean_status(e),
Expand Down Expand Up @@ -172,6 +173,13 @@ fn is_surface_failure(e: &SolidError) -> bool {
e.surface_failure().is_some()
}

/// An offset routine's failure (solidcore's `OffsetFailure`) that replaced a status the routine
/// returned (`Upstream` before solidcore described it): it passes on as that status. An offset's
/// refusals replaced none and pass on as before.
fn is_offset_failure(e: &SolidError) -> bool {
e.offset_failure().is_some_and(|o| o.replaces_upstream_status())
}

/// `size_ok`.
fn size_ok(x: f64) -> bool {
x.is_finite() && x > 0.0 && x <= 1e6
Expand Down Expand Up @@ -1606,4 +1614,24 @@ mod tests {
assert!(matches!(status(surface(SurfaceFailure::OutsideDomain("the point is outside the sweep"))), Error::Status(1020)));
assert!(matches!(status(surface(SurfaceFailure::OffSurface("the point is off the surface"))), Error::Status(1021)));
}

/// An offset routine's failure passes on as the status it replaced, as the `Upstream` it
/// replaced did, whether or not it names a face; an offset's refusal does not pass on as a
/// status.
#[test]
fn offset_failures_pass_on_as_their_status() {
use solidcore::error::Failure;
use solidcore::error::offset::OffsetFailure;
let offset = |o| Failure::new("offset::offset_executive::crossing_loops", o).into_error();
assert!(matches!(status(offset(OffsetFailure::Failed("x"))), Error::Status(1001)));
assert!(matches!(status(offset(OffsetFailure::Missing("the face's surface"))), Error::Status(1006)));
assert!(matches!(status(offset(OffsetFailure::InvalidValue("x"))), Error::Status(1007)));
assert!(matches!(status(offset(OffsetFailure::Inconsistent("x"))), Error::Status(1005)));
let at = Failure::new("offset::draft", OffsetFailure::InvalidValue("the face has no surface or no outer face use"))
.at(solidcore::EntityRef::Face(solidcore::topology::FaceId::from_index(2)))
.into_error();
assert!(matches!(status(at), Error::Status(1007)));
let folds = offset(OffsetFailure::Folds { face: solidcore::topology::FaceId::from_index(1), distance: 2.0, largest: 1.0 });
assert!(!matches!(status(folds), Error::Status(_)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,9 @@ fn rust_cr_eu_body(
Ok(fs) => fs,
Err(e) if e.is_unported() => return Err(e),
// the tangent-surface exits (SM_ERR only signalled upstream)
Err(SolidError::Upstream { code, .. }) => return Ok(vec![f64::from(code)]),
Err(e) if solidcore::error::upstream_status(&e).is_some() => {
return Ok(vec![f64::from(solidcore::error::upstream_status(&e).unwrap_or(-1))]);
}
Err(e) => return Err(e),
};
fs.solver.surface_extension_factor = ext;
Expand Down Expand Up @@ -817,8 +819,7 @@ fn surface_of(s: &SrfIn) -> Result<Surface, HarnessError> {
fn status(e: &SolidError) -> f64 {
match e {
SolidError::InvalidInput { .. } | SolidError::PortInvariant { .. } => 1007.0,
SolidError::Upstream { code, .. } => f64::from(*code),
_ => -1.0,
e => solidcore::error::upstream_status(e).map_or(-1.0, f64::from),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ impl From<HarnessError> for Fail {
fn status(e: &SolidError) -> f64 {
match e {
SolidError::InvalidInput { .. } | SolidError::PortInvariant { .. } => 1007.0,
SolidError::Upstream { code, .. } => f64::from(*code),
_ => -1.0,
e => solidcore::error::upstream_status(e).map_or(-1.0, f64::from),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,9 @@ fn static_rust(case: &Case) -> Result<Output, HarnessError> {
}
let status = match e {
SolidError::InvalidInput { .. } | SolidError::PortInvariant { .. } => 1007.0,
SolidError::Upstream { code, .. } => f64::from(code),
ref e if solidcore::error::upstream_status(e).is_some() => {
f64::from(solidcore::error::upstream_status(e).unwrap_or(-1))
}
// a typed refusal (no upstream status): recorded, not a harness failure
SolidError::Unsupported { .. } => return Ok(Output::err(e.to_string())),
_ => return Err(HarnessError::new(e.to_string())),
Expand Down
39 changes: 26 additions & 13 deletions solidcore/crates/solidcore/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,12 @@ impl core::fmt::Display for Failure {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}: {}", self.operation, self.reason)?;
// a reason that names no entity of its own is followed by the failure's
if let (Reason::Failed(_) | Reason::Topology(_), Some(entity)) = (&self.reason, self.entity)
{
let names_none = match &self.reason {
Reason::Failed(_) | Reason::Topology(_) => true,
Reason::Offset(o) => o.replaces_upstream_status(),
_ => false,
};
if let (true, Some(entity)) = (names_none, self.entity) {
write!(f, " ({entity})")?;
}
if let Some(cause) = &self.cause {
Expand Down Expand Up @@ -1095,9 +1099,11 @@ impl SolidError {
/// an `Upstream` site ([`nurbs::BSplineFailure`], [`intersect::IntersectFailure`],
/// [`primitive::PrimitiveFailure`], [`mass::MassFailure`], [`heal::HealFailure`],
/// [`geom::GeomFailure`], [`query::QueryFailure`], [`topology::TopologyFailure`],
/// [`surface::SurfaceFailure`], a [`sweep::SweepFailure`] or [`curve::CurveFailure`] that
/// replaced one ([`sweep::SweepFailure::replaces_upstream_status`],
/// [`curve::CurveFailure::replaces_upstream_status`])). The callers that branch on an
/// [`surface::SurfaceFailure`], a [`sweep::SweepFailure`], [`curve::CurveFailure`] or
/// [`offset::OffsetFailure`] that replaced one
/// ([`sweep::SweepFailure::replaces_upstream_status`],
/// [`curve::CurveFailure::replaces_upstream_status`],
/// [`offset::OffsetFailure::replaces_upstream_status`])). The callers that branch on an
/// upstream status use it so that they treat both alike; it goes with `Upstream` in
/// the last PR of the typed-errors work.
#[must_use]
Expand All @@ -1116,6 +1122,7 @@ impl SolidError {
| Reason::Surface(_) => Some(f.status),
Reason::Sweep(w) if w.replaces_upstream_status() => Some(f.status),
Reason::Curve(c) if c.replaces_upstream_status() => Some(f.status),
Reason::Offset(o) if o.replaces_upstream_status() => Some(f.status),
_ => None,
},
_ => None,
Expand Down Expand Up @@ -1337,9 +1344,11 @@ impl SolidError {
/// ([`nurbs::BSplineFailure`], [`intersect::IntersectFailure`],
/// [`primitive::PrimitiveFailure`], [`mass::MassFailure`], [`heal::HealFailure`],
/// [`geom::GeomFailure`], [`query::QueryFailure`], [`topology::TopologyFailure`],
/// [`surface::SurfaceFailure`], a [`sweep::SweepFailure`] or [`curve::CurveFailure`] that
/// replaced one ([`sweep::SweepFailure::replaces_upstream_status`],
/// [`curve::CurveFailure::replaces_upstream_status`])).
/// [`surface::SurfaceFailure`], a [`sweep::SweepFailure`], [`curve::CurveFailure`] or
/// [`offset::OffsetFailure`] that replaced one
/// ([`sweep::SweepFailure::replaces_upstream_status`],
/// [`curve::CurveFailure::replaces_upstream_status`],
/// [`offset::OffsetFailure::replaces_upstream_status`])).
///
/// For difftest's comparison only: it is removed with `Upstream` and `sm_status` in the
/// last PR of the typed-errors work. Callers match [`SolidError::kind`] and the
Expand Down Expand Up @@ -2336,16 +2345,17 @@ mod tests {
pub(crate) mod replaced_upstream_samples {
use super::SolidError;

/// The typed failures of three areas that replaced an `Upstream` with `code` (#13790):
/// an intersection's, a geometry utility's and a topology routine's, at `entity` when one is given.
/// The typed failures of four areas that replaced an `Upstream` with `code` (#13790):
/// an intersection's, a geometry utility's, a topology routine's and an offset routine's,
/// at `entity` when one is given.
pub(crate) fn replaced_upstream(
operation: &'static str,
code: i32,
entity: Option<crate::error::EntityRef>,
) -> [SolidError; 3] {
) -> [SolidError; 4] {
use crate::error::{
geom::GeomFailure, intersect::IntersectFailure, topology::TopologyFailure, Failure,
Reason,
geom::GeomFailure, intersect::IntersectFailure, offset::OffsetFailure,
topology::TopologyFailure, Failure, Reason,
};
let typed = |reason: Reason| {
let f = Failure::new(operation, reason).with_status(code);
Expand All @@ -2365,6 +2375,9 @@ pub(crate) mod replaced_upstream_samples {
typed(Reason::Topology(TopologyFailure::Failed(
"replaced an upstream status",
))),
typed(Reason::Offset(OffsetFailure::Failed(
"replaced an upstream status",
))),
]
}

Expand Down
155 changes: 129 additions & 26 deletions solidcore/crates/solidcore/src/error/intersect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
//! `operation` path says which routine. The handles these routines work on are the
//! classification's or intersector's own, not ids of the caller's B-rep, so no failure
//! names an entity. The failures that are checks of a caller's handles stay
//! [`crate::SolidError::InvalidInput`]. `surface_intersector` and `topology_solver` do
//! not use these yet.
//! [`crate::SolidError::InvalidInput`]. `surface_intersector/p1` and `p5`, which have
//! open work elsewhere, do not use these yet.

use super::{
ErrorKind, Failure, Reason, SM_ERR, SM_ERR_ASSERT_FAILURE, SM_ERR_NULL_POINTER,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl crate::SolidError {
}

#[cfg(test)]
mod tests {
pub(super) mod tests {
use super::*;
use crate::SolidError;

Expand Down Expand Up @@ -153,38 +153,141 @@ mod tests {
);
}

/// The intersection routines that describe their failures build no bare `Upstream`
/// status in their library code.
/// A sample of the second set of converted files (`surface_intersector/p2` to `p4`,
/// `topology_solver/p1` and `p2`): each keeps the operation, status, kind and entity of
/// its `Upstream`.
#[test]
fn the_described_intersect_routines_build_no_bare_statuses() {
let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/intersect");
let mut files = vec![
"point_class.rs".to_owned(),
"ray_tracer.rs".to_owned(),
"surface_drop_curve.rs".to_owned(),
"surface_drop_lib/p1.rs".to_owned(),
"surface_drop_lib/p2.rs".to_owned(),
"surface_drop_lib/p3.rs".to_owned(),
"adv_surface_intersector.rs".to_owned(),
"api_intersectors.rs".to_owned(),
"surface_silhouette.rs".to_owned(),
"surface_tracer.rs".to_owned(),
fn the_surface_intersector_and_topology_solver_samples_keep_their_upstream_answers() {
let samples = [
(
"intersect::surface_intersector::p2::SurfaceIntersector::compute_point_values",
IntersectFailure::Failed("the surface point has no normal"),
SM_ERR,
),
(
"intersect::surface_intersector::p3::SurfaceIntersector::find_boundary_start_points",
IntersectFailure::Missing("the surface's cache"),
SM_ERR_NULL_POINTER,
),
(
"intersect::surface_intersector::p4::SurfaceIntersector::do_law_intersection",
IntersectFailure::Missing("the first surface's offset surface"),
SM_ERR_NULL_POINTER,
),
(
"intersect::topology_solver::p1::LocalSolver::solve_it",
IntersectFailure::Failed("the base, point and curve local solvers solve nothing"),
SM_ERR,
),
(
"intersect::topology_solver::p2::LocalSolver::solve_face_face",
IntersectFailure::Missing("the first surface to solve against"),
SM_ERR_NULL_POINTER,
),
];
for (operation, failure, code) in samples {
let typed = SolidError::intersect(operation, failure);
let before = SolidError::Upstream {
operation,
code,
entity: None,
};
assert_eq!(typed.sm_status(), before.sm_status(), "{typed:?}");
assert_eq!(typed.upstream_status(), Some(code), "{typed:?}");
assert_eq!(super::super::upstream_status(&typed), Some(code));
assert_eq!(typed.kind(), before.kind(), "{typed:?}");
assert_eq!(typed.operation(), before.operation(), "{typed:?}");
assert_eq!(typed.entity(), None, "{typed:?}");
assert_eq!(typed.is_port_failure(), before.is_port_failure());
assert_eq!(typed.is_status_answer(), before.is_status_answer());
assert_eq!(
typed.is_unported_or_cancelled(),
before.is_unported_or_cancelled()
);
}
}

/// The intersection routines describe their failures: their library code builds no
/// bare `Upstream` status, except the files listed (with the bare sites each may still
/// hold) whose conversion waits on other open work.
#[test]
fn the_intersect_routines_build_no_bare_statuses() {
const WAITING: [(&str, usize); 2] = [
("intersect/surface_intersector/p1.rs", 6),
("intersect/surface_intersector/p5.rs", 6),
];
files.extend((1..=9).map(|i| format!("curve_class/p{i}.rs")));
for rel in files {
let text = std::fs::read_to_string(src.join(&rel))
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
assert_eq!(bare_statuses_under(&root, "intersect", 40), WAITING);
}

/// The files under `src/<area>` (and `src/<area>.rs`) whose library code builds bare
/// statuses, with the number each builds, in path order; at least `least` files are read.
pub(in crate::error) fn bare_statuses_under(
root: &std::path::Path,
area: &str,
least: usize,
) -> Vec<(&'static str, usize)> {
let mut files = vec![root.join(format!("{area}.rs"))];
let mut dirs = vec![root.join(area)];
while let Some(dir) = dirs.pop() {
for entry in std::fs::read_dir(dir).expect("read dir") {
let path = entry.expect("entry").path();
if path.is_dir() {
dirs.push(path);
} else if path.extension().is_some_and(|x| x == "rs") {
files.push(path);
}
}
}
files.sort();
assert!(files.len() >= least, "{files:?}");
let mut found = Vec::new();
for path in files {
let relative = path
.strip_prefix(root)
.expect("under src")
.to_string_lossy()
.replace('\\', "/");
if relative.ends_with("/tests.rs") || relative.ends_with("_tests.rs") {
continue;
}
let text = std::fs::read_to_string(&path)
.expect("read")
.replace("\r\n", "\n");
// The library part ends at the test module: `#[cfg(test)]`, then any attributes
// (such as an `#[allow(..)]` for the tests' unwraps), then `mod` with any
// visibility.
let library = text
.find("#[cfg(test)]\nmod tests")
.map_or(text.as_str(), |at| &text[..at]);
for needle in [
.match_indices("\n#[cfg(test)]\n")
.find(|(at, _)| {
let mut rest = text[at + "\n#[cfg(test)]\n".len()..].lines();
rest.find(|line| {
let line = line.trim_start();
!(line.starts_with("#[") || line.starts_with("clippy::") || line == ")]")
})
.is_some_and(|line| {
let line = line
.strip_prefix("pub(crate) ")
.or_else(|| line.strip_prefix("pub(super) "))
.or_else(|| line.strip_prefix("pub "))
.unwrap_or(line);
line.starts_with("mod ")
})
})
.map_or(text.as_str(), |(at, _)| &text[..at]);
let bare: usize = [
"SolidError::sm_err(",
"SolidError::null_pointer(",
"SolidError::Upstream {",
] {
assert!(!library.contains(needle), "{needle} in {rel}");
"SolidError::nl_ser(",
]
.iter()
.map(|needle| library.matches(needle).count())
.sum();
if bare > 0 {
found.push((&*relative.leak(), bare));
}
}
found
}
}
Loading
Loading