Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn compile_fn_with_args(
// may remain within the function scope.
let mut compiler = FnCompiler::new(context, module, func);

let mut ret_val = compiler.compile_code_block(context, body)?;
let mut ret_val = compiler.compile_code_block(context, module, body)?;

// Special case: if the return type is unit but the return value type is not, then we have an
// implicit return from the last expression in the code block having a semi-colon. This isn't
Expand Down
173 changes: 109 additions & 64 deletions sway-core/src/ir_generation/function.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,21 @@ pub(crate) fn type_check_method_application(
warnings,
errors
);
let variable_decl = check!(
unknown_decl.expect_variable().cloned(),
return err(warnings, errors),
warnings,
errors
);

if !variable_decl.is_mutable.is_mutable() && *is_mutable {
let is_decl_mutable = match unknown_decl {
TypedDeclaration::ConstantDeclaration(_) => false,
_ => {
let variable_decl = check!(
unknown_decl.expect_variable().cloned(),
return err(warnings, errors),
warnings,
errors
);
variable_decl.is_mutable.is_mutable()
}
};

if !is_decl_mutable && *is_mutable {
errors.push(CompileError::MethodRequiresMutableSelf {
method_name: method_name.easy_name(),
variable_name: name.clone(),
Expand Down
19 changes: 5 additions & 14 deletions sway-core/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,25 +303,16 @@ impl TypedAstNode {
value,
visibility,
}) => {
let result = type_check_ascribed_expr(
ctx.by_ref(),
type_ascription.clone(),
value,
);
let result =
type_check_ascribed_expr(ctx.by_ref(), type_ascription, value);
is_screaming_snake_case(&name).ok(&mut warnings, &mut errors);
let value =
check!(result, error_recovery_expr(name.span()), warnings, errors);
let typed_const_decl =
TypedDeclaration::VariableDeclaration(TypedVariableDeclaration {
TypedDeclaration::ConstantDeclaration(TypedConstantDeclaration {
name: name.clone(),
body: value,
is_mutable: if visibility.is_public() {
VariableMutability::ExportedConst
} else {
VariableMutability::Immutable
},
const_decl_origin: true,
type_ascription: insert_type(type_ascription),
value,
visibility,
});
ctx.namespace.insert_symbol(name, typed_const_decl.clone());
typed_const_decl
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[[package]]
name = 'const_nonconst_init'
source = 'root'
dependencies = ['core']

[[package]]
name = 'core'
source = 'path+from-root-E57A3612ABF8CF11'
dependencies = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "const_nonconst_init"

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
script;

fn bla(x: u64) -> u64 {
x + 1
}

fn main() -> u64 {
const X = bla(0);
X
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

# check: $()const X = bla(0);
# check-next: $()Could not evaluate initializer to a const declaration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check-next isn't a thing with the FileCheck crate -- that's from LLVM FileCheck 🙂. You probably want nextln. Unfortunately this would've just been ignored and isn't an error.

Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ const EN1c = En1::NoVal;
const ETH_ID0_VALUE = ETH_ID0.value;
const TUP1_idx2 = TUP1.2;

const INT1 = 1;

fn main() -> u64 {
const int1 = 1;
assert(int1 == INT1);

// initialization through function applications.
let eth_id0 = ~ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000);
let eth_id1 = ~ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001);
const eth_id0 = ~ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000);
const eth_id1 = ~ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001);
assert(eth_id0 == ETH_ID0 && eth_id1 == ETH_ID1);

// tuples and arrays.
let t1 = (2, 1, 21);
const t1 = (2, 1, 21);
assert(t1.0 == TUP1.0 && t1.1 == TUP1.1 && t1.2 == TUP1.2);
assert(t1.0 == TUP2.0 && t1.1 == TUP2.1 && t1.2 == TUP2.2);
let a1 = [1, 2, 3];
const a1 = [1, 2, 3];
assert(a1[0] == ARR1[0] && a1[1] == ARR1[1] && a1[2] == ARR1[2]);
assert(a1[0] == ARR2[0] && a1[1] == ARR2[1] && a1[2] == ARR2[2]);

Expand Down