Skip to content
Open
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
17 changes: 17 additions & 0 deletions src/types/checker/builtin_types/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub(crate) fn inject_builtin_throwables(
"TypeError",
"ValueError",
"ArithmeticError",
"UnhandledMatchError",
"Exception",
"RuntimeException",
"JsonException",
Expand Down Expand Up @@ -246,6 +247,22 @@ pub(crate) fn inject_builtin_throwables(
used_traits: Vec::new(),
},
);
class_map.insert(
"UnhandledMatchError".to_string(),
FlattenedClass {
name: "UnhandledMatchError".to_string(),
extends: Some("Error".to_string()),
implements: Vec::new(),
is_abstract: false,
is_final: false,
is_readonly_class: false,
properties: Vec::new(),
methods: Vec::new(),
attributes: Vec::new(),
constants: Vec::new(),
used_traits: Vec::new(),
},
);

// Fiber: cooperative coroutine class. Methods are placeholders here — the
// codegen intercepts every Fiber operation (`new Fiber(...)`, instance
Expand Down
1 change: 1 addition & 0 deletions src/types/checker/builtin_types/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ pub(crate) fn patch_builtin_exception_signatures(checker: &mut Checker) {
"TypeError",
"ValueError",
"ArithmeticError",
"UnhandledMatchError",
"Exception",
"RuntimeException",
"JsonException",
Expand Down
25 changes: 25 additions & 0 deletions tests/codegen/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,28 @@ try { echo $c->foo(); echo 'no'; } catch (Error $e) { echo 'err'; }
);
assert_eq!(out, "err");
}

/// UnhandledMatchError is a registered builtin Error subclass: newable with a message,
/// throwable from a `match` default arm, catchable by its fully-qualified name, and its
/// getMessage() round-trips — matching PHP 8's unhandled-match error type.
#[test]
fn test_unhandled_match_error_throw_and_catch() {
let out = compile_and_run(
r#"<?php
function classify(int $n): string {
return match (true) {
$n < 0 => "neg",
$n === 0 => "zero",
default => throw new UnhandledMatchError("no arm for " . $n),
};
}
echo classify(-1), "|", classify(0), "|";
try {
classify(5);
} catch (\UnhandledMatchError $e) {
echo "caught:", $e->getMessage();
}
"#,
);
assert_eq!(out, "neg|zero|caught:no arm for 5");
}
Loading