Skip to content

Commit

Permalink
Merge efaceee into e3eb823
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj authored Jun 13, 2024
2 parents e3eb823 + efaceee commit 6b6ef74
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 39 deletions.
30 changes: 26 additions & 4 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{
language::{
parsed::TreeType,
ty::{
self, ConstantDecl, FunctionDecl, ProjectionKind, StructDecl, TraitDecl, TyAstNode,
TyAstNodeContent, TyDecl, TyImplItem, TypeAliasDecl,
self, ConfigurableDecl, ConstantDecl, FunctionDecl, ProjectionKind, StructDecl,
TraitDecl, TyAstNode, TyAstNodeContent, TyDecl, TyImplItem, TypeAliasDecl,
},
CallPath, Visibility,
},
Expand Down Expand Up @@ -81,6 +81,11 @@ fn is_entry_point(node: &TyAstNode, decl_engine: &DeclEngine, tree_type: &TreeTy
let decl = decl_engine.get_constant(decl_id);
decl.visibility.is_public()
}
TyAstNode {
content:
TyAstNodeContent::Declaration(TyDecl::ConfigurableDecl(ConfigurableDecl { .. })),
..
} => false,
TyAstNode {
content:
TyAstNodeContent::Declaration(TyDecl::TypeAliasDecl(TypeAliasDecl {
Expand Down Expand Up @@ -567,19 +572,26 @@ fn connect_declaration<'eng: 'cfg, 'cfg>(
ty::TyDecl::ConfigurableDecl(ty::ConfigurableDecl { decl_id, .. }) => {
let config_decl = decl_engine.get_configurable(decl_id);
let ty::TyConfigurableDecl {
call_path, value, ..
call_path,
value,
type_ascription,
..
} = &*config_decl;

graph
.namespace
.insert_configurable(call_path.suffix.clone(), entry_node);

connect_type_id(engines, type_ascription.type_id, graph, entry_node)?;

if let Some(value) = &value {
connect_expression(
engines,
&value.expression,
graph,
&[entry_node],
exit_node,
"constant declaration expression",
"configurable declaration expression",
tree_type,
value.span.clone(),
options,
Expand Down Expand Up @@ -2305,6 +2317,16 @@ fn construct_dead_code_warning_from_node(
span: decl_engine.get_constant(decl_id).name().span(),
warning_content: Warning::DeadDeclaration,
},
ty::TyAstNode {
content:
ty::TyAstNodeContent::Declaration(ty::TyDecl::ConfigurableDecl(ty::ConfigurableDecl {
decl_id,
})),
..
} => CompileWarning {
span: decl_engine.get_configurable(decl_id).name().span(),
warning_content: Warning::DeadDeclaration,
},
ty::TyAstNode {
content: ty::TyAstNodeContent::Declaration(ty::TyDecl::VariableDecl(decl)),
span,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::VecDeque;

use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
warning::{CompileWarning, Warning},
};
Expand Down Expand Up @@ -84,26 +85,41 @@ impl ty::TyConfigurableDecl {
.insert(engines, TypeInfo::RawUntypedSlice, None),
);

let (decode_fn_ref, _, _): (crate::decl_engine::DeclRefFunction, _, _) =
crate::TypeBinding::type_check(
&mut TypeBinding::<CallPath> {
inner: CallPath {
prefixes: vec![],
suffix: sway_types::Ident::new_no_span("abi_decode_in_place".into()),
is_absolute: false,
},
type_arguments: crate::TypeArgs::Regular(vec![TypeArgument {
type_id: type_ascription.type_id,
initial_type_id: type_ascription.type_id,
span: sway_types::Span::dummy(),
call_path_tree: None,
}]),
span: sway_types::Span::dummy(),
let value_span = value
.as_ref()
.map(|x| x.span.clone())
.unwrap_or_else(|| span.clone());
let abi_decode_in_place_handler = Handler::default();
let r = crate::TypeBinding::type_check(
&mut TypeBinding::<CallPath> {
inner: CallPath {
prefixes: vec![],
suffix: sway_types::Ident::new_with_override(
"abi_decode_in_place".into(),
value_span.clone(),
),
is_absolute: false,
},
handler,
ctx.by_ref(),
)
.unwrap();
type_arguments: crate::TypeArgs::Regular(vec![TypeArgument {
type_id: type_ascription.type_id,
initial_type_id: type_ascription.type_id,
span: sway_types::Span::dummy(),
call_path_tree: None,
}]),
span: value_span.clone(),
},
&abi_decode_in_place_handler,
ctx.by_ref(),
);

// Map expected errors to more understandable ones
handler.map_and_emit_errors_from(abi_decode_in_place_handler, |e| match e {
CompileError::SymbolNotFound { span, .. } => {
Some(CompileError::ConfigurableMissingAbiDecodeInPlace { span })
}
e => Some(e),
})?;
let (decode_fn_ref, _, _): (crate::decl_engine::DeclRefFunction, _, _) = r?;

let mut decode_fn_decl = (*engines.de().get_function(&decode_fn_ref)).clone();
let decl_mapping = crate::TypeParameter::gather_decl_mapping_from_trait_constraints(
Expand Down
15 changes: 15 additions & 0 deletions sway-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,8 @@ pub enum CompileError {
CouldNotGenerateEntryMissingImpl { ty: String, span: Span },
#[error("Only bool, u8, u16, u32, u64, u256, b256, string arrays and string slices can be used here.")]
EncodingUnsupportedType { span: Span },
#[error("Configurables need a function named \"abi_decode_in_place\" to be in scope.")]
ConfigurableMissingAbiDecodeInPlace { span: Span },
}

impl std::convert::From<TypeError> for CompileError {
Expand Down Expand Up @@ -1175,6 +1177,7 @@ impl Spanned for CompileError {
CouldNotGenerateEntryMissingImpl { span, .. } => span.clone(),
CannotBeEvaluatedToConfigurableSizeUnknown { span } => span.clone(),
EncodingUnsupportedType { span } => span.clone(),
ConfigurableMissingAbiDecodeInPlace { span } => span.clone(),
}
}
}
Expand Down Expand Up @@ -2430,6 +2433,18 @@ impl ToDiagnostic for CompileError {
},
}
},
ConfigurableMissingAbiDecodeInPlace { span } => Diagnostic {
reason: Some(Reason::new(code(1), "Configurables need a function named \"abi_decode_in_place\" to be in scope.".to_string())),
issue: Issue::error(
source_engine,
span.clone(),
String::new()
),
hints: vec![],
help: vec![
"Certify you are accessing a compatible version of the \"core\" library.".into()
],
},
_ => Diagnostic {
// TODO: Temporary we use self here to achieve backward compatibility.
// In general, self must not be used and will not be used once we
Expand Down
19 changes: 19 additions & 0 deletions sway-error/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ impl Handler {
{
self.inner.borrow_mut().errors.retain(f)
}

// Map all errors from `other` into this handler. If any mapping return none it is ignored. This
// method returns if any error was mapped or not.
pub fn map_and_emit_errors_from(
&self,
other: Handler,
mut f: impl FnMut(CompileError) -> Option<CompileError>,
) -> Result<(), ErrorEmitted> {
let mut emitted = Ok(());

let (errs, _) = other.consume();
for err in errs {
if let Some(err) = (f)(err) {
emitted = Err(self.emit_err(err));
}
}

emitted
}
}

/// Proof that an error was emitted through a `Handler`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"typeArguments": null
},
"name": "BOOL",
"offset": 6928
"offset": 7016
},
{
"configurableType": {
Expand All @@ -16,7 +16,7 @@
"typeArguments": null
},
"name": "U8",
"offset": 7064
"offset": 7168
},
{
"configurableType": {
Expand All @@ -25,7 +25,7 @@
"typeArguments": null
},
"name": "ANOTHER_U8",
"offset": 6856
"offset": 6944
},
{
"configurableType": {
Expand All @@ -34,7 +34,7 @@
"typeArguments": null
},
"name": "U16",
"offset": 7008
"offset": 7112
},
{
"configurableType": {
Expand All @@ -43,7 +43,7 @@
"typeArguments": null
},
"name": "U32",
"offset": 7048
"offset": 7152
},
{
"configurableType": {
Expand All @@ -52,7 +52,7 @@
"typeArguments": null
},
"name": "U64",
"offset": 7056
"offset": 7160
},
{
"configurableType": {
Expand All @@ -61,7 +61,7 @@
"typeArguments": null
},
"name": "U256",
"offset": 7016
"offset": 7120
},
{
"configurableType": {
Expand All @@ -70,7 +70,7 @@
"typeArguments": null
},
"name": "B256",
"offset": 6896
"offset": 6984
},
{
"configurableType": {
Expand All @@ -79,7 +79,7 @@
"typeArguments": []
},
"name": "CONFIGURABLE_STRUCT",
"offset": 6968
"offset": 7056
},
{
"configurableType": {
Expand All @@ -88,7 +88,7 @@
"typeArguments": []
},
"name": "CONFIGURABLE_ENUM_A",
"offset": 6936
"offset": 7024
},
{
"configurableType": {
Expand All @@ -97,7 +97,7 @@
"typeArguments": []
},
"name": "CONFIGURABLE_ENUM_B",
"offset": 6952
"offset": 7040
},
{
"configurableType": {
Expand All @@ -106,7 +106,7 @@
"typeArguments": null
},
"name": "ARRAY_BOOL",
"offset": 6864
"offset": 6952
},
{
"configurableType": {
Expand All @@ -115,7 +115,7 @@
"typeArguments": null
},
"name": "ARRAY_U64",
"offset": 6872
"offset": 6960
},
{
"configurableType": {
Expand All @@ -124,7 +124,7 @@
"typeArguments": null
},
"name": "TUPLE_BOOL_U64",
"offset": 6992
"offset": 7096
},
{
"configurableType": {
Expand All @@ -133,7 +133,25 @@
"typeArguments": null
},
"name": "STR_4",
"offset": 6984
"offset": 7088
},
{
"configurableType": {
"name": "",
"type": 13,
"typeArguments": null
},
"name": "NOT_USED",
"offset": 7072
},
{
"configurableType": {
"name": "",
"type": 13,
"typeArguments": null
},
"name": "NOT_USED_BUT_ALLOWED",
"offset": 7080
}
],
"encoding": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ type AnotherU8 = u8;
ARRAY_U64: [u64; 3] = [9, 8, 7],
TUPLE_BOOL_U64: (bool, u64) = (true, 11),
STR_4: str[4] = __to_str_array("abcd"),

NOT_USED: u8 = 1,
#[allow(dead_code)]
NOT_USED_BUT_ALLOWED: u8 = 2,
}

fn main() {
assert(BOOL == true);
assert(BOOL == true);
assert(U8 == 1);
assert(ANOTHER_U8 == 3);
assert(U16 == 2);
assert(U32 == 3);
assert(U64 == 4);
assert(U256 == 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAu256);
assert(B256 == 0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ expected_result_new_encoding = { action = "return_data", value = "" }
validate_abi = true
expected_warnings = 6
unsupported_profiles = ["debug"]

# check: $()NOT_USED: u8 = 1
# nextln: $()This declaration is never used

0 comments on commit 6b6ef74

Please # to comment.