Skip to content

Commit 73fd7ec

Browse files
jjcnnIGI-111
andauthored
Fail nicely on imports without callpaths (#5792)
## 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>
1 parent 905bb49 commit 73fd7ec

File tree

8 files changed

+74
-0
lines changed

8 files changed

+74
-0
lines changed

sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs

+13
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ fn item_use_to_use_statements(
268268
};
269269
return Err(handler.emit_err(error.into()));
270270
}
271+
271272
let mut ret = Vec::new();
272273
let mut prefix = Vec::new();
273274
let item_span = item_use.span();
@@ -279,6 +280,18 @@ fn item_use_to_use_statements(
279280
&mut ret,
280281
item_span,
281282
);
283+
284+
// Check that all use statements have a call_path
285+
// This is not the case for `use foo;`, which is currently not supported
286+
for use_stmt in ret.iter() {
287+
if use_stmt.call_path.is_empty() {
288+
let error = ConvertParseTreeError::ImportsWithoutItemsNotSupports {
289+
span: use_stmt.span.clone(),
290+
};
291+
return Err(handler.emit_err(error.into()));
292+
}
293+
}
294+
282295
debug_assert!(prefix.is_empty());
283296
Ok(ret)
284297
}

sway-error/src/convert_parse_tree_error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use thiserror::Error;
55
pub enum ConvertParseTreeError {
66
#[error("pub use imports are not supported")]
77
PubUseNotSupported { span: Span },
8+
#[error("Imports without items are not supported")]
9+
ImportsWithoutItemsNotSupports { span: Span },
810
#[error("functions used in applications may not be arbitrary expressions")]
911
FunctionArbitraryExpression { span: Span },
1012
#[error("generics are not supported here")]
@@ -125,6 +127,7 @@ impl Spanned for ConvertParseTreeError {
125127
fn span(&self) -> Span {
126128
match self {
127129
ConvertParseTreeError::PubUseNotSupported { span } => span.clone(),
130+
ConvertParseTreeError::ImportsWithoutItemsNotSupports { span } => span.clone(),
128131
ConvertParseTreeError::FunctionArbitraryExpression { span } => span.clone(),
129132
ConvertParseTreeError::GenericsNotSupportedHere { span } => span.clone(),
130133
ConvertParseTreeError::MultipleGenericsNotSupported { span } => span.clone(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[[package]]
2+
name = "core"
3+
source = "path+from-root-EDF14A16D4AB69C3"
4+
5+
[[package]]
6+
name = "import_non_item"
7+
source = "member"
8+
dependencies = ["core"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[project]
2+
authors = ["Fuel Labs <contact@fuel.sh>"]
3+
license = "Apache-2.0"
4+
name = "import_non_item"
5+
entry = "main.sw"
6+
7+
[dependencies]
8+
core = { path = "../../../../../../sway-lib-core" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"configurables": [],
3+
"functions": [
4+
{
5+
"attributes": null,
6+
"inputs": [],
7+
"name": "main",
8+
"output": {
9+
"name": "",
10+
"type": 0,
11+
"typeArguments": null
12+
}
13+
}
14+
],
15+
"loggedTypes": [],
16+
"messagesTypes": [],
17+
"types": [
18+
{
19+
"components": null,
20+
"type": "u64",
21+
"typeId": 0,
22+
"typeParameters": null
23+
}
24+
]
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
script;
2+
3+
use my_internal_mod;
4+
5+
fn main() {
6+
assert(true);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library;
2+
3+
fn foo() -> u32 {
4+
42
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
category = "fail"
2+
3+
# check: $()error
4+
# check: $()Imports without items are not supported
5+
# check: $()Aborting due to 1 error

0 commit comments

Comments
 (0)