Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions forc-plugins/forc-migrate/src/visiting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use sway_core::{
ty::{
TyAbiDecl, TyAstNodeContent, TyCodeBlock, TyDecl, TyExpression, TyExpressionVariant,
TyFunctionDecl, TyImplSelfOrTrait, TyIntrinsicFunctionKind, TyModule,
TyReassignmentTarget, TySideEffect, TySideEffectVariant, TyStorageDecl, TyStorageField,
TyStructDecl, TyTraitDecl, TyTraitItem, TyUseStatement, TyVariableDecl,
TyReassignmentTarget, TyStatement, TyStorageDecl, TyStorageField, TyStructDecl,
TyTraitDecl, TyTraitItem, TyUseStatement, TyVariableDecl,
},
CallPath,
},
Expand Down Expand Up @@ -369,9 +369,11 @@ impl __ProgramVisitor {
.all_nodes
.iter()
.find_map(|node| match &node.content {
TyAstNodeContent::SideEffect(TySideEffect {
side_effect: TySideEffectVariant::UseStatement(ty_use),
}) if ty_use.span == item_use.span() => Some(ty_use),
TyAstNodeContent::Statement(TyStatement::Use(ty_use))
if ty_use.span == item_use.span() =>
{
Some(ty_use)
}
_ => None,
})
});
Expand Down Expand Up @@ -818,15 +820,19 @@ impl __ProgramVisitor {
// TODO: Implement visiting `annotations`.
match __ref([annotated.value]) {
ItemKind::Use(item_use) => {
let ty_use = ty_node.map(|ty_node|
match &ty_node.content {
TyAstNodeContent::SideEffect(ty_side_effect) => match &ty_side_effect.side_effect {
TySideEffectVariant::UseStatement(ty_use) => Ok(ty_use),
_ => bail!(internal_error("`ItemKind::Use` must correspond to a `TySideEffectVariant::UseStatement`.")),
},
_ => bail!(internal_error("`ItemKind::Use` must correspond to a `TyAstNodeContent::SideEffect`.")),
}
).transpose()?;
let ty_use = ty_node
.map(|ty_node| match &ty_node.content {
TyAstNodeContent::Statement(TyStatement::Use(ty_use)) => {
Ok(ty_use)
}
TyAstNodeContent::Statement(_) => bail!(internal_error(
"`ItemKind::Use` must correspond to a `TyStatement::Use`.",
)),
_ => bail!(internal_error(
"`ItemKind::Use` must correspond to a `TyAstNodeContent::Statement`.",
)),
})
.transpose()?;

visitor.visit_use(ctx, item_use, ty_use, output)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn connect_node<'eng: 'cfg, 'cfg>(
}
Ok(NodeConnection::NextStep(Some(entry)))
}
ty::TyAstNodeContent::SideEffect(_) => Ok(NodeConnection::NextStep(leaf_opt)),
ty::TyAstNodeContent::Statement(_) => Ok(NodeConnection::NextStep(leaf_opt)),
ty::TyAstNodeContent::Declaration(decl) => Ok(NodeConnection::NextStep(
connect_declaration(engines, node, decl, graph, leaf_opt)?,
)),
Expand Down
10 changes: 7 additions & 3 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ fn connect_node<'eng: 'cfg, 'cfg>(
},
)
}
ty::TyAstNodeContent::SideEffect(_) => (leaves.to_vec(), exit_node),
ty::TyAstNodeContent::Statement(_) => (leaves.to_vec(), exit_node),
ty::TyAstNodeContent::Declaration(decl) => {
// all leaves connect to this node, then this node is the singular leaf
let cfg_node: ControlFlowGraphNode =
Expand Down Expand Up @@ -2435,6 +2435,10 @@ fn construct_dead_code_warning_from_node(
content: ty::TyAstNodeContent::Declaration(ty::TyDecl::AbiDecl(_)),
..
} => return None,
ty::TyAstNode {
content: ty::TyAstNodeContent::Statement(ty::TyStatement::Let(_)),
..
} => return None,
// We handle storage fields individually. There is no need to emit any warnings for the
// storage declaration itself.
ty::TyAstNode {
Expand All @@ -2456,7 +2460,7 @@ fn construct_dead_code_warning_from_node(
// Otherwise, this is unreachable.
ty::TyAstNode {
span,
content: ty::TyAstNodeContent::Expression(_) | ty::TyAstNodeContent::SideEffect(_),
content: ty::TyAstNodeContent::Expression(_) | ty::TyAstNodeContent::Statement(_),
} => CompileWarning {
span: span.clone(),
warning_content: Warning::UnreachableCode,
Expand Down Expand Up @@ -2629,7 +2633,7 @@ fn allow_dead_code_ast_node(decl_engine: &DeclEngine, node: &ty::TyAstNode) -> b
ty::TyDecl::StorageDecl { .. } => false,
},
ty::TyAstNodeContent::Expression(_) => false,
ty::TyAstNodeContent::SideEffect(_) => false,
ty::TyAstNodeContent::Statement(_) => false,
ty::TyAstNodeContent::Error(_, _) => false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ fn const_eval_codeblock(
}
}
},
ty::TyAstNodeContent::SideEffect(_) => Err(ConstEvalError::CannotBeEvaluatedToConst {
ty::TyAstNodeContent::Statement(_) => Err(ConstEvalError::CannotBeEvaluatedToConst {
span: ast_node.span.clone(),
}),
ty::TyAstNodeContent::Error(_, _) => Err(ConstEvalError::CannotBeEvaluatedToConst {
Expand Down
4 changes: 1 addition & 3 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ impl<'a> FnCompiler<'a> {
ty::TyDecl::EnumVariantDecl { .. } => unexpected_decl("enum variant"),
ty::TyDecl::TraitTypeDecl { .. } => unexpected_decl("trait type"),
},
ty::TyAstNodeContent::Statement(_) => Ok(None),
ty::TyAstNodeContent::Expression(te) => {
match &te.expression {
TyExpressionVariant::ImplicitReturn(exp) => self
Expand All @@ -404,9 +405,6 @@ impl<'a> FnCompiler<'a> {
}
}
}
// a side effect can be () because it just impacts the type system/namespacing.
// There should be no new IR generated.
ty::TyAstNodeContent::SideEffect(_) => Ok(None),
ty::TyAstNodeContent::Error(_, _) => {
unreachable!("error node found when generating IR");
}
Expand Down
35 changes: 25 additions & 10 deletions sway-core/src/language/parsed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
mod code_block;
pub mod declaration;
mod expression;
mod include_statement;
mod mod_statement;
mod module;
mod program;
mod use_statement;

pub use code_block::*;
pub use declaration::*;
pub use expression::*;
pub use include_statement::IncludeStatement;
pub use mod_statement::ModStatement;
pub use module::{ModuleEvaluationOrder, ParseModule, ParseSubmodule};
pub use program::{ParseProgram, TreeType};
use sway_error::handler::ErrorEmitted;
Expand Down Expand Up @@ -54,15 +54,13 @@ impl PartialEqWithEngines for AstNode {
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum AstNodeContent {
/// A statement of the form `use foo::bar;` or `use ::foo::bar;`
UseStatement(UseStatement),
/// Any statement node.
Statement(Statement),
/// Any type of declaration, of which there are quite a few. See [Declaration] for more details
/// on the possible variants.
Declaration(Declaration),
/// Any type of expression, of which there are quite a few. See [Expression] for more details.
Expression(Expression),
/// A statement of the form `mod foo::bar;` which imports/includes another source file.
IncludeStatement(IncludeStatement),
/// A malformed statement.
///
/// Used for parser recovery when we cannot form a more specific node.
Expand All @@ -75,20 +73,37 @@ impl EqWithEngines for AstNodeContent {}
impl PartialEqWithEngines for AstNodeContent {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
match (self, other) {
(AstNodeContent::UseStatement(lhs), AstNodeContent::UseStatement(rhs)) => lhs.eq(rhs),
(AstNodeContent::Statement(lhs), AstNodeContent::Statement(rhs)) => lhs.eq(rhs, ctx),
(AstNodeContent::Declaration(lhs), AstNodeContent::Declaration(rhs)) => {
lhs.eq(rhs, ctx)
}
(AstNodeContent::Expression(lhs), AstNodeContent::Expression(rhs)) => lhs.eq(rhs, ctx),
(AstNodeContent::IncludeStatement(lhs), AstNodeContent::IncludeStatement(rhs)) => {
lhs.eq(rhs)
}
(AstNodeContent::Error(lhs, ..), AstNodeContent::Error(rhs, ..)) => lhs.eq(rhs),
_ => false,
}
}
}

/// Statements that can appear in the parse tree.
#[derive(Debug, Clone)]
pub enum Statement {
/// A statement of the form `use foo::bar;` or `use ::foo::bar;`
Use(UseStatement),
/// A statement of the form `mod foo::bar;` which imports/includes another source file.
Mod(ModStatement),
}

impl EqWithEngines for Statement {}
impl PartialEqWithEngines for Statement {
fn eq(&self, other: &Self, _ctx: &PartialEqWithEnginesContext) -> bool {
match (self, other) {
(Statement::Use(lhs), Statement::Use(rhs)) => lhs.eq(rhs),
(Statement::Mod(lhs), Statement::Mod(rhs)) => lhs.eq(rhs),
_ => false,
}
}
}

impl ParseTree {
/// Excludes all test functions from the parse tree.
pub(crate) fn exclude_tests(&mut self, engines: &Engines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sway_types::{span::Span, Ident};
use crate::language::Visibility;

#[derive(Clone, Debug, PartialEq)]
pub struct IncludeStatement {
pub struct ModStatement {
// this span may be used for errors in the future, although it is not right now.
pub span: Span,
pub mod_name: Ident,
Expand Down
Loading
Loading