Skip to content

Commit

Permalink
Disallow importing private type via use
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Nov 16, 2022
1 parent bed9569 commit 4850e6c
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 14 deletions.
8 changes: 8 additions & 0 deletions crates/analyzer/src/db/queries/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ pub fn module_used_item_map(
diagnostics.extend(items.diagnostics.iter().cloned());

for (name, (name_span, item)) in items.value.iter() {
if !item.is_public(db) {
diagnostics.push(errors::error(
&format!("{} {} is private", item.item_kind_display_name(), name,),
*name_span,
name.as_str(),
));
}

if let Some((other_name_span, other_item)) =
accum.insert(name.clone(), (*name_span, *item))
{
Expand Down
4 changes: 2 additions & 2 deletions crates/analyzer/tests/snapshots/analysis__basic_ingot.snap
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ note:
note:
┌─ ingots/basic_ingot/src/ding/dang.fe:1:1
1type Dang = Array<u256, 42>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Array<u256, 42>
1pub type Dang = Array<u256, 42>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Array<u256, 42>


note:
Expand Down
66 changes: 58 additions & 8 deletions crates/analyzer/tests/snapshots/errors__bad_visibility.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,47 @@ source: crates/analyzer/tests/errors.rs
expression: error_string_ingot(&path)

---
error: unresolved path item
error: type MyInt is private
┌─ compile_errors/bad_visibility/src/main.fe:1:11
1use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
│ ^^^^^ MyInt

error: constant MY_CONST is private
┌─ compile_errors/bad_visibility/src/main.fe:1:18
1use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
│ ^^^^^^^^ MY_CONST

error: struct MyStruct is private
┌─ compile_errors/bad_visibility/src/main.fe:1:28
1use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
│ ^^^^^^^^ MyStruct

error: trait MyTrait is private
┌─ compile_errors/bad_visibility/src/main.fe:1:38
1use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
│ ^^^^^^^ MyTrait

error: function my_func is private
┌─ compile_errors/bad_visibility/src/main.fe:1:47
1 │ use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
^^^^^^^ my_func

error: type MyContract is private
┌─ compile_errors/bad_visibility/src/main.fe:1:56
1use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
│ ^^^^^^^^^^ MyContract

error: type MyEnum is private
┌─ compile_errors/bad_visibility/src/main.fe:1:68
1use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
^^^^^^ not found
│ ^^^^^^ MyEnum

error: the type `MyInt` is private
┌─ compile_errors/bad_visibility/src/main.fe:7:33
Expand Down Expand Up @@ -101,10 +137,24 @@ error: the function `my_func` is private
= `my_func` can only be used within `foo`
= Hint: use `pub` to make `my_func` visible from outside of `foo`

error: the type `MyContract` is private
error: the type `MyEnum` is private
┌─ compile_errors/bad_visibility/src/main.fe:25:16
25let _: MyContract = MyContract(addr)
25let e: MyEnum = MyEnum::Some
^^^^^^ this type is not `pub`
┌─ compile_errors/bad_visibility/src/foo.fe:17:6
17enum MyEnum {
│ ------ `MyEnum` is defined here
= `MyEnum` can only be used within `foo`
= Hint: use `pub` to make `MyEnum` visible from outside of `foo`

error: the type `MyContract` is private
┌─ compile_errors/bad_visibility/src/main.fe:29:16
29 │ let _: MyContract = MyContract(addr)
│ ^^^^^^^^^^ this type is not `pub`
┌─ compile_errors/bad_visibility/src/foo.fe:13:10
Expand All @@ -116,9 +166,9 @@ error: the type `MyContract` is private
= Hint: use `pub` to make `MyContract` visible from outside of `foo`

error: the type `MyContract` is private
┌─ compile_errors/bad_visibility/src/main.fe:25:29
┌─ compile_errors/bad_visibility/src/main.fe:29:29
25 │ let _: MyContract = MyContract(addr)
29let _: MyContract = MyContract(addr)
│ ^^^^^^^^^^ this type is not `pub`
┌─ compile_errors/bad_visibility/src/foo.fe:13:10
Expand All @@ -130,9 +180,9 @@ error: the type `MyContract` is private
= Hint: use `pub` to make `MyContract` visible from outside of `foo`

error: the type `MyContract` is private
┌─ compile_errors/bad_visibility/src/main.fe:26:9
┌─ compile_errors/bad_visibility/src/main.fe:30:9
26 │ MyContract.create(ctx, 1)
30MyContract.create(ctx, 1)
│ ^^^^^^^^^^ this type is not `pub`
┌─ compile_errors/bad_visibility/src/foo.fe:13:10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ source: crates/analyzer/tests/errors.rs
expression: "error_string(&path, test_files::fixture(path))"

---
error: struct OutOfReachMarker is private
┌─ compile_errors/emittable_not_implementable.fe:1:31
1use std::context::{Emittable, OutOfReachMarker}
^^^^^^^^^^^^^^^^ OutOfReachMarker

error: the struct `OutOfReachMarker` is private
┌─ compile_errors/emittable_not_implementable.fe:6:24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
struct Bur {}
pub struct Bur {}

struct Bud {}
pub struct Bud {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ fn my_func() {}

contract MyContract {
x: i32
}

enum MyEnum {
Some
Thing
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ contract Main {
my_func()
}

pub fn priv_enum() {
let e: MyEnum = MyEnum::Some
}

pub fn priv_contract(ctx: Context, addr: address) {
let _: MyContract = MyContract(addr)
MyContract.create(ctx, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type Dang = Array<u256, 42>
pub type Dang = Array<u256, 42>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub struct Bing {
pub my_address: address
}

fn get_42_backend() -> u256 {
pub fn get_42_backend() -> u256 {
return 42
}

Expand Down
5 changes: 5 additions & 0 deletions newsfragments/815.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Disallow importing private type via `use`

The following was previously allowed but will now error:

`use foo::PrivateStruct`

0 comments on commit 4850e6c

Please sign in to comment.