Skip to content

Commit

Permalink
Remove a couple unneeded uses of CompileResult. (FuelLabs#4254)
Browse files Browse the repository at this point in the history
## Description

Small PR to remove a couple unneeded uses of `CompileResult`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: emilyaherbert <[email protected]>
  • Loading branch information
emilyaherbert and emilyaherbert authored Mar 9, 2023
1 parent 264e4ec commit 7008875
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 73 deletions.
4 changes: 2 additions & 2 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl<'cfg> ControlFlowGraph<'cfg> {
let mut entry_points = vec![];
let mut non_entry_points = vec![];
for ast_entrypoint in module_nodes {
if ast_entrypoint.is_entry_point(decl_engine, tree_type)? {
if ast_entrypoint.is_entry_point(decl_engine, tree_type) {
entry_points.push(ast_entrypoint);
} else {
non_entry_points.push(ast_entrypoint);
Expand Down Expand Up @@ -203,7 +203,7 @@ fn collect_entry_points(
for i in graph.node_indices() {
let is_entry = match &graph[i] {
ControlFlowGraphNode::ProgramNode { node, .. } => {
node.is_entry_point(decl_engine, tree_type)?
node.is_entry_point(decl_engine, tree_type)
}
_ => false,
};
Expand Down
42 changes: 13 additions & 29 deletions sway-core/src/language/ty/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{
hash::{Hash, Hasher},
};

use sway_error::error::CompileError;
use sway_types::{Ident, Span};

use crate::{
Expand Down Expand Up @@ -146,24 +145,13 @@ impl TyAstNode {
}

/// Returns `true` if this AST node will be exported in a library, i.e. it is a public declaration.
pub(crate) fn is_public(&self, decl_engine: &DeclEngine) -> CompileResult<bool> {
let mut warnings = vec![];
let mut errors = vec![];
let public = match &self.content {
TyAstNodeContent::Declaration(decl) => {
let visibility = check!(
decl.visibility(decl_engine),
return err(warnings, errors),
warnings,
errors
);
visibility.is_public()
}
pub(crate) fn is_public(&self, decl_engine: &DeclEngine) -> bool {
match &self.content {
TyAstNodeContent::Declaration(decl) => decl.visibility(decl_engine).is_public(),
TyAstNodeContent::Expression(_)
| TyAstNodeContent::SideEffect(_)
| TyAstNodeContent::ImplicitReturnExpression(_) => false,
};
ok(public, warnings, errors)
}
}

/// Check to see if this node is a function declaration with generic type parameters.
Expand Down Expand Up @@ -204,11 +192,7 @@ impl TyAstNode {
}
}

pub(crate) fn is_entry_point(
&self,
decl_engine: &DeclEngine,
tree_type: &TreeType,
) -> Result<bool, CompileError> {
pub(crate) fn is_entry_point(&self, decl_engine: &DeclEngine, tree_type: &TreeType) -> bool {
match tree_type {
TreeType::Predicate | TreeType::Script => {
// Predicates and scripts have main and test functions as entry points.
Expand All @@ -223,9 +207,9 @@ impl TyAstNode {
..
} => {
let decl = decl_engine.get_function(decl_id);
Ok(decl.is_entry())
decl.is_entry()
}
_ => Ok(false),
_ => false,
}
}
TreeType::Contract | TreeType::Library { .. } => match self {
Expand All @@ -239,7 +223,7 @@ impl TyAstNode {
..
} => {
let decl = decl_engine.get_function(decl_id);
Ok(decl.visibility == Visibility::Public || decl.is_test())
decl.visibility == Visibility::Public || decl.is_test()
}
TyAstNode {
content:
Expand All @@ -249,19 +233,19 @@ impl TyAstNode {
..
}),
..
} => Ok(decl_engine.get_trait(decl_id).visibility.is_public()),
} => decl_engine.get_trait(decl_id).visibility.is_public(),
TyAstNode {
content:
TyAstNodeContent::Declaration(TyDeclaration::StructDeclaration(decl_ref)),
..
} => {
let struct_decl = decl_engine.get_struct(decl_ref);
Ok(struct_decl.visibility == Visibility::Public)
struct_decl.visibility == Visibility::Public
}
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::ImplTrait { .. }),
..
} => Ok(true),
} => true,
TyAstNode {
content:
TyAstNodeContent::Declaration(TyDeclaration::ConstantDeclaration {
Expand All @@ -272,9 +256,9 @@ impl TyAstNode {
..
} => {
let decl = decl_engine.get_constant(decl_id);
Ok(decl.visibility.is_public())
decl.visibility.is_public()
}
_ => Ok(false),
_ => false,
},
}
}
Expand Down
7 changes: 3 additions & 4 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,9 @@ impl TyDeclaration {
ok(type_id, warnings, errors)
}

pub(crate) fn visibility(&self, decl_engine: &DeclEngine) -> CompileResult<Visibility> {
pub(crate) fn visibility(&self, decl_engine: &DeclEngine) -> Visibility {
use TyDeclaration::*;
let visibility = match self {
match self {
TraitDeclaration { decl_id, .. } => {
let TyTraitDeclaration { visibility, .. } = decl_engine.get_trait(decl_id);
visibility
Expand All @@ -660,7 +660,6 @@ impl TyDeclaration {
| AbiDeclaration { .. }
| ErrorRecovery(_) => Visibility::Public,
VariableDeclaration(decl) => decl.mutability.visibility(),
};
ok(visibility, vec![], vec![])
}
}
}
8 changes: 1 addition & 7 deletions sway-core/src/language/ty/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,8 @@ impl CollectTypesMetadata for TyProgram {
.map(|(_, submod)| &submod.module),
) {
for node in module.all_nodes.iter() {
let is_public = check!(
node.is_public(decl_engine),
return err(warnings, errors),
warnings,
errors
);
let is_generic_function = node.is_generic_function(decl_engine);
if is_public {
if node.is_public(decl_engine) {
let node_metadata = check!(
node.collect_types_metadata(ctx),
return err(warnings, errors),
Expand Down
26 changes: 4 additions & 22 deletions sway-core/src/semantic_analysis/namespace/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
engine_threading::Engines,
error::*,
language::{parsed::*, ty, Visibility},
language::{parsed::*, ty},
semantic_analysis::*,
transform::to_parsed_lang,
Ident, Namespace,
Expand Down Expand Up @@ -225,13 +225,7 @@ impl Module {
let implemented_traits = src_ns.implemented_traits.clone();
let mut symbols_and_decls = vec![];
for (symbol, decl) in src_ns.symbols.iter() {
let visibility = check!(
decl.visibility(decl_engine),
return err(warnings, errors),
warnings,
errors
);
if visibility == Visibility::Public {
if decl.visibility(decl_engine).is_public() {
symbols_and_decls.push((symbol.clone(), decl.clone()));
}
}
Expand Down Expand Up @@ -282,13 +276,7 @@ impl Module {
.map(|(symbol, (_, _, decl))| (symbol.clone(), decl.clone()))
.collect::<Vec<_>>();
for (symbol, decl) in src_ns.symbols.iter() {
let visibility = check!(
decl.visibility(decl_engine),
return err(warnings, errors),
warnings,
errors
);
if visibility == Visibility::Public {
if decl.visibility(decl_engine).is_public() {
symbols_and_decls.push((symbol.clone(), decl.clone()));
}
}
Expand Down Expand Up @@ -362,13 +350,7 @@ impl Module {
let mut impls_to_insert = TraitMap::default();
match src_ns.symbols.get(item).cloned() {
Some(decl) => {
let visibility = check!(
decl.visibility(decl_engine),
return err(warnings, errors),
warnings,
errors
);
if visibility != Visibility::Public {
if !decl.visibility(decl_engine).is_public() {
errors.push(CompileError::ImportPrivateSymbol {
name: item.clone(),
span: item.span(),
Expand Down
12 changes: 3 additions & 9 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sway_types::Spanned;

use crate::{
error::*,
language::{ty, CallPath, Visibility},
language::{ty, CallPath},
CompileResult, Engines, Ident,
};

Expand Down Expand Up @@ -53,7 +53,7 @@ impl Root {
mod_path: &Path,
call_path: &CallPath,
) -> CompileResult<&ty::TyDeclaration> {
let mut warnings = vec![];
let warnings = vec![];
let mut errors = vec![];

let result = self.resolve_call_path(mod_path, call_path);
Expand All @@ -67,13 +67,7 @@ impl Root {
value: Some(decl), ..
} = result
{
let visibility = check!(
decl.visibility(engines.de()),
return err(warnings, errors),
warnings,
errors
);
if visibility != Visibility::Public {
if !decl.visibility(engines.de()).is_public() {
errors.push(CompileError::ImportPrivateSymbol {
name: call_path.suffix.clone(),
span: call_path.suffix.span(),
Expand Down

0 comments on commit 7008875

Please sign in to comment.