diff --git a/builtin/intrinsics.mbt b/builtin/intrinsics.mbt index f92912913..5557f0878 100644 --- a/builtin/intrinsics.mbt +++ b/builtin/intrinsics.mbt @@ -62,34 +62,10 @@ pub fn ignore[T](t : T) -> Unit = "%ignore" pub fn physical_equal[T](a : T, b : T) -> Bool = "%refeq" ///| -/// Aborts the program with an error message. Always causes a panic, regardless -/// of the message provided. -/// -/// Parameters: -/// -/// * `message` : A string containing the error message to be displayed when -/// aborting. -/// -/// Returns a value of type `T`. However, this function never actually returns a -/// value as it always causes a panic. -/// -/// Example: -/// -/// ```moonbit -/// test "panic abort/with_message" { -/// let x : Int = abort("Something went wrong") // specify return type as Int -/// ignore(x) -/// } -/// ``` -pub fn abort[T](msg : String) -> T { - ignore(msg) - panic() -} +pub fnalias @panic.panic ///| -pub fn panic[T]() -> T = "%panic" - -// Bool primitive ops +pub fnalias @panic.abort ///| /// Performs logical negation on a boolean value. diff --git a/builtin/moon.pkg.json b/builtin/moon.pkg.json index 7d3ae2117..04adbefb1 100644 --- a/builtin/moon.pkg.json +++ b/builtin/moon.pkg.json @@ -1,5 +1,7 @@ { - "import": [], + "import": [ + "moonbitlang/core/panic" + ], "test-import": [ "moonbitlang/core/char", "moonbitlang/core/string", diff --git a/intrinsics/intrinsics.mbt b/intrinsics/intrinsics.mbt new file mode 100644 index 000000000..7e0d007ce --- /dev/null +++ b/intrinsics/intrinsics.mbt @@ -0,0 +1,16 @@ +// Copyright 2025 International Digital Economy Academy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +///| +pub fn unreachable[T]() -> T = "%panic" diff --git a/intrinsics/intrinsics.mbti b/intrinsics/intrinsics.mbti new file mode 100644 index 000000000..b47a743c1 --- /dev/null +++ b/intrinsics/intrinsics.mbti @@ -0,0 +1,11 @@ +package "moonbitlang/core/intrinsics" + +// Values +fn unreachable[T]() -> T + +// Types and methods + +// Type aliases + +// Traits + diff --git a/intrinsics/moon.pkg.json b/intrinsics/moon.pkg.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/intrinsics/moon.pkg.json @@ -0,0 +1 @@ +{} diff --git a/panic/moon.pkg.json b/panic/moon.pkg.json new file mode 100644 index 000000000..6c25e3cfc --- /dev/null +++ b/panic/moon.pkg.json @@ -0,0 +1,8 @@ +{ + "import": [ + "moonbitlang/core/intrinsics" + ], + "virtual": { + "has-default": true + } +} diff --git a/panic/panic.mbt b/panic/panic.mbt new file mode 100644 index 000000000..66c16812a --- /dev/null +++ b/panic/panic.mbt @@ -0,0 +1,28 @@ +// Copyright 2025 International Digital Economy Academy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +///| +/// Aborts the program. Always causes a panic, regardless of the message provided. +/// +/// Returns a value of type `T`. However, this function never actually returns a +/// value as it always causes a panic. +pub fn abort[T](msg : String) -> T { + let _ = msg + panic() +} + +///| +pub fn panic[T]() -> T { + @intrinsics.unreachable() +} diff --git a/panic/panic.mbti b/panic/panic.mbti new file mode 100644 index 000000000..dfd9ea053 --- /dev/null +++ b/panic/panic.mbti @@ -0,0 +1,13 @@ +package "moonbitlang/core/panic" + +// Values +fn abort[T](String) -> T + +fn panic[T]() -> T + +// Types and methods + +// Type aliases + +// Traits +