-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add pub attribute for struct fields #232
base: main
Are you sure you want to change the base?
Changes from all commits
90903bb
3ba79be
c39bcde
ac98569
f811633
e472009
5eebfa9
48e4e5a
8085c27
99fc9cc
416884f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
struct Thing { | ||
xx: Field, | ||
pub xx: Field, | ||
} | ||
|
||
fn try_to_mutate(thing: Thing) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
struct Thing { | ||
xx: Field, | ||
yy: Field, | ||
pub xx: Field, | ||
pub yy: Field, | ||
} | ||
|
||
fn main(pub xx: Field, pub yy: Field) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
struct Thing { | ||
xx: Field, | ||
yy: Field, | ||
pub xx: Field, | ||
pub yy: Field, | ||
} | ||
|
||
fn main(pub xx: Field, pub yy: Field) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,12 @@ use serde::{Deserialize, Serialize}; | |
use crate::{ | ||
constants::Span, | ||
error::{ErrorKind, Result}, | ||
lexer::{Token, TokenKind, Tokens}, | ||
lexer::{Keyword, Token, TokenKind, Tokens}, | ||
syntax::is_type, | ||
}; | ||
|
||
use super::{ | ||
types::{Ident, ModulePath, Ty, TyKind}, | ||
types::{Attribute, AttributeKind, Ident, ModulePath, Ty, TyKind}, | ||
Error, ParserCtx, | ||
}; | ||
|
||
|
@@ -17,7 +17,7 @@ pub struct StructDef { | |
//pub attribute: Attribute, | ||
pub module: ModulePath, // name resolution | ||
pub name: CustomType, | ||
pub fields: Vec<(Ident, Ty)>, | ||
pub fields: Vec<(Ident, Ty, Option<Attribute>)>, | ||
pub span: Span, | ||
} | ||
|
||
|
@@ -55,6 +55,26 @@ impl StructDef { | |
tokens.bump(ctx); | ||
break; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. realized looking at this code that we can write an empty struct (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at the same time it seems OK to allow it... |
||
// check for pub keyword | ||
// struct Foo { pub a: Field, b: Field } | ||
// ^ | ||
let attribute = if matches!( | ||
tokens.peek(), | ||
Some(Token { | ||
kind: TokenKind::Keyword(Keyword::Pub), | ||
.. | ||
}) | ||
) { | ||
let token = tokens.bump(ctx).unwrap(); | ||
Some(Attribute { | ||
kind: AttributeKind::Pub, | ||
span: token.span, | ||
}) | ||
} else { | ||
None | ||
}; | ||
|
||
// struct Foo { a: Field, b: Field } | ||
// ^ | ||
let field_name = Ident::parse(ctx, tokens)?; | ||
|
@@ -67,7 +87,7 @@ impl StructDef { | |
// ^^^^^ | ||
let field_ty = Ty::parse(ctx, tokens)?; | ||
span = span.merge_with(field_ty.span); | ||
fields.push((field_name, field_ty)); | ||
fields.push((field_name, field_ty, attribute)); | ||
|
||
// struct Foo { a: Field, b: Field } | ||
// ^ ^ | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,7 +5,7 @@ use std::collections::HashMap; | |||
use crate::{ | ||||
constants::Span, | ||||
error::{Error, ErrorKind, Result}, | ||||
parser::types::TyKind, | ||||
parser::types::{FuncOrMethod, TyKind}, | ||||
}; | ||||
|
||||
/// Some type information on local variables that we want to track in the [TypedFnEnv] environment. | ||||
|
@@ -39,7 +39,7 @@ impl TypeInfo { | |||
} | ||||
|
||||
/// The environment we use to type check functions. | ||||
#[derive(Default, Debug, Clone)] | ||||
#[derive(Debug, Clone)] | ||||
pub struct TypedFnEnv { | ||||
/// The current nesting level. | ||||
/// Starting at 0 (top level), and increasing as we go into a block. | ||||
|
@@ -55,12 +55,21 @@ pub struct TypedFnEnv { | |||
|
||||
/// Determines if forloop variables are allowed to be accessed. | ||||
forbid_forloop_scope: bool, | ||||
|
||||
/// The kind of function we're currently type checking | ||||
current_fn_kind: FuncOrMethod, | ||||
} | ||||
|
||||
impl TypedFnEnv { | ||||
/// Creates a new TypeEnv | ||||
pub fn new() -> Self { | ||||
Self::default() | ||||
/// Creates a new TypeEnv with the given function kind | ||||
pub fn new(fn_kind: &FuncOrMethod) -> Self { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sure there is a more elegant way of doing this. The reason I went with this is that since current_fn_kind is not an option, calling Self::default() calls the default constructor of FuncOrMethod as well, which calls unreachable!: Line 739 in b17ea20
How can we do it better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I didn't fully understand the last part. Do you mean the default() for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant default() should be removed, if it is not used anymore. |
||||
Self { | ||||
current_scope: 0, | ||||
vars: HashMap::new(), | ||||
forloop_scopes: Vec::new(), | ||||
forbid_forloop_scope: false, | ||||
current_fn_kind: fn_kind.clone(), | ||||
} | ||||
} | ||||
|
||||
/// Enters a scoped block. | ||||
|
@@ -182,4 +191,8 @@ impl TypedFnEnv { | |||
Ok(None) | ||||
} | ||||
} | ||||
|
||||
pub fn current_fn_kind(&self) -> &FuncOrMethod { | ||||
&self.current_fn_kind | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can just test the negative case here. Then the positive tests should cover the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree ^ we can simplify the test