Skip to content

Commit

Permalink
feat: Add From implementations for Node/NodeMut (#4810)
Browse files Browse the repository at this point in the history
* feat: Add From implementations for Node/NodeMut

Avoids the need to construct the node manually in some cases as into/from can automaticallly do the conversion.

* chore: make generate
  • Loading branch information
Markus Westerlind authored Jun 7, 2022
1 parent b4cd9d3 commit 77dd4ca
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
22 changes: 16 additions & 6 deletions libflux/flux-core/src/semantic/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ impl<'a> Converter<'a> {
ast::Statement::Variable(s) => {
Statement::Variable(Box::new(self.convert_variable_assignment(Some(package), s)))
}
ast::Statement::Bad(s) => Statement::Error(s.base.location.clone()),
ast::Statement::Bad(s) => Statement::Error(BadStmt {
loc: s.base.location.clone(),
}),
}
}

Expand Down Expand Up @@ -757,7 +759,7 @@ impl<'a> Converter<'a> {
Ok(d) => Expression::Duration(d),
Err(err) => {
self.errors.push(err);
Expression::Error(location)
Expression::Error(BadExpr { loc: location })
}
}
}
Expand All @@ -770,9 +772,13 @@ impl<'a> Converter<'a> {
ErrorKind::InvalidPipeLit,
));

Expression::Error(lit.base.location.clone())
Expression::Error(BadExpr {
loc: lit.base.location.clone(),
})
}
ast::Expression::Bad(bad) => Expression::Error(bad.base.location.clone()),
ast::Expression::Bad(bad) => Expression::Error(BadExpr {
loc: bad.base.location.clone(),
}),
}
}

Expand Down Expand Up @@ -919,7 +925,9 @@ impl<'a> Converter<'a> {
.push(located(s.loc().clone(), ErrorKind::MissingReturn));
Block::Return(ReturnStmt {
loc: s.loc().clone(),
argument: Expression::Error(s.loc().clone()),
argument: Expression::Error(BadExpr {
loc: s.loc().clone(),
}),
})
}
None => {
Expand All @@ -929,7 +937,9 @@ impl<'a> Converter<'a> {
));
Block::Return(ReturnStmt {
loc: block.base.location.clone(),
argument: Expression::Error(block.base.location.clone()),
argument: Expression::Error(BadExpr {
loc: block.base.location.clone(),
}),
})
}
};
Expand Down
4 changes: 2 additions & 2 deletions libflux/flux-core/src/semantic/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub fn format(pkg: &semantic::nodes::Package) -> Result<String, Error> {
}

/// Format a `Node`
pub fn format_node(node: walk::Node) -> Result<String, Error> {
pub fn format_node<'a>(node: impl Into<walk::Node<'a>>) -> Result<String, Error> {
let mut formatter = Formatter::default();
formatter.format_node(&node);
formatter.format_node(&node.into());
formatter.output()
}

Expand Down
20 changes: 17 additions & 3 deletions libflux/flux-core/src/semantic/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub enum Statement {
Test(Box<TestStmt>),
TestCase(Box<TestCaseStmt>),
Builtin(BuiltinStmt),
Error(ast::SourceLocation),
Error(BadStmt),
}

impl Statement {
Expand Down Expand Up @@ -295,7 +295,7 @@ pub enum Expression {
DateTime(DateTimeLit),
Regexp(RegexpLit),

Error(ast::SourceLocation),
Error(BadExpr),
}

impl Expression {
Expand Down Expand Up @@ -350,7 +350,7 @@ impl Expression {
Expression::Boolean(lit) => &lit.loc,
Expression::DateTime(lit) => &lit.loc,
Expression::Regexp(lit) => &lit.loc,
Expression::Error(loc) => loc,
Expression::Error(e) => &e.loc,
}
}
fn infer(&mut self, infer: &mut InferState<'_, '_>) -> Result {
Expand Down Expand Up @@ -2006,6 +2006,20 @@ pub fn convert_duration(ast_dur: &[ast::Duration]) -> AnyhowResult<Duration> {
})
}

#[derive(Derivative)]
#[derivative(Debug, PartialEq, Clone)]
#[allow(missing_docs)]
pub struct BadExpr {
pub loc: ast::SourceLocation,
}

#[derive(Derivative)]
#[derivative(Debug, PartialEq, Clone)]
#[allow(missing_docs)]
pub struct BadStmt {
pub loc: ast::SourceLocation,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 5 additions & 4 deletions libflux/flux-core/src/semantic/walk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ macro_rules! mk_node {
) => {

$(#[$attr])*
#[derive(derive_more::From)]
pub enum $name<'a> {
Package(&'a $($mut)? Package),
File(&'a $($mut)? File),
Expand Down Expand Up @@ -44,7 +45,7 @@ macro_rules! mk_node {
BooleanLit(&'a $($mut)? BooleanLit),
DateTimeLit(&'a $($mut)? DateTimeLit),
RegexpLit(&'a $($mut)? RegexpLit),
ErrorExpr(&'a $($mut)? SourceLocation),
ErrorExpr(&'a $($mut)? BadExpr),

// Statements.
ExprStmt(&'a $($mut)? ExprStmt),
Expand All @@ -53,7 +54,7 @@ macro_rules! mk_node {
TestStmt(&'a $($mut)? TestStmt),
TestCaseStmt(&'a $($mut)? TestCaseStmt),
BuiltinStmt(&'a $($mut)? BuiltinStmt),
ErrorStmt(&'a $($mut)? SourceLocation),
ErrorStmt(&'a $($mut)? BadStmt),

// StringExprPart.
TextPart(&'a $($mut)? TextPart),
Expand Down Expand Up @@ -154,14 +155,14 @@ macro_rules! mk_node {
Self::TestStmt(n) => &n.loc,
Self::TestCaseStmt(n) => &n.loc,
Self::BuiltinStmt(n) => &n.loc,
Self::ErrorStmt(loc) => loc,
Self::ErrorStmt(n) => &n.loc,
Self::Block(n) => n.loc(),
Self::Property(n) => &n.loc,
Self::TextPart(n) => &n.loc,
Self::InterpolatedPart(n) => &n.loc,
Self::VariableAssgn(n) => &n.loc,
Self::MemberAssgn(n) => &n.loc,
Self::ErrorExpr(loc) => loc,
Self::ErrorExpr(n) => &n.loc,
}
}

Expand Down
4 changes: 2 additions & 2 deletions libflux/flux-core/src/semantic/walk/walk_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ impl NodeMut<'_> {
NodeMut::BooleanLit(ref mut n) => n.loc = loc,
NodeMut::DateTimeLit(ref mut n) => n.loc = loc,
NodeMut::RegexpLit(ref mut n) => n.loc = loc,
NodeMut::ErrorExpr(ref mut n) => **n = loc,
NodeMut::ErrorExpr(ref mut n) => n.loc = loc,
NodeMut::ExprStmt(ref mut n) => n.loc = loc,
NodeMut::OptionStmt(ref mut n) => n.loc = loc,
NodeMut::ReturnStmt(ref mut n) => n.loc = loc,
NodeMut::TestStmt(ref mut n) => n.loc = loc,
NodeMut::TestCaseStmt(ref mut n) => n.loc = loc,
NodeMut::BuiltinStmt(ref mut n) => n.loc = loc,
NodeMut::ErrorStmt(ref mut n) => **n = loc,
NodeMut::ErrorStmt(ref mut n) => n.loc = loc,
NodeMut::Block(_) => (),
NodeMut::Property(ref mut n) => n.loc = loc,
NodeMut::TextPart(ref mut n) => n.loc = loc,
Expand Down
10 changes: 5 additions & 5 deletions libflux/go/libflux/buildinfo.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ var sourceHashes = map[string]string{
"libflux/flux-core/src/scanner/unicode.rl.COPYING": "6cf2d5d26d52772ded8a5f0813f49f83dfa76006c5f398713be3854fe7bc4c7e",
"libflux/flux-core/src/semantic/bootstrap.rs": "1fc8e72e74f7b08df75b6b9802fcc2e02e5b9d890da82b4877e109ea72cc02fb",
"libflux/flux-core/src/semantic/check.rs": "d0228a0a8176a5360d88cfe48acb1ffd036817b6aaadfadb94af446492603305",
"libflux/flux-core/src/semantic/convert.rs": "82aed0bf2a0d7cf24a3331f233ff945dc35a64b49449fec445117c4c70208b76",
"libflux/flux-core/src/semantic/convert.rs": "122cc098c7606b2f5032e986cc806468a55b55a6476b22bf2150ec4749c8f450",
"libflux/flux-core/src/semantic/env.rs": "8da036aa5f0e09f94fd2461687e97131068e56c762bef8cd98cfd91f36ef3e98",
"libflux/flux-core/src/semantic/flatbuffers/mod.rs": "270671ffdcb1eb5308f9bbab0431c9464df264070a2deb05c526d182a6ec5585",
"libflux/flux-core/src/semantic/flatbuffers/semantic_generated.rs": "beaaa6b08d8b56dba81153a58e440bbdc430b4eca0201a3431e90b793f1adbce",
"libflux/flux-core/src/semantic/flatbuffers/types.rs": "029d51104eb4a0bbfc8d82a470e0f2915a27d7cb00c08a3f35e638b5a3105fc2",
"libflux/flux-core/src/semantic/formatter/mod.rs": "f39a1d127900ea409af7ad5d02b646c66471f31c90bb49359c48ea2bb3bbd5c1",
"libflux/flux-core/src/semantic/formatter/mod.rs": "8dd34520750a39ad242adfbb68c38a170d56bad82d5ccf80b165f0977ea68289",
"libflux/flux-core/src/semantic/fresh.rs": "97238fbc317e7c51836a6ba3441d641d9f4f8c7f637bde4bccbd0e09146129d0",
"libflux/flux-core/src/semantic/fs.rs": "f7f609bc8149769d99b737150e184a2d54029c0b768365dbcf08ff193b0e1f6f",
"libflux/flux-core/src/semantic/import.rs": "184e955211db1ceb1be782b4daf75584b86907b1428e50015497909cfc2dd89a",
"libflux/flux-core/src/semantic/infer.rs": "9d4293f2471a90cc89c1e45cdc72082e0da1a484981b803aea05856e6b4d722f",
"libflux/flux-core/src/semantic/mod.rs": "3cec76c645e411592898a3e153d571787157bdfdf8552c21748c5e8c5e41d48d",
"libflux/flux-core/src/semantic/nodes.rs": "d3514513174e98e015eab3adfdbd82475efe1bce5e069a7880e3dd3b37631126",
"libflux/flux-core/src/semantic/nodes.rs": "23ee2dec99b71f0fe81987528b3dfbd95e89d77a274ccc8a0baa146dea89ad51",
"libflux/flux-core/src/semantic/sub.rs": "a989e50c113ca899fe02f8d49a4744a420580a3f803f656db25beb2d0c2a247b",
"libflux/flux-core/src/semantic/types.rs": "ed414b695e925f18f74984ec88bba652ef8dd8c9e905cb9e8fa19b101a4601b4",
"libflux/flux-core/src/semantic/vectorize.rs": "6ce2dc4e6ff572abc0146a220291322ea88557ce674ae16220a2d67420e9fa0d",
"libflux/flux-core/src/semantic/walk/_walk.rs": "198e6c546f73f78b1de4b963084033a06a6d71e72b0294b16c5e4de874f97a38",
"libflux/flux-core/src/semantic/walk/mod.rs": "a0eab3f225dc294569217da295d44f31073e643073e6b00fec5846cde2382ade",
"libflux/flux-core/src/semantic/walk/mod.rs": "0376a7fca19bf84df1e4aabbd9a8d2f7238161082cffb842a86a525e949950ce",
"libflux/flux-core/src/semantic/walk/test_utils.rs": "b980587707038c420d16b99f99587e69b71f02c32277db37c7e4a094d32f2b38",
"libflux/flux-core/src/semantic/walk/walk_mut.rs": "4dff7aaf3c1904da9ce955aaa33b24c74de41084fee3942eb9b8e960ec448250",
"libflux/flux-core/src/semantic/walk/walk_mut.rs": "7fb1a7c3604062268a6377cdf0150f07f18d621000bb27cbe56d8f005d61fb13",
"libflux/flux/Cargo.toml": "9fae0ee886a5c5278ad6b4606c36d9e929ab9529995856a333a618caae35c210",
"libflux/flux/FLUXDOC.md": "92e6dd8043bd87b4924e09aa28fb5346630aee1214de28ea2c8fc0687cad0785",
"libflux/flux/build.rs": "6f2a4da51744a174ab13a1ebcb18ea39c0acfc2c720e7a61ea3e326fb0649529",
Expand Down

0 comments on commit 77dd4ca

Please sign in to comment.