| @@ -22,6 +22,15 @@ pub const INVALID_ROOT: &str = "E0902"; |
| 22 | 22 | pub const COMPILATION_FAILED: &str = "E0903"; |
| 23 | 23 | pub const UNSUPPORTED_RESULT: &str = "E0904"; |
| 24 | 24 | |
| 25 | +/// A transactional driver outcome that is not a successful `Program`. |
| 26 | +/// Cancellation is scheduler control flow, not a source diagnostic; neither |
| 27 | +/// variant exposes intermediate values or a partially transformed program. |
| 28 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 29 | +pub enum CtfeDriverError { |
| 30 | + Diagnostics(Vec<Diagnostic>), |
| 31 | + Cancelled, |
| 32 | +} |
| 33 | + |
| 25 | 34 | // A root borrows neither HIR nor strings; these owned fields keep diagnostic |
| 26 | 35 | // construction straightforward while the prepared Program grows during |
| 27 | 36 | // evidence specialization. |
| @@ -83,15 +92,29 @@ impl MarkedRoot { |
| 83 | 92 | } |
| 84 | 93 | } |
| 85 | 94 | |
| 86 | | −/// Evaluate every source-marked declaration in source order. The input is |
| 87 | | −/// consumed, but never observed in a partially transformed state: an error |
| 88 | | −/// returns no Program, and all preparatory specialization happens on a clone. |
| 95 | +/// Evaluate every source-marked declaration in source order without |
| 96 | +/// cancellation. The input is consumed, but never observed in a partially |
| 97 | +/// transformed state: an error returns no Program, and all preparatory |
| 98 | +/// specialization happens on a clone. |
| 89 | 99 | pub fn evaluate_declarations( |
| 90 | 100 | hir: &Hir, |
| 91 | 101 | typeck: &TypeckOutput, |
| 92 | 102 | program: Program, |
| 93 | 103 | ) -> Result<Program, Vec<Diagnostic>> { |
| 94 | | − evaluate_selected(hir, typeck, program, None) |
| 104 | + uncancelled(evaluate_selected(hir, typeck, program, None, &|| false)) |
| 105 | +} |
| 106 | + |
| 107 | +/// [`evaluate_declarations`] with cooperative machine cancellation. |
| 108 | +/// Admission, dependency scanning and `prepare_required` remain synchronous; |
| 109 | +/// the predicate is polled by each root's direct machine. Cancellation returns |
| 110 | +/// no diagnostics or Program and discards all intermediate immutable values. |
| 111 | +pub fn evaluate_declarations_with( |
| 112 | + hir: &Hir, |
| 113 | + typeck: &TypeckOutput, |
| 114 | + program: Program, |
| 115 | + cancelled: &dyn Fn() -> bool, |
| 116 | +) -> Result<Program, CtfeDriverError> { |
| 117 | + evaluate_selected(hir, typeck, program, None, cancelled) |
| 95 | 118 | } |
| 96 | 119 | |
| 97 | 120 | /// Evaluate only the source-marked declarations in `selected`, in their |
| @@ -104,7 +127,38 @@ pub fn evaluate_declarations_for( |
| 104 | 127 | program: Program, |
| 105 | 128 | selected: &BTreeSet<DefId>, |
| 106 | 129 | ) -> Result<Program, Vec<Diagnostic>> { |
| 107 | | − evaluate_selected(hir, typeck, program, Some(selected)) |
| 130 | + uncancelled(evaluate_selected( |
| 131 | + hir, |
| 132 | + typeck, |
| 133 | + program, |
| 134 | + Some(selected), |
| 135 | + &|| false, |
| 136 | + )) |
| 137 | +} |
| 138 | + |
| 139 | +/// [`evaluate_declarations_for`] with cooperative machine cancellation. This |
| 140 | +/// shares the complete selected transaction with [`evaluate_declarations_with`]. |
| 141 | +pub fn evaluate_declarations_for_with( |
| 142 | + hir: &Hir, |
| 143 | + typeck: &TypeckOutput, |
| 144 | + program: Program, |
| 145 | + selected: &BTreeSet<DefId>, |
| 146 | + cancelled: &dyn Fn() -> bool, |
| 147 | +) -> Result<Program, CtfeDriverError> { |
| 148 | + evaluate_selected(hir, typeck, program, Some(selected), cancelled) |
| 149 | +} |
| 150 | + |
| 151 | +fn uncancelled(result: Result<Program, CtfeDriverError>) -> Result<Program, Vec<Diagnostic>> { |
| 152 | + match result { |
| 153 | + Ok(program) => Ok(program), |
| 154 | + Err(CtfeDriverError::Diagnostics(diagnostics)) => Err(diagnostics), |
| 155 | + Err(CtfeDriverError::Cancelled) => { |
| 156 | + // Both callers above pass `&|| false`. B1 creates Cancelled only |
| 157 | + // when that exact predicate returns true, so no source program can |
| 158 | + // reach this branch through the compatibility entry points. |
| 159 | + unreachable!("a never-cancelled CTFE transaction was cancelled") |
| 160 | + } |
| 161 | + } |
| 108 | 162 | } |
| 109 | 163 | |
| 110 | 164 | fn evaluate_selected( |
| @@ -112,7 +166,8 @@ fn evaluate_selected( |
| 112 | 166 | typeck: &TypeckOutput, |
| 113 | 167 | program: Program, |
| 114 | 168 | selected: Option<&BTreeSet<DefId>>, |
| 115 | | −) -> Result<Program, Vec<Diagnostic>> { |
| 169 | + cancelled: &dyn Fn() -> bool, |
| 170 | +) -> Result<Program, CtfeDriverError> { |
| 116 | 171 | let mut roots = marked_roots(hir, &program, selected); |
| 117 | 172 | if roots.is_empty() { |
| 118 | 173 | return Ok(program); |
| @@ -126,7 +181,7 @@ fn evaluate_selected( |
| 126 | 181 | } |
| 127 | 182 | } |
| 128 | 183 | if !diagnostics.is_empty() { |
| 129 | | − return Err(diagnostics); |
| 184 | + return Err(CtfeDriverError::Diagnostics(diagnostics)); |
| 130 | 185 | } |
| 131 | 186 | let ids: Vec<FnId> = roots |
| 132 | 187 | .iter() |
| @@ -140,7 +195,7 @@ fn evaluate_selected( |
| 140 | 195 | prepare_required(&mut prepared, &ids); |
| 141 | 196 | for root in &roots { |
| 142 | 197 | if let Some(diagnostic) = check_dependencies(root, &prepared) { |
| 143 | | − return Err(vec![diagnostic]); |
| 198 | + return Err(CtfeDriverError::Diagnostics(vec![diagnostic])); |
| 144 | 199 | } |
| 145 | 200 | } |
| 146 | 201 | |
| @@ -149,16 +204,16 @@ fn evaluate_selected( |
| 149 | 204 | let mut values = BTreeMap::new(); |
| 150 | 205 | for root in &roots { |
| 151 | 206 | let id = root.id.expect("admitted"); |
| 152 | | − match evaluator.evaluate(id) { |
| 207 | + match evaluator.evaluate_with(id, cancelled) { |
| 153 | 208 | Ok(value) => { |
| 154 | 209 | if let Err(reason) = check_static_layout(&value) { |
| 155 | | − return Err(vec![root.result_error(reason)]); |
| 210 | + return Err(CtfeDriverError::Diagnostics(vec![ |
| 211 | + root.result_error(reason), |
| 212 | + ])); |
| 156 | 213 | } |
| 157 | 214 | values.insert(id, value); |
| 158 | 215 | } |
| 159 | | − Err(error) => { |
| 160 | | − return Err(vec![evaluation_diagnostic(root, error)]); |
| 161 | | − } |
| 216 | + Err(error) => return Err(evaluation_failure(root, error)), |
| 162 | 217 | } |
| 163 | 218 | } |
| 164 | 219 | drop(evaluator); |
| @@ -177,7 +232,9 @@ fn evaluate_selected( |
| 177 | 232 | .iter() |
| 178 | 233 | .find(|root| root.id == Some(*id)) |
| 179 | 234 | .expect("evaluated root"); |
| 180 | | − return Err(vec![root.result_error(static_value_error(error))]); |
| 235 | + return Err(CtfeDriverError::Diagnostics(vec![ |
| 236 | + root.result_error(static_value_error(error)), |
| 237 | + ])); |
| 181 | 238 | } |
| 182 | 239 | }; |
| 183 | 240 | } |
| @@ -430,8 +487,8 @@ fn check_static_layout(value: &louise_core::static_value::StaticValue) -> Result |
| 430 | 487 | Ok(()) |
| 431 | 488 | } |
| 432 | 489 | |
| 433 | | −fn evaluation_diagnostic(root: &MarkedRoot, error: CtfeError) -> Diagnostic { |
| 434 | | − match error { |
| 490 | +fn evaluation_failure(root: &MarkedRoot, error: CtfeError) -> CtfeDriverError { |
| 491 | + let diagnostic = match error { |
| 435 | 492 | CtfeError::CompilationLimit { error, .. } => { |
| 436 | 493 | let code = match error.kind { |
| 437 | 494 | louise_interp::LimitKind::ReifiedNodes | louise_interp::LimitKind::StaticBytes => { |
| @@ -449,11 +506,25 @@ fn evaluation_diagnostic(root: &MarkedRoot, error: CtfeError) -> Diagnostic { |
| 449 | 506 | ) |
| 450 | 507 | } |
| 451 | 508 | CtfeError::Reification(error) => root.result_error(reify_error(error.kind)), |
| 452 | | − CtfeError::Evaluation(error) => direct_error(root, error), |
| 453 | | − } |
| 509 | + CtfeError::Evaluation(error) => { |
| 510 | + return match direct_error(root, error) { |
| 511 | + Ok(diagnostic) => CtfeDriverError::Diagnostics(vec![diagnostic]), |
| 512 | + Err(error) => { |
| 513 | + debug_assert_eq!(error.kind, DirectErrorKind::Cancelled); |
| 514 | + CtfeDriverError::Cancelled |
| 515 | + } |
| 516 | + }; |
| 517 | + } |
| 518 | + }; |
| 519 | + CtfeDriverError::Diagnostics(vec![diagnostic]) |
| 454 | 520 | } |
| 455 | 521 | |
| 456 | | −fn direct_error(root: &MarkedRoot, error: DirectError) -> Diagnostic { |
| 522 | +fn direct_error(root: &MarkedRoot, error: DirectError) -> Result<Diagnostic, DirectError> { |
| 523 | + // Runner cancellation must not acquire E0903 text, source labels, or any |
| 524 | + // other user-facing diagnostic. It is returned as typed control flow. |
| 525 | + if error.kind == DirectErrorKind::Cancelled { |
| 526 | + return Err(error); |
| 527 | + } |
| 457 | 528 | let reason = match &error.kind { |
| 458 | 529 | DirectErrorKind::Runtime(error) => error.to_string(), |
| 459 | 530 | DirectErrorKind::Evaluation(error) => match error { |
| @@ -468,6 +539,10 @@ fn direct_error(root: &MarkedRoot, error: DirectError) -> Diagnostic { |
| 468 | 539 | format!("function {caller} calls missing function {callee}") |
| 469 | 540 | } |
| 470 | 541 | }, |
| 542 | + // The guard above returns this variant before any diagnostic sink. |
| 543 | + DirectErrorKind::Cancelled => { |
| 544 | + unreachable!("cancellation checked before diagnostic mapping") |
| 545 | + } |
| 471 | 546 | DirectErrorKind::Operation { |
| 472 | 547 | name, effect, op, .. |
| 473 | 548 | } => match name { |
| @@ -485,7 +560,7 @@ fn direct_error(root: &MarkedRoot, error: DirectError) -> Diagnostic { |
| 485 | 560 | diagnostic = diagnostic.with_label(span, format!("while evaluating `{}`", frame.name)); |
| 486 | 561 | } |
| 487 | 562 | } |
| 488 | | − diagnostic |
| 563 | + Ok(diagnostic) |
| 489 | 564 | } |
| 490 | 565 | |
| 491 | 566 | fn limit_error(error: LimitError) -> String { |