From 01a08af00d0e0daf47161880dc3ed34b8190cd41 Mon Sep 17 00:00:00 2001 From: Uri Bar <106860404+staycoolcall911@users.noreply.github.com> Date: Fri, 25 Aug 2023 00:46:48 +0300 Subject: [PATCH 1/2] chore(compiler)!: remove panic built-in method (#3950) > Don't panic! Stay cool... This minor breaking change was staring at me for too long - better remove `panic()` before anyone starts using it. Closes #2055 ## Checklist - [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [ ] Description explains motivation and solution - [ ] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- .../02-concepts/01-preflight-and-inflight.md | 2 +- docs/docs/03-language-reference.md | 13 +----------- examples/tests/error/utilities.w | 3 +-- libs/wingc/src/ast.rs | 9 +-------- libs/wingc/src/lib.rs | 20 +------------------ .../completions/call_struct_expansion.snap | 12 ----------- .../call_struct_expansion_partial.snap | 12 ----------- .../src/lsp/snapshots/completions/empty.snap | 12 ----------- .../only_show_symbols_in_scope.snap | 12 ----------- .../completions/struct_literal_value.snap | 12 ----------- .../test_corpus/valid/debug_env.w_test_sim.md | 2 +- 11 files changed, 6 insertions(+), 103 deletions(-) diff --git a/docs/docs/02-concepts/01-preflight-and-inflight.md b/docs/docs/02-concepts/01-preflight-and-inflight.md index de5f99c7468..cc0c4b2b884 100644 --- a/docs/docs/02-concepts/01-preflight-and-inflight.md +++ b/docs/docs/02-concepts/01-preflight-and-inflight.md @@ -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. diff --git a/docs/docs/03-language-reference.md b/docs/docs/03-language-reference.md index 72ed26e99c7..69fc22a739e 100644 --- a/docs/docs/03-language-reference.md +++ b/docs/docs/03-language-reference.md @@ -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); > ``` @@ -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; diff --git a/examples/tests/error/utilities.w b/examples/tests/error/utilities.w index 0a422904368..6c324a4ee97 100644 --- a/examples/tests/error/utilities.w +++ b/examples/tests/error/utilities.w @@ -1,4 +1,3 @@ assert(false); log("W"); -throw("me"); -panic("L"); +throw("me"); \ No newline at end of file diff --git a/libs/wingc/src/ast.rs b/libs/wingc/src/ast.rs index c3c203b0443..4c228b5bbbd 100644 --- a/libs/wingc/src/ast.rs +++ b/libs/wingc/src/ast.rs @@ -304,7 +304,6 @@ pub struct Stmt { #[derive(Debug)] pub enum UtilityFunctions { Log, - Panic, Throw, Assert, } @@ -312,12 +311,7 @@ pub enum UtilityFunctions { impl UtilityFunctions { /// Returns all utility functions. pub fn all() -> Vec { - vec![ - UtilityFunctions::Log, - UtilityFunctions::Panic, - UtilityFunctions::Throw, - UtilityFunctions::Assert, - ] + vec![UtilityFunctions::Log, UtilityFunctions::Throw, UtilityFunctions::Assert] } } @@ -325,7 +319,6 @@ 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"), } diff --git a/libs/wingc/src/lib.rs b/libs/wingc/src/lib.rs index c695ff46803..8dedec5ab1c 100644 --- a/libs/wingc/src/lib.rs +++ b/libs/wingc/src/lib.rs @@ -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(); } @@ -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); diff --git a/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion.snap b/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion.snap index 0f1c0bd004a..eaec35e6afc 100644 --- a/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion.snap +++ b/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion.snap @@ -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" diff --git a/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion_partial.snap b/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion_partial.snap index 76f6c9f4596..2fa5ecc4b5f 100644 --- a/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion_partial.snap +++ b/libs/wingc/src/lsp/snapshots/completions/call_struct_expansion_partial.snap @@ -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" diff --git a/libs/wingc/src/lsp/snapshots/completions/empty.snap b/libs/wingc/src/lsp/snapshots/completions/empty.snap index 01abaae3093..a16c46c8443 100644 --- a/libs/wingc/src/lsp/snapshots/completions/empty.snap +++ b/libs/wingc/src/lsp/snapshots/completions/empty.snap @@ -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" diff --git a/libs/wingc/src/lsp/snapshots/completions/only_show_symbols_in_scope.snap b/libs/wingc/src/lsp/snapshots/completions/only_show_symbols_in_scope.snap index 6387b3c3862..9bb6c11d82a 100644 --- a/libs/wingc/src/lsp/snapshots/completions/only_show_symbols_in_scope.snap +++ b/libs/wingc/src/lsp/snapshots/completions/only_show_symbols_in_scope.snap @@ -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" diff --git a/libs/wingc/src/lsp/snapshots/completions/struct_literal_value.snap b/libs/wingc/src/lsp/snapshots/completions/struct_literal_value.snap index 6c1c26d4a38..61505c5c11d 100644 --- a/libs/wingc/src/lsp/snapshots/completions/struct_literal_value.snap +++ b/libs/wingc/src/lsp/snapshots/completions/struct_literal_value.snap @@ -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" diff --git a/tools/hangar/__snapshots__/test_corpus/valid/debug_env.w_test_sim.md b/tools/hangar/__snapshots__/test_corpus/valid/debug_env.w_test_sim.md index 43946a08247..15946e9ff87 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/debug_env.w_test_sim.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/debug_env.w_test_sim.md @@ -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) From 1184705bd916fbaa02a4cf1c8dad1bca097e6f68 Mon Sep 17 00:00:00 2001 From: Marcio Cruz de Almeida <67694075+marciocadev@users.noreply.github.com> Date: Thu, 24 Aug 2023 18:55:12 -0300 Subject: [PATCH 2/2] fix(compiler): error when more than one space is placed between `if` and `let` (#3960) @MarkMcCulloh, your review on PR #3888 made me realize that if I put more than one space between `if let`, it generates errors. I've updated the `if let` to work even with extra spaces. Closes #3959 ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- examples/tests/valid/optionals.w | 6 ++++++ libs/tree-sitter-wing/grammar.js | 3 ++- .../test_corpus/valid/optionals.w_compile_tf-aws.md | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/tests/valid/optionals.w b/examples/tests/valid/optionals.w index 0ca09eb0423..deacf2a1216 100644 --- a/examples/tests/valid/optionals.w +++ b/examples/tests/valid/optionals.w @@ -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"); diff --git a/libs/tree-sitter-wing/grammar.js b/libs/tree-sitter-wing/grammar.js index c9d8ae40ad5..094b038aa9a 100644 --- a/libs/tree-sitter-wing/grammar.js +++ b/libs/tree-sitter-wing/grammar.js @@ -274,7 +274,8 @@ module.exports = grammar({ if_let_statement: ($) => seq( - "if let", + "if", + "let", optional(field("reassignable", $.reassignable)), field("name", $.identifier), "=", diff --git a/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md index 4fb5ec1e07c..05ceea75c62 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md @@ -476,6 +476,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) {