-
Notifications
You must be signed in to change notification settings - Fork 100
Add support for failable mark to function declarations #300
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb6bc37
test: Add tests for functions with return types
mks-h acc701c
feat: Introduce Failable type
mks-h 2c70f71
feat: Require only failable functions to always return Failable type
mks-h 5c249a5
fix: Disallow declaring Failable types in function arguments
mks-h f2ff4cd
fix: Make return statement accept non-Failable versions of type
mks-h b126cdc
fix: Make it absurd to cast Failable and non-Failable types
mks-h 49b82dd
fix: Unwrap Failable type by handling failure
mks-h efdfd8e
fix: Make commands always return failable "Text?"
mks-h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::compiler::AmberCompiler; | ||
use crate::Cli; | ||
use crate::test_amber; | ||
|
||
#[test] | ||
#[should_panic(expected = "ERROR: Return type does not match function return type")] | ||
fn function_with_wrong_typed_return() { | ||
let code = r#" | ||
pub fun test(): Num { | ||
return "Hello, World!" | ||
} | ||
echo test() | ||
"#; | ||
|
||
test_amber!(code, "Hello, World!"); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "ERROR: Failable functions must return a Failable type")] | ||
fn function_failable_with_typed_nonfailable_return() { | ||
let code = r#" | ||
pub fun test(): Null { | ||
fail 1 | ||
} | ||
echo test() failed: echo "Failed" | ||
"#; | ||
|
||
test_amber!(code, "Failed"); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "ERROR: Non-failable functions cannot return a Failable type")] | ||
fn function_nonfailable_with_typed_failable_return() { | ||
let code = r#" | ||
pub fun test(): Null? { | ||
echo "Hello, World!" | ||
} | ||
echo test() failed: echo "Failed" | ||
"#; | ||
|
||
test_amber!(code, "Hello, World!"); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "ERROR: Failable types cannot be used as arguments")] | ||
fn function_with_failable_typed_arg() { | ||
let code = r#" | ||
pub fun test(a: Text?) { | ||
echo a | ||
} | ||
test("Hello, World!") | ||
"#; | ||
|
||
test_amber!(code, "Hello, World!"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use crate::Cli; | |
|
||
pub mod cli; | ||
pub mod formatter; | ||
pub mod errors; | ||
pub mod stdlib; | ||
pub mod validity; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fun failable(): Num? { | ||
if 0 > 5 { | ||
fail 1 | ||
} | ||
|
||
return 1 | ||
} | ||
|
||
let a = failable() failed: echo "Failed" | ||
|
||
if a is Num: echo "Succeded" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fun failable(): Num? { | ||
if 0 > 5 { | ||
fail 1 | ||
} | ||
|
||
return 1 | ||
} | ||
|
||
let a = failable() failed: echo "Failed" | ||
|
||
if a is Num: echo "Succeded" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fun test() { | ||
if 0 < 5 { | ||
fail 1 | ||
} | ||
|
||
return 42 | ||
} | ||
|
||
test() failed: echo "Succeded" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pub fun test(): Num? { | ||
if 0 < 5 { | ||
fail 1 | ||
} | ||
|
||
return 42 | ||
} | ||
|
||
echo test() failed: echo "Succeded" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub fun test(): Text { | ||
return "Succeded" | ||
} | ||
|
||
echo test() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fun test(): Num? { | ||
if 0 < 5 { | ||
fail 1 | ||
} | ||
|
||
return 42 | ||
} | ||
|
||
if unsafe test() is Num: echo "Succeded" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.