Skip to content

Commit

Permalink
multiple fixes for the new encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Apr 24, 2024
1 parent 6528166 commit 360ce99
Show file tree
Hide file tree
Showing 62 changed files with 275 additions and 116 deletions.
10 changes: 7 additions & 3 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,13 @@ pub(crate) fn compile_constant_expression_to_constant(
// Special case functions because the span in `const_expr` is to the inlined function
// definition, rather than the actual call site.
ty::TyExpressionVariant::FunctionApplication { call_path, .. } => {
Err(CompileError::NonConstantDeclValue {
span: call_path.span(),
})
let span = call_path.span();
let span = if span == Span::dummy() {
const_expr.span.clone()
} else {
span
};
Err(CompileError::NonConstantDeclValue { span })
}
_otherwise => Err(CompileError::NonConstantDeclValue {
span: const_expr.span.clone(),
Expand Down
55 changes: 35 additions & 20 deletions sway-core/src/language/ty/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ fn get_type_not_allowed_error(
})
}

fn check_no_ref_main(engines: &Engines, handler: &Handler, main_function: &DeclId<TyFunctionDecl>) {
let main_function = engines.de().get_function(main_function);
for param in &main_function.parameters {
if param.is_reference && param.is_mutable {
handler.emit_err(CompileError::RefMutableNotAllowedInMain {
param_name: param.name.clone(),
span: param.name.span(),
});
}
}
}

impl TyProgram {
/// Validate the root module given the expected program kind.
pub fn validate_root(
Expand Down Expand Up @@ -237,7 +249,11 @@ impl TyProgram {

TyProgramKind::Contract {
entry_function: if experimental.new_encoding {
assert!(entries.len() == 1);
if entries.len() != 1 {
return Err(handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
}));
}
Some(entries[0])
} else {
None
Expand Down Expand Up @@ -276,8 +292,15 @@ impl TyProgram {
return Err(last_error.unwrap());
}

// check if no ref mut arguments passed to a `main()` in a `script` or `predicate`.
check_no_ref_main(engines, handler, &mains[0]);

let (entry_fn_id, main_fn_id) = if experimental.new_encoding {
assert!(entries.len() == 1);
if entries.len() != 1 {
return Err(handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
}));
}
(entries[0], mains[0])
} else {
assert!(entries.is_empty());
Expand Down Expand Up @@ -318,8 +341,15 @@ impl TyProgram {
return Err(last_error.unwrap());
}

// check if no ref mut arguments passed to a `main()` in a `script` or `predicate`.
check_no_ref_main(engines, handler, &mains[0]);

let (entry_fn_id, main_fn_id) = if experimental.new_encoding {
assert!(entries.len() == 1);
if entries.len() != 1 {
return Err(handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
}));
}
(entries[0], mains[0])
} else {
assert!(entries.is_empty());
Expand Down Expand Up @@ -380,23 +410,6 @@ impl TyProgram {
}
};

// check if no ref mut arguments passed to a `main()` in a `script` or `predicate`.
match &typed_program_kind {
TyProgramKind::Script { main_function, .. }
| TyProgramKind::Predicate { main_function, .. } => {
let main_function = decl_engine.get_function(main_function);
for param in &main_function.parameters {
if param.is_reference && param.is_mutable {
handler.emit_err(CompileError::RefMutableNotAllowedInMain {
param_name: param.name.clone(),
span: param.name.span(),
});
}
}
}
_ => (),
}

//configurables and constant cannot be str slice
for c in configurables.iter() {
if let Some(error) = get_type_not_allowed_error(
Expand Down Expand Up @@ -426,6 +439,8 @@ impl TyProgram {
}
}

assert!(!handler.has_errors());

Ok((typed_program_kind, declarations, configurables))
}

Expand Down
30 changes: 10 additions & 20 deletions sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
semantic_analysis::TypeCheckContext,
Engines, TypeId, TypeInfo, TypeParameter,
};
use itertools::Itertools;
use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
Expand Down Expand Up @@ -555,7 +554,7 @@ where
TypeInfo::RawUntypedPtr => "raw_ptr".into(),
TypeInfo::RawUntypedSlice => "raw_slice".into(),
TypeInfo::Alias { name, .. } => name.to_string(),
x => return None,
_ => return None,
};

Some(name)
Expand Down Expand Up @@ -676,12 +675,9 @@ where

match entry_fn {
Ok(entry_fn) => Ok(entry_fn),
Err(gen_handler) => {
handler.append(gen_handler);
Err(handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
}))
}
Err(gen_handler) => Err(gen_handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
})),
}
}

Expand Down Expand Up @@ -732,12 +728,9 @@ where

match entry_fn {
Ok(entry_fn) => Ok(entry_fn),
Err(gen_handler) => {
handler.append(gen_handler);
Err(handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
}))
}
Err(gen_handler) => Err(gen_handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
})),
}
}

Expand Down Expand Up @@ -805,12 +798,9 @@ where

match entry_fn {
Ok(entry_fn) => Ok(entry_fn),
Err(gen_handler) => {
handler.append(gen_handler);
Err(handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
}))
}
Err(gen_handler) => Err(gen_handler.emit_err(CompileError::CouldNotGenerateEntry {
span: Span::dummy(),
})),
}
}
}
12 changes: 9 additions & 3 deletions sway-core/src/semantic_analysis/cei_pattern_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ fn analyze_contract(engines: &Engines, ast_nodes: &[ty::TyAstNode]) -> Vec<Compi
let decl_engine = engines.de();
let mut warnings: Vec<CompileWarning> = vec![];
for fn_decl in contract_entry_points(decl_engine, ast_nodes) {
// no need to analyze the entry fn
if fn_decl.name.as_str() == "__entry" {
continue;
}
analyze_code_block(engines, &fn_decl.body, &fn_decl.name, &mut warnings);
}
warnings
Expand Down Expand Up @@ -608,10 +612,12 @@ fn effects_of_intrinsic(intr: &sway_ast::Intrinsic) -> HashSet<Effect> {
StateClear | StateStoreWord | StateStoreQuad => HashSet::from([Effect::StorageWrite]),
StateLoadWord | StateLoadQuad => HashSet::from([Effect::StorageRead]),
Smo => HashSet::from([Effect::OutputMessage]),
ContractCall => HashSet::from([Effect::Interaction]),
Revert | JmpMem | IsReferenceType | IsStrArray | SizeOfType | SizeOfVal | SizeOfStr
| ContractCall | ContractRet | AssertIsStrArray | ToStrArray | Eq | Gt | Lt | Gtf
| AddrOf | Log | Add | Sub | Mul | Div | And | Or | Xor | Mod | Rsh | Lsh | PtrAdd
| PtrSub | Not => HashSet::new(),
| ContractRet | AssertIsStrArray | ToStrArray | Eq | Gt | Lt | Gtf | AddrOf | Log | Add
| Sub | Mul | Div | And | Or | Xor | Mod | Rsh | Lsh | PtrAdd | PtrSub | Not => {
HashSet::new()
}
}
}

Expand Down
21 changes: 12 additions & 9 deletions sway-core/src/semantic_analysis/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,22 +329,24 @@ impl ty::TyModule {
(TreeType::Predicate, true) => {
let mut fn_generator =
auto_impl::EncodingAutoImplContext::new(&mut ctx).unwrap();
let node = fn_generator.generate_predicate_entry(
if let Ok(node) = fn_generator.generate_predicate_entry(
engines,
main_decl.as_ref().unwrap(),
handler,
)?;
all_nodes.push(node)
) {
all_nodes.push(node)
}
}
(TreeType::Script, true) => {
let mut fn_generator =
auto_impl::EncodingAutoImplContext::new(&mut ctx).unwrap();
let node = fn_generator.generate_script_entry(
if let Ok(node) = fn_generator.generate_script_entry(
engines,
main_decl.as_ref().unwrap(),
handler,
)?;
all_nodes.push(node)
) {
all_nodes.push(node)
}
}
(TreeType::Contract, _) => {
// collect all contract methods
Expand All @@ -357,14 +359,15 @@ impl ty::TyModule {

let mut fn_generator =
auto_impl::EncodingAutoImplContext::new(&mut ctx).unwrap();
let node = fn_generator.generate_contract_entry(
if let Ok(node) = fn_generator.generate_contract_entry(
engines,
parsed.span.source_id().map(|x| x.module_id()),
&contract_fns,
fallback_fn,
handler,
)?;
all_nodes.push(node)
) {
all_nodes.push(node)
}
}
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion sway-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ pub enum CompileError {
FallbackFnsAreContractOnly { span: Span },
#[error("Fallback functions cannot have parameters")]
FallbackFnsCannotHaveParameters { span: Span },
#[error("Could not generate the entry method because one of the arguments does not implement AbiEncode/AbiDecode")]
#[error("Could not generate the entry method. See errors above for more details.")]
CouldNotGenerateEntry { span: Span },
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
script;

enum Result<T, E> {
Ok: T,
Err: E,
}

// should return 5
fn main() -> u64 {
let result_a = Result::Ok::<u64, bool>(5u64);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[[package]]
name = 'configurables_are_not_const'
source = 'member'
name = "configurables_are_not_const"
source = "member"
dependencies = ["core"]

[[package]]
name = "core"
source = "path+from-root-96EE05F4B3FB2E76"
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "configurables_are_not_const"

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[[package]]
name = "const-instead-of-let"
source = "member"
dependencies = ["std"]
dependencies = [
"core",
"std",
]

[[package]]
name = "core"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ license = "Apache-2.0"
name = "const-instead-of-let"

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
std = { path = "../../../reduced_std_libs/sway-lib-std-assert" }
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
category = "fail"

# check: $()error
# check: const-instead-of-let/src/main.sw:13:38
# check: const-instead-of-let/src/main.sw:13
# check: $()const INVALID_CONST = contract_1.foo();
# nextln: $()Could not evaluate initializer to a const declaration.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license = "Apache-2.0"
name = "contract_a"

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }
std = { path = "../../../../reduced_std_libs/sway-lib-std-assert/" }

[contract-dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
[[package]]
name = 'contract_b'
source = 'member'
contract-dependencies = ['contract_c (1111111111111111111111111111111111111111111111111111111111111111)']
name = "contract_b"
source = "member"
dependencies = ["core"]
contract-dependencies = ["contract_c (1111111111111111111111111111111111111111111111111111111111111111)"]

[[package]]
name = 'contract_c'
source = 'path+from-root-A309856C1410AE80'
name = "contract_c"
source = "path+from-root-A309856C1410AE80"
dependencies = ["core"]

[[package]]
name = "core"
source = "path+from-root-A309856C1410AE80"
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ license = "Apache-2.0"
name = "contract_b"
implicit-std = false

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }

[contract-dependencies]
contract_c = { path = "../contract_c", salt = "0x1111111111111111111111111111111111111111111111111111111111111111" }

Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[[package]]
name = 'contract_c'
source = 'member'
name = "contract_c"
source = "member"
dependencies = ["core"]

[[package]]
name = "core"
source = "path+from-root-3C8F523D3E6B5411"
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ license = "Apache-2.0"
name = "contract_c"
implicit-std = false

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[[package]]
name = "core"
source = "path+from-root-3C5B50C68E15AEA9"

[[package]]
name = "disallowed_gm"
source = "member"
dependencies = ["core"]
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ license = "Apache-2.0"
name = "disallowed_gm"
entry = "main.sw"
implicit-std = false

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Loading

0 comments on commit 360ce99

Please sign in to comment.