-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional static typing to functions and structs (#904)
- Loading branch information
1 parent
a833d25
commit 92e6230
Showing
7 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,35 @@ | ||
use crate::ast::prelude::*; | ||
|
||
#[test] | ||
#[cfg(not(miri))] | ||
fn ast_parse() { | ||
rt::<ast::Type>("Bar"); | ||
rt::<ast::Type>("one::two::three::four::Five"); | ||
rt::<ast::Type>("Self"); | ||
rt::<ast::Type>("(one::One, two::Two)"); | ||
rt::<ast::Type>("(one::One, (two::Two, three::Three))"); | ||
} | ||
|
||
/// A type, used for static typing. | ||
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)] | ||
#[non_exhaustive] | ||
pub enum Type { | ||
/// If the type is an identifier or a path. | ||
Path(ast::Path), | ||
/// If the type should return nothing (a.k.a the "never" type in Rust). | ||
Bang(T![!]), | ||
/// If the type is a tuple. | ||
Tuple(ast::Parenthesized<Box<Type>, T![,]>), | ||
} | ||
|
||
impl Parse for Type { | ||
fn parse(p: &mut Parser<'_>) -> Result<Self> { | ||
let segment = match p.nth(0)? { | ||
K![!] => Self::Bang(p.parse()?), | ||
K!['('] => Self::Tuple(p.parse()?), | ||
_ => Self::Path(p.parse()?), | ||
}; | ||
|
||
Ok(segment) | ||
} | ||
} |
This file contains 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 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 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,23 @@ | ||
prelude!(); | ||
|
||
use ErrorKind::*; | ||
|
||
#[test] | ||
fn deny_static_typing_function() { | ||
assert_errors! { | ||
"fn foo() -> Bar {}", | ||
span!(0, 18), Custom { error } => { | ||
assert_eq!(error.to_string(), "Adding a return type in functions is not supported"); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn deny_static_typing_field() { | ||
assert_errors! { | ||
"struct Struct { foo: Bar }", | ||
span!(16, 24), Custom { error } => { | ||
assert_eq!(error.to_string(), "Static typing on fields is not supported"); | ||
} | ||
} | ||
} |