Skip to content

Commit

Permalink
Fail nicely on imports without callpaths (#5792)
Browse files Browse the repository at this point in the history
## Description

Fixes #5650.

`use` statements without a call path such as

```
use foo;
```

causes the compiler to crash during semantic analysis.

This PR causes the compiler to fail nicely instead.

These types of imports should be allowed, but I am in the process of
reworking the import logic, so I'm postponing an actual fix.

## 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] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [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.

---------

Co-authored-by: IGI-111 <igi-111@protonmail.com>
  • Loading branch information
jjcnn and IGI-111 authored Apr 2, 2024
1 parent 905bb49 commit 73fd7ec
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ fn item_use_to_use_statements(
};
return Err(handler.emit_err(error.into()));
}

let mut ret = Vec::new();
let mut prefix = Vec::new();
let item_span = item_use.span();
Expand All @@ -279,6 +280,18 @@ fn item_use_to_use_statements(
&mut ret,
item_span,
);

// Check that all use statements have a call_path
// This is not the case for `use foo;`, which is currently not supported
for use_stmt in ret.iter() {
if use_stmt.call_path.is_empty() {
let error = ConvertParseTreeError::ImportsWithoutItemsNotSupports {
span: use_stmt.span.clone(),
};
return Err(handler.emit_err(error.into()));
}
}

debug_assert!(prefix.is_empty());
Ok(ret)
}
Expand Down
3 changes: 3 additions & 0 deletions sway-error/src/convert_parse_tree_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use thiserror::Error;
pub enum ConvertParseTreeError {
#[error("pub use imports are not supported")]
PubUseNotSupported { span: Span },
#[error("Imports without items are not supported")]
ImportsWithoutItemsNotSupports { span: Span },
#[error("functions used in applications may not be arbitrary expressions")]
FunctionArbitraryExpression { span: Span },
#[error("generics are not supported here")]
Expand Down Expand Up @@ -125,6 +127,7 @@ impl Spanned for ConvertParseTreeError {
fn span(&self) -> Span {
match self {
ConvertParseTreeError::PubUseNotSupported { span } => span.clone(),
ConvertParseTreeError::ImportsWithoutItemsNotSupports { span } => span.clone(),
ConvertParseTreeError::FunctionArbitraryExpression { span } => span.clone(),
ConvertParseTreeError::GenericsNotSupportedHere { span } => span.clone(),
ConvertParseTreeError::MultipleGenericsNotSupported { span } => span.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "core"
source = "path+from-root-EDF14A16D4AB69C3"

[[package]]
name = "import_non_item"
source = "member"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "import_non_item"
entry = "main.sw"

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurables": [],
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [],
"messagesTypes": [],
"types": [
{
"components": null,
"type": "u64",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
script;

use my_internal_mod;

fn main() {
assert(true);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library;

fn foo() -> u32 {
42
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
category = "fail"

# check: $()error
# check: $()Imports without items are not supported
# check: $()Aborting due to 1 error

0 comments on commit 73fd7ec

Please sign in to comment.