Skip to content

internal: add virtual pkg "moonbitlang/core/panic" #2016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
28 changes: 2 additions & 26 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion builtin/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"import": [],
"import": [
"moonbitlang/core/panic"
],
"test-import": [
"moonbitlang/core/char",
"moonbitlang/core/string",
Expand Down
16 changes: 16 additions & 0 deletions intrinsics/intrinsics.mbt
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions intrinsics/intrinsics.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package "moonbitlang/core/intrinsics"

// Values
fn unreachable[T]() -> T

// Types and methods

// Type aliases

// Traits

1 change: 1 addition & 0 deletions intrinsics/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
8 changes: 8 additions & 0 deletions panic/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"import": [
"moonbitlang/core/intrinsics"
],
"virtual": {
"has-default": true
}
}
28 changes: 28 additions & 0 deletions panic/panic.mbt
Original file line number Diff line number Diff line change
@@ -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()
}
13 changes: 13 additions & 0 deletions panic/panic.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package "moonbitlang/core/panic"

// Values
fn abort[T](String) -> T

fn panic[T]() -> T

// Types and methods

// Type aliases

// Traits

Loading