Skip to content

Commit

Permalink
Adds a better error message in the case of using ref and deref ex…
Browse files Browse the repository at this point in the history
…pressions (FuelLabs#3687)

Instead of using the Rust `unimplemented!()` macro, Sway users now see
an "unimplemented" error from the Sway compiler when attempting to use
`ref` or `deref` expressions.

Closes FuelLabs#3677

Co-authored-by: emilyaherbert <[email protected]>
  • Loading branch information
emilyaherbert and emilyaherbert authored Jan 4, 2023
1 parent aa54dba commit 25f753b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2 deletions.
14 changes: 12 additions & 2 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1584,8 +1584,18 @@ fn expr_to_expression(
}),
span,
},
Expr::Ref { .. } => unimplemented!(),
Expr::Deref { .. } => unimplemented!(),
Expr::Ref { ref_token, .. } => {
let error = ConvertParseTreeError::RefExprNotYetSupported {
span: ref_token.span(),
};
return Err(handler.emit_err(error.into()));
}
Expr::Deref { deref_token, .. } => {
let error = ConvertParseTreeError::DerefExprNotYetSupported {
span: deref_token.span(),
};
return Err(handler.emit_err(error.into()));
}
Expr::Not { bang_token, expr } => {
let expr = expr_to_expression(handler, engines, *expr)?;
op_call("not", bang_token.span(), span, &[expr])?
Expand Down
6 changes: 6 additions & 0 deletions sway-error/src/convert_parse_tree_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ pub enum ConvertParseTreeError {
CannotAnnotateDependency { span: Span },
#[error("Expected dependency at the beginning before any other items.")]
ExpectedDependencyAtBeginning { span: Span },
#[error("Ref expressions are not supported yet.")]
RefExprNotYetSupported { span: Span },
#[error("Deref expressions are not supported yet.")]
DerefExprNotYetSupported { span: Span },
}

impl Spanned for ConvertParseTreeError {
Expand Down Expand Up @@ -149,6 +153,8 @@ impl Spanned for ConvertParseTreeError {
ConvertParseTreeError::CannotDocCommentDependency { span } => span.clone(),
ConvertParseTreeError::CannotAnnotateDependency { span } => span.clone(),
ConvertParseTreeError::ExpectedDependencyAtBeginning { span } => span.clone(),
ConvertParseTreeError::RefExprNotYetSupported { span } => span.clone(),
ConvertParseTreeError::DerefExprNotYetSupported { span } => span.clone(),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'ref_and_deref_expressions'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "ref_and_deref_expressions"
entry = "main.sw"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"attributes": null,
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u32",
"typeArguments": null
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
contract;

abi MyContract {
fn test_function() -> bool;
}

impl MyContract for Contract {
fn test_function() -> bool {
foo(ref Vec::new());
true
}
}

pub fn foo(ref mut vec: Vec<u64>) {
vec.push(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
category = "fail"

# check: $()error
# nextln: ref_and_deref_expressions/src/main.sw:9:13
# check: $()foo(ref Vec::new());
# nextln: $()Ref expressions are not supported yet.

0 comments on commit 25f753b

Please sign in to comment.