Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow importing private type via use #815

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 │ type Dang = Array<u256, 42>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Array<u256, 42>
1 │ pub 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
1 │ use 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
1 │ use 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
1 │ use 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
1 │ use 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
1 │ use 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
1 │ use foo::{MyInt, MY_CONST, MyStruct, MyTrait, my_func, MyContract, MyEnum }
│ ^^^^^^ not found
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly related to the fix but the test was wrong in that it imported a non existing Enum rather than an enum that isn't pub.

│ ^^^^^^ 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
25 │ let _: MyContract = MyContract(addr)
25 │ let e: MyEnum = MyEnum::Some
│ ^^^^^^ this type is not `pub`
┌─ compile_errors/bad_visibility/src/foo.fe:17:6
17 │ enum 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)
29 │ let _: 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)
30 │ MyContract.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
1 │ use 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`