diff --git a/crates/analyzer/src/namespace/items.rs b/crates/analyzer/src/namespace/items.rs index ea91c55552..86baac77d0 100644 --- a/crates/analyzer/src/namespace/items.rs +++ b/crates/analyzer/src/namespace/items.rs @@ -104,9 +104,7 @@ impl Item { pub fn items(&self, db: &dyn AnalyzerDb) -> Rc> { match self { Item::Ingot(ingot) => ingot - .lib_module(db) - .expect("ingot is missing a lib file") - .items(db), + .lib_items(db), Item::Module(module) => module.items(db), Item::Type(_) => todo!("cannot access items in types yet"), Item::GenericType(_) @@ -362,6 +360,27 @@ impl IngotId { db.ingot_lib_module(*self).value } + pub fn lib_items(&self, db: &dyn AnalyzerDb) -> Rc> { + let lib_module = self.lib_module(db).expect("missing lib module"); + let items = lib_module.items(db); + let ajacent_modules = lib_module.adjacent_modules(db); + + let items = items + .deref() + .clone() + .into_iter() + .chain( + ajacent_modules + .deref() + .clone() + .into_iter() + .map(|(name, module)| (name, Item::Module(module))) + ) + .collect(); + + Rc::new(items) + } + pub fn diagnostics(&self, db: &dyn AnalyzerDb) -> Vec { let mut diagnostics = vec![]; self.sink_diagnostics(db, &mut diagnostics); diff --git a/crates/analyzer/tests/snapshots/analysis__basic_ingot.snap b/crates/analyzer/tests/snapshots/analysis__basic_ingot.snap index 29e9a7893c..e5d84d17bc 100644 --- a/crates/analyzer/tests/snapshots/analysis__basic_ingot.snap +++ b/crates/analyzer/tests/snapshots/analysis__basic_ingot.snap @@ -10,6 +10,86 @@ note: │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Array +note: + ┌─ ingots/basic_ingot/src/bing.fe:5:5 + │ +5 │ my_address: address + │ ^^^^^^^^^^^^^^^^^^^ address + +note: + ┌─ ingots/basic_ingot/src/bing.fe:7:1 + │ +7 │ ╭ fn get_42_backend() -> u256: +8 │ │ return std::get_42() + │ ╰────────────────────────^ attributes hash: 17979516652885443340 + │ + = FunctionSignature { + self_decl: None, + params: [], + return_type: Ok( + Base( + Numeric( + U256, + ), + ), + ), + } + +note: + ┌─ ingots/basic_ingot/src/bing.fe:11:4 + │ +11 │ ╭ pub fn add(x: u256, y: u256) -> u256: +12 │ │ return x + y + │ ╰───────────────────^ attributes hash: 4022593831796629401 + │ + = FunctionSignature { + self_decl: None, + params: [ + FunctionParam { + name: "x", + typ: Ok( + Base( + Numeric( + U256, + ), + ), + ), + }, + FunctionParam { + name: "y", + typ: Ok( + Base( + Numeric( + U256, + ), + ), + ), + }, + ], + return_type: Ok( + Base( + Numeric( + U256, + ), + ), + ), + } + +note: + ┌─ ingots/basic_ingot/src/bing.fe:12:15 + │ +12 │ return x + y + │ ^ ^ u256: Value + │ │ + │ u256: Value + +note: + ┌─ ingots/basic_ingot/src/bing.fe:12:15 + │ +12 │ return x + y + │ ^^^^^ u256: Value + + note: ┌─ ingots/basic_ingot/src/ding/dong.fe:2:3 │ @@ -35,7 +115,7 @@ note: │ 9 │ ╭ pub fn get_my_baz() -> Baz: 10 │ │ return Baz(my_bool=true, my_u256=26) - │ ╰────────────────────────────────────────────^ attributes hash: 12775921899186886669 + │ ╰────────────────────────────────────────────^ attributes hash: 12400766704252115713 │ = FunctionSignature { self_decl: None, @@ -45,7 +125,7 @@ note: Struct { name: "Baz", id: StructId( - 1, + 2, ), field_count: 2, }, @@ -71,14 +151,14 @@ note: ┌─ ingots/basic_ingot/src/main.fe:10:16 │ 10 │ return Baz(my_bool=true, my_u256=26) - │ ^^^ TypeConstructor(Struct(Struct { name: "Baz", id: StructId(1), field_count: 2 })) + │ ^^^ TypeConstructor(Struct(Struct { name: "Baz", id: StructId(2), field_count: 2 })) note: ┌─ ingots/basic_ingot/src/main.fe:12:5 │ 12 │ ╭ pub fn get_my_bing() -> Bong: 13 │ │ return Bong(my_address=address(42)) - │ ╰───────────────────────────────────────────^ attributes hash: 9604670028259107253 + │ ╰───────────────────────────────────────────^ attributes hash: 4642145316486155293 │ = FunctionSignature { self_decl: None, @@ -88,7 +168,7 @@ note: Struct { name: "Bing", id: StructId( - 2, + 0, ), field_count: 1, }, @@ -124,7 +204,7 @@ note: ┌─ ingots/basic_ingot/src/main.fe:13:16 │ 13 │ return Bong(my_address=address(42)) - │ ^^^^ TypeConstructor(Struct(Struct { name: "Bing", id: StructId(2), field_count: 1 })) + │ ^^^^ TypeConstructor(Struct(Struct { name: "Bing", id: StructId(0), field_count: 1 })) note: ┌─ ingots/basic_ingot/src/main.fe:15:5 @@ -155,7 +235,7 @@ note: ┌─ ingots/basic_ingot/src/main.fe:16:16 │ 16 │ return get_42_backend() - │ ^^^^^^^^^^^^^^ Pure(FunctionId(5)) + │ ^^^^^^^^^^^^^^ Pure(FunctionId(0)) note: ┌─ ingots/basic_ingot/src/main.fe:18:5 @@ -166,7 +246,7 @@ note: 21 │ │ my_u256=42, 22 │ │ my_i8=-1 23 │ │ ) - │ ╰─────────^ attributes hash: 12523642377619379671 + │ ╰─────────^ attributes hash: 12062965033232055368 │ = FunctionSignature { self_decl: None, @@ -176,7 +256,7 @@ note: Struct { name: "Dyng", id: StructId( - 0, + 1, ), field_count: 3, }, @@ -227,7 +307,7 @@ note: ┌─ ingots/basic_ingot/src/main.fe:19:16 │ 19 │ return dong::Dyng( - │ ^^^^^^^^^^ TypeConstructor(Struct(Struct { name: "Dyng", id: StructId(0), field_count: 3 })) + │ ^^^^^^^^^^ TypeConstructor(Struct(Struct { name: "Dyng", id: StructId(1), field_count: 3 })) note: ┌─ ingots/basic_ingot/src/main.fe:25:5 @@ -282,89 +362,9 @@ note: ┌─ ingots/basic_ingot/src/main.fe:26:34 │ 26 │ let bing: BingContract = BingContract.create(0) - │ ^^^^^^^^^^^^^^^^^^^ BuiltinAssociatedFunction { contract: ContractId(1), function: Create } + │ ^^^^^^^^^^^^^^^^^^^ BuiltinAssociatedFunction { contract: ContractId(0), function: Create } 27 │ return bing.add(40, 50) - │ ^^^^^^^^ External { contract: ContractId(1), function: FunctionId(6) } - - -note: - ┌─ ingots/basic_ingot/src/bing.fe:2:5 - │ -2 │ my_address: address - │ ^^^^^^^^^^^^^^^^^^^ address - -note: - ┌─ ingots/basic_ingot/src/bing.fe:4:1 - │ -4 │ ╭ fn get_42_backend() -> u256: -5 │ │ return std::get_42() - │ ╰────────────────────────^ attributes hash: 17979516652885443340 - │ - = FunctionSignature { - self_decl: None, - params: [], - return_type: Ok( - Base( - Numeric( - U256, - ), - ), - ), - } - -note: - ┌─ ingots/basic_ingot/src/bing.fe:8:4 - │ -8 │ ╭ pub fn add(x: u256, y: u256) -> u256: -9 │ │ return x + y - │ ╰───────────────────^ attributes hash: 4022593831796629401 - │ - = FunctionSignature { - self_decl: None, - params: [ - FunctionParam { - name: "x", - typ: Ok( - Base( - Numeric( - U256, - ), - ), - ), - }, - FunctionParam { - name: "y", - typ: Ok( - Base( - Numeric( - U256, - ), - ), - ), - }, - ], - return_type: Ok( - Base( - Numeric( - U256, - ), - ), - ), - } - -note: - ┌─ ingots/basic_ingot/src/bing.fe:9:15 - │ -9 │ return x + y - │ ^ ^ u256: Value - │ │ - │ u256: Value - -note: - ┌─ ingots/basic_ingot/src/bing.fe:9:15 - │ -9 │ return x + y - │ ^^^^^ u256: Value + │ ^^^^^^^^ External { contract: ContractId(0), function: FunctionId(1) } diff --git a/crates/library/std/src/bar/bar.fe b/crates/library/std/src/bar/bar.fe new file mode 100644 index 0000000000..b7325d79ac --- /dev/null +++ b/crates/library/std/src/bar/bar.fe @@ -0,0 +1,2 @@ +struct Bar: + pass \ No newline at end of file diff --git a/crates/library/std/src/foo.fe b/crates/library/std/src/foo.fe new file mode 100644 index 0000000000..c8c035e436 --- /dev/null +++ b/crates/library/std/src/foo.fe @@ -0,0 +1,2 @@ +struct Foo: + pass \ No newline at end of file diff --git a/crates/test-files/fixtures/ingots/basic_ingot/src/bing.fe b/crates/test-files/fixtures/ingots/basic_ingot/src/bing.fe index ca319d338f..07d579a188 100644 --- a/crates/test-files/fixtures/ingots/basic_ingot/src/bing.fe +++ b/crates/test-files/fixtures/ingots/basic_ingot/src/bing.fe @@ -1,3 +1,6 @@ +use std::foo::Foo +use std::bar::bar::Bar + struct Bing: my_address: address