Skip to content

Commit

Permalink
Disallow block-level const declarations without a value expression. (F…
Browse files Browse the repository at this point in the history
…uelLabs#4385)

Closes FuelLabs#4218.

## Description

As the title says, disallows block-level const declarations without a
value expressions.

## 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.
  • Loading branch information
tritao authored Apr 5, 2023
1 parent 14d8ead commit 9ecbdad
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
53 changes: 13 additions & 40 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn item_to_ast_nodes(
)?)),
ItemKind::Const(item_const) => decl(Declaration::ConstantDeclaration(
item_const_to_constant_declaration(
context, handler, engines, item_const, attributes, is_root,
context, handler, engines, item_const, attributes, true,
)?,
)),
ItemKind::Storage(item_storage) => decl(Declaration::StorageDeclaration(
Expand Down Expand Up @@ -588,10 +588,10 @@ fn item_trait_to_trait_declaration(
fn_signature_to_trait_fn(context, handler, engines, fn_sig, attributes)
.map(TraitItem::TraitFn)
}
ItemTraitItem::Const(const_decl) => {
item_const_to_const_decl(context, handler, engines, const_decl, attributes)
.map(TraitItem::Constant)
}
ItemTraitItem::Const(const_decl) => item_const_to_constant_declaration(
context, handler, engines, const_decl, attributes, false,
)
.map(TraitItem::Constant),
}?))
})
.filter_map_ok(|item| item)
Expand Down Expand Up @@ -656,10 +656,10 @@ fn item_impl_to_declaration(
item_fn_to_function_declaration(context, handler, engines, fn_item, attributes)
.map(ImplItem::Fn)
}
sway_ast::ItemImplItem::Const(const_item) => {
item_const_to_const_decl(context, handler, engines, const_item, attributes)
.map(ImplItem::Constant)
}
sway_ast::ItemImplItem::Const(const_item) => item_const_to_constant_declaration(
context, handler, engines, const_item, attributes, true,
)
.map(ImplItem::Constant),
}?))
})
.filter_map_ok(|item| item)
Expand Down Expand Up @@ -767,8 +767,8 @@ fn item_abi_to_abi_declaration(
)?;
Ok(TraitItem::TraitFn(trait_fn))
}
ItemTraitItem::Const(const_decl) => item_const_to_const_decl(
context, handler, engines, const_decl, attributes,
ItemTraitItem::Const(const_decl) => item_const_to_constant_declaration(
context, handler, engines, const_decl, attributes, false,
)
.map(TraitItem::Constant),
}?))
Expand Down Expand Up @@ -820,7 +820,7 @@ pub(crate) fn item_const_to_constant_declaration(
engines: Engines<'_>,
item_const: ItemConst,
attributes: AttributesMap,
is_root: bool,
require_expression: bool,
) -> Result<ConstantDeclaration, ErrorEmitted> {
let span = item_const.span();

Expand All @@ -832,7 +832,7 @@ pub(crate) fn item_const_to_constant_declaration(
let expr = match item_const.expr_opt {
Some(expr) => Some(expr_to_expression(context, handler, engines, expr)?),
None => {
if is_root {
if require_expression {
let err = ConvertParseTreeError::ConstantRequiresExpression { span: span.clone() };
if let Some(errors) = emit_all(handler, vec![err]) {
return Err(errors);
Expand Down Expand Up @@ -1314,33 +1314,6 @@ fn ty_to_type_argument(
Ok(type_argument)
}

fn item_const_to_const_decl(
context: &mut Context,
handler: &Handler,
engines: Engines<'_>,
item: ItemConst,
attributes: AttributesMap,
) -> Result<ConstantDeclaration, ErrorEmitted> {
let span = item.name.span();
let value = match item.expr_opt {
Some(expr) => Some(expr_to_expression(context, handler, engines, expr)?),
None => None,
};
let type_ascription = match item.ty_opt {
Some((_, ty)) => ty_to_type_argument(context, handler, engines, ty)?,
None => engines.te().insert(engines.de(), TypeInfo::Unknown).into(),
};
Ok(ConstantDeclaration {
name: item.name,
type_ascription,
value,
visibility: Visibility::Public,
is_configurable: true,
attributes,
span,
})
}

fn fn_signature_to_trait_fn(
context: &mut Context,
handler: &Handler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'const_block_level_no_expr'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "const_block_level_no_expr"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
script;

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

# check: $()const X;
# nextln: $()Constant requires expression.

0 comments on commit 9ecbdad

Please sign in to comment.