Skip to content

Commit

Permalink
Merge branch 'main' into rybickic/refactor-display
Browse files Browse the repository at this point in the history
  • Loading branch information
monadabot authored Aug 24, 2023
2 parents 9e931fb + 1184705 commit d34c7a9
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 104 deletions.
2 changes: 1 addition & 1 deletion docs/docs/02-concepts/01-preflight-and-inflight.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ inflight () => {

## Phase-independent code

The global functions `log`, `assert`, `throw`, and `panic` can all be used in both preflight and inflight code.
The global functions `log`, `assert`, and `throw` can all be used in both preflight and inflight code.

Issue [#435](https://github.com/winglang/wing/issues/435) is tracking support for the capability to define phase-independent functions.

Expand Down
13 changes: 1 addition & 12 deletions docs/docs/03-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,11 @@ log("UTC: ${t1.utc.toIso())}"); // output: 2023-02-09T06:21:03.000Z
| -------- | -------------------------------------------------------- |
| `log` | logs str |
| `throw` | creates and throws an instance of an exception |
| `panic` | exits with a serializable, dumps the trace + a core dump |
| `assert` | checks a condition and _panics_ if evaluated to false |
`panic` is a fatal call by design. If the intention is error handling, panic is the
last resort. Exceptions are non fatal and should be used instead for effectively
communicating errors to the user.
| `assert` | checks a condition and _throws_ if evaluated to false |
> ```TS
> log("Hello ${name}");
> throw("a recoverable error occurred");
> panic("a fatal error encountered");
> assert(x > 0);
> ```
Expand Down Expand Up @@ -969,13 +963,8 @@ translate to JavaScript. You can create a new exception with a `throw` call.
In the presence of `try`, both `catch` and `finally` are optional but at least one of them must be present.
In the presence of `catch` the variable holding the exception (`e` in the example below) is optional.
`panic` is meant to be fatal error handling.
`throw` is meant to be recoverable error handling.
An uncaught exception is considered user error but a panic call is not. Compiler
guarantees exception safety by throwing a compile error if an exception is
expected from a call and it is not being caught.
> ```TS
> try {
> let x: num? = 1;
Expand Down
3 changes: 1 addition & 2 deletions examples/tests/error/utilities.w
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
assert(false);
log("W");
throw("me");
panic("L");
throw("me");
6 changes: 6 additions & 0 deletions examples/tests/valid/optionals.w
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ if let var z = a {
assert(z == 2);
}

// extra space between if and let
let b: num? = 1;
if let z = b {
assert(z == 1);
}

// Nested if lets
if let parsedName = tryParseName("Good Name") {
assert(parsedName.first == "Good");
Expand Down
3 changes: 2 additions & 1 deletion libs/tree-sitter-wing/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ module.exports = grammar({

if_let_statement: ($) =>
seq(
"if let",
"if",
"let",
optional(field("reassignable", $.reassignable)),
field("name", $.identifier),
"=",
Expand Down
9 changes: 1 addition & 8 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,28 +304,21 @@ pub struct Stmt {
#[derive(Debug)]
pub enum UtilityFunctions {
Log,
Panic,
Throw,
Assert,
}

impl UtilityFunctions {
/// Returns all utility functions.
pub fn all() -> Vec<UtilityFunctions> {
vec![
UtilityFunctions::Log,
UtilityFunctions::Panic,
UtilityFunctions::Throw,
UtilityFunctions::Assert,
]
vec![UtilityFunctions::Log, UtilityFunctions::Throw, UtilityFunctions::Assert]
}
}

impl Display for UtilityFunctions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UtilityFunctions::Log => write!(f, "log"),
UtilityFunctions::Panic => write!(f, "panic"),
UtilityFunctions::Throw => write!(f, "throw"),
UtilityFunctions::Assert => write!(f, "assert"),
}
Expand Down
20 changes: 1 addition & 19 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub unsafe extern "C" fn wingc_free(ptr: *mut u8, size: usize) {
/// should be called before any other function
#[no_mangle]
pub unsafe extern "C" fn wingc_init() {
// Setup a custom panic hook to report panics as complitation diagnostics
// Setup a custom panic hook to report panics as compilation diagnostics
set_custom_panic_hook();
}

Expand Down Expand Up @@ -260,24 +260,6 @@ pub fn type_check(
scope,
types,
);
add_builtin(
UtilityFunctions::Panic.to_string().as_str(),
Type::Function(FunctionSignature {
this_type: None,
parameters: vec![FunctionParameter {
typeref: types.string(),
name: "message".into(),
docs: Docs::with_summary("The message to panic with"),
variadic: false,
}],
return_type: types.void(),
phase: Phase::Independent,
js_override: Some("{((msg) => {console.error(msg, (new Error()).stack);process.exit(1)})($args$)}".to_string()),
docs: Docs::with_summary("panics with an error"),
}),
scope,
types,
);

let mut scope_env = types.get_scope_env(&scope);
let mut tc = TypeChecker::new(types, file_path, jsii_types, jsii_imports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ source: libs/wingc/src/lsp/completions.rs
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: panic
kind: 3
detail: "(message: str): void"
documentation:
kind: markdown
value: "```wing\npanic: (message: str): void\n```\n---\npanics with an error\n\n### Parameters\n- `message` — The message to panic with"
sortText: cc|panic
insertText: panic($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: throw
kind: 3
detail: "(message: str): void"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ source: libs/wingc/src/lsp/completions.rs
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: panic
kind: 3
detail: "(message: str): void"
documentation:
kind: markdown
value: "```wing\npanic: (message: str): void\n```\n---\npanics with an error\n\n### Parameters\n- `message` — The message to panic with"
sortText: cc|panic
insertText: panic($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: throw
kind: 3
detail: "(message: str): void"
Expand Down
12 changes: 0 additions & 12 deletions libs/wingc/src/lsp/snapshots/completions/empty.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ source: libs/wingc/src/lsp/completions.rs
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: panic
kind: 3
detail: "(message: str): void"
documentation:
kind: markdown
value: "```wing\npanic: (message: str): void\n```\n---\npanics with an error\n\n### Parameters\n- `message` — The message to panic with"
sortText: cc|panic
insertText: panic($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: throw
kind: 3
detail: "(message: str): void"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ source: libs/wingc/src/lsp/completions.rs
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: panic
kind: 3
detail: "(message: str): void"
documentation:
kind: markdown
value: "```wing\npanic: (message: str): void\n```\n---\npanics with an error\n\n### Parameters\n- `message` — The message to panic with"
sortText: cc|panic
insertText: panic($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: throw
kind: 3
detail: "(message: str): void"
Expand Down
12 changes: 0 additions & 12 deletions libs/wingc/src/lsp/snapshots/completions/struct_literal_value.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ source: libs/wingc/src/lsp/completions.rs
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: panic
kind: 3
detail: "(message: str): void"
documentation:
kind: markdown
value: "```wing\npanic: (message: str): void\n```\n---\npanics with an error\n\n### Parameters\n- `message` — The message to panic with"
sortText: cc|panic
insertText: panic($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: throw
kind: 3
detail: "(message: str): void"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```log
[symbol environment at ../../../../examples/tests/valid/debug_env.w:7:5]
level 0: { this => A }
level 1: { A => A [type], assert => (condition: bool): void, cloud => cloud [namespace], log => (message: str): void, panic => (message: str): void, std => std [namespace], throw => (message: str): void }
level 1: { A => A [type], assert => (condition: bool): void, cloud => cloud [namespace], log => (message: str): void, std => std [namespace], throw => (message: str): void }
pass ─ debug_env.wsim (no tests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ class $Root extends $stdlib.std.Resource {
{((cond) => {if (!cond) throw new Error("assertion failed: z == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(z,2)))};
}
}
const b = 1;
{
const $IF_LET_VALUE = b;
if ($IF_LET_VALUE != undefined) {
const z = $IF_LET_VALUE;
{((cond) => {if (!cond) throw new Error("assertion failed: z == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(z,1)))};
}
}
{
const $IF_LET_VALUE = (tryParseName("Good Name"));
if ($IF_LET_VALUE != undefined) {
Expand Down

0 comments on commit d34c7a9

Please sign in to comment.