Skip to content

Commit

Permalink
Fixes star_import_with_reexports. (FuelLabs#4498)
Browse files Browse the repository at this point in the history
## Description

`star_import_with_reexports` was not able to handle external paths.

This was causing an issue FuelLabs#3946 where
`get_items_for_type_and_trait_name` was called with trait name
`std::core::ops::Eq` instead of `core::ops::Eq`.

With these changes we bring back `assert_eq` and fix
`star_import_with_reexports` to handle properly external paths so it no
longer adds the src prefix to `use_synonyms`.

Closes FuelLabs#3946
Closes FuelLabs#2780

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] 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.

Co-authored-by: Sophie Dankel <[email protected]>
  • Loading branch information
esdrubal and sdankel authored Apr 25, 2023
1 parent dd09dc9 commit fe02ce3
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 40 deletions.
33 changes: 22 additions & 11 deletions sway-core/src/semantic_analysis/namespace/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,24 @@ impl Module {
}
}

let mut symbols_paths_and_decls = vec![];
for (symbol, (mod_path, _, decl)) in use_synonyms {
let mut is_external = false;
let submodule = src_ns.submodule(&[mod_path[0].clone()]);
if let Some(submodule) = submodule {
is_external = submodule.is_external
};

let mut path = src[..1].to_vec();
if is_external {
path = mod_path;
} else {
path.extend(mod_path);
}

symbols_paths_and_decls.push((symbol, path, decl));
}

let dst_ns = &mut self[dst];
dst_ns
.implemented_traits
Expand All @@ -295,19 +313,12 @@ impl Module {
.insert(symbol, (path, GlobImport::Yes, decl));
};

for symbol_and_decl in symbols_and_decls {
try_add(symbol_and_decl.0, src.to_vec(), symbol_and_decl.1);
for (symbol, decl) in symbols_and_decls {
try_add(symbol, src.to_vec(), decl);
}

for (symbol, (mod_path, _, decl)) in use_synonyms {
// N.B. We had a path like `::bar::baz`, which makes the module `bar` "crate-relative".
// Given that `bar`'s "crate" is `foo`, we'll need `foo::bar::baz` outside of it.
//
// FIXME(Centril, #2780): Seems like the compiler has no way of
// distinguishing between external and crate-relative paths?
let mut src = src[..1].to_vec();
src.extend(mod_path);
try_add(symbol, src, decl);
for (symbol, path, decl) in symbols_paths_and_decls {
try_add(symbol, path, decl);
}

ok((), warnings, errors)
Expand Down
4 changes: 1 addition & 3 deletions sway-lib-std/src/assert.sw
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub fn assert(condition: bool) {
}
}

// NOTE: temporarily disabled until https://github.com/FuelLabs/sway/issues/3946 is fixed
/*
/// Asserts that the given values `v1` & `v2` will always be equal during runtime.
///
/// ### Arguments
Expand All @@ -61,4 +59,4 @@ pub fn assert_eq<T>(v1: T, v2: T) where T: Eq {
log(v2);
revert(FAILED_ASSERT_EQ_SIGNAL);
}
}*/
}
2 changes: 1 addition & 1 deletion sway-lib-std/src/prelude.sw
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ::storage::storage_map::*;
use ::vec::Vec;

// Error handling
use ::assert::assert;
use ::assert::{assert, assert_eq};
use ::option::Option;
use ::result::Result;
use ::revert::{require, revert};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,37 @@
}
}
],
"loggedTypes": [],
"loggedTypes": [
{
"logId": 0,
"loggedType": {
"name": "",
"type": 1,
"typeArguments": null
}
},
{
"logId": 1,
"loggedType": {
"name": "",
"type": 1,
"typeArguments": null
}
}
],
"messagesTypes": [],
"types": [
{
"components": [],
"type": "()",
"typeId": 0,
"typeParameters": null
},
{
"components": null,
"type": "u64",
"typeId": 1,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ fn simple_option_generics_test() {
assert(get_an_option::<u64>().is_none());
}

// Disabled until https://github.com/FuelLabs/sway/issues/3946 is resolved
/*fn test_assert_eq_u64() {
fn test_assert_eq_u64() {
let a = 42;
let b = 40 + 2;
assert_eq(a, b);
}*/
}

fn test_try_from() {
let x = u64::try_from(7);
Expand All @@ -61,7 +60,7 @@ fn main() {
simple_vec_test();
complex_vec_test();
simple_option_generics_test();
// test_assert_eq_u64();
test_assert_eq_u64();
test_try_from();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@
"logId": 12,
"loggedType": {
"name": "",
"type": 8,
"type": 9,
"typeArguments": []
}
},
{
"logId": 13,
"loggedType": {
"name": "",
"type": 8,
"type": 9,
"typeArguments": []
}
},
Expand Down Expand Up @@ -225,11 +225,11 @@
},
{
"name": "ContractId",
"type": 8,
"type": 9,
"typeArguments": null
}
],
"type": "enum Identity",
"type": "enum std::identity::Identity",
"typeId": 3,
"typeParameters": null
},
Expand All @@ -247,7 +247,7 @@
"typeArguments": null
}
],
"type": "struct Address",
"type": "struct std::address::Address",
"typeId": 5,
"typeParameters": null
},
Expand All @@ -259,15 +259,15 @@
"typeArguments": null
}
],
"type": "struct B512",
"type": "struct std::b512::B512",
"typeId": 6,
"typeParameters": null
},
{
"components": [
{
"name": "buf",
"type": 9,
"type": 8,
"typeArguments": null
},
{
Expand All @@ -276,36 +276,36 @@
"typeArguments": null
}
],
"type": "struct Bytes",
"type": "struct std::bytes::Bytes",
"typeId": 7,
"typeParameters": null
},
{
"components": [
{
"name": "value",
"type": 1,
"name": "ptr",
"type": 4,
"typeArguments": null
},
{
"name": "cap",
"type": 12,
"typeArguments": null
}
],
"type": "struct ContractId",
"type": "struct std::bytes::RawBytes",
"typeId": 8,
"typeParameters": null
},
{
"components": [
{
"name": "ptr",
"type": 4,
"typeArguments": null
},
{
"name": "cap",
"type": 12,
"name": "value",
"type": 1,
"typeArguments": null
}
],
"type": "struct RawBytes",
"type": "struct std::contract_id::ContractId",
"typeId": 9,
"typeParameters": null
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
category = "disabled"
category = "run"
expected_result = { action = "return", value = 1 }
validate_abi = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'assert_eq_revert'
source = 'member'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-92AC3CEAE777B425'

[[package]]
name = 'std'
source = 'path+from-root-92AC3CEAE777B425'
dependencies = ['core']
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 = "assert_eq_revert"

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

fn main() {
assert_eq(1, 2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "revert", value = -65533 } # 0xffffffffffff0003 as i64
validate_abi = true

0 comments on commit fe02ce3

Please sign in to comment.