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
1 change: 1 addition & 0 deletions src/autoload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const BUILTIN_CLASS_LIKE_NAMES: &[&str] = &[
"EmptyIterator",
"Error",
"ArithmeticError",
"UnhandledMatchError",
"Exception",
"Fiber",
"FiberError",
Expand Down
20 changes: 20 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,25 @@ pub(crate) fn inject_builtin_throwables(
used_traits: Vec::new(),
},
);
// Thrown by a `match` expression when no arm matches and no `default` arm is present. Codegen
// materializes it at the implicit no-match throw, so it must be a declared builtin subclass of
// Error for the checker to resolve the type reference.
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
23 changes: 23 additions & 0 deletions tests/codegen/control_flow/branches_and_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,26 @@ fn test_while_null_no_loop() {
}

// --- Ternary operator ---

// --- match without default (UnhandledMatchError) ---

/// Regression: a `match` with no `default` arm references the builtin `UnhandledMatchError` class
/// (thrown at the implicit no-match point), so that class must be a declared builtin subclass of
/// `Error`. Before it was declared, any default-less `match` failed with
/// "Undefined class: UnhandledMatchError". The happy path (a matching arm) compiles and runs.
#[test]
fn test_match_without_default_compiles() {
let out = compile_and_run(
r#"<?php
declare(strict_types=1);
function classify(int $x): string {
return match ($x) {
1 => "one",
2 => "two",
};
}
echo classify(1), classify(2);
"#,
);
assert_eq!(out, "onetwo");
}
Loading