Skip to content
Merged
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
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ impl Elaborator<'_> {

fn check_method_call_visibility(&mut self, func_id: FuncId, object_type: &Type, name: &Ident) {
if !method_call_is_visible(
self.self_type.as_ref(),
object_type,
func_id,
self.module_id(),
Expand Down
30 changes: 30 additions & 0 deletions compiler/noirc_frontend/src/hir/resolution/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn type_member_is_visible(
}

pub fn method_call_is_visible(
self_type: Option<&Type>,
object_type: &Type,
func_id: FuncId,
current_module: ModuleId,
Expand Down Expand Up @@ -155,6 +156,14 @@ pub fn method_call_is_visible(
);
}

// A private method defined on `Foo<i32>` should be visible when calling
// it from an impl on `Foo<i64>`, even though the generics are different.
if self_type
.is_some_and(|self_type| is_same_type_regardless_generics(self_type, object_type))
{
return true;
}

if let Some(struct_id) = func_meta.type_id {
return struct_member_is_visible(
struct_id,
Expand All @@ -178,3 +187,24 @@ pub fn method_call_is_visible(
}
}
}

fn is_same_type_regardless_generics(type1: &Type, type2: &Type) -> bool {
if type1 == type2 {
return true;
}

match (type1.follow_bindings(), type2.follow_bindings()) {
(Type::Array(..), Type::Array(..)) => true,
(Type::Slice(..), Type::Slice(..)) => true,
(Type::String(..), Type::String(..)) => true,
(Type::FmtString(..), Type::FmtString(..)) => true,
(Type::Tuple(..), Type::Tuple(..)) => true,
(Type::Function(..), Type::Function(..)) => true,
(Type::DataType(data_type1, ..), Type::DataType(data_type2, ..)) => {
data_type1.borrow().id == data_type2.borrow().id
}
(Type::Reference(type1, _), _) => is_same_type_regardless_generics(&type1, type2),
(_, Type::Reference(type2, _)) => is_same_type_regardless_generics(type1, &type2),
_ => false,
}
}
50 changes: 50 additions & 0 deletions compiler/noirc_frontend/src/tests/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,53 @@ fn errors_on_use_of_private_exported_item() {
"#;
check_errors!(src);
}

#[named]
#[test]
fn private_impl_method_on_another_module_1() {
let src = r#"
pub mod bar {
pub struct Foo<T> {}
}

impl<T> bar::Foo<T> {
fn foo(self) {
let _ = self;
}

fn bar(self) {
self.foo();
}
}

fn main() {}
"#;
assert_no_errors!(src);
}

#[named]
#[test]
fn private_impl_method_on_another_module_2() {
let src = r#"
pub mod bar {
pub struct Foo<T> {}
}

impl bar::Foo<i32> {
fn foo(self) {
let _ = self;
}
}

impl bar::Foo<i64> {
fn bar(self) {
let _ = self;
let foo = bar::Foo::<i32> {};
foo.foo();
}
}

fn main() {}
"#;
assert_no_errors!(src);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[package]
name = "noirc_frontend_tests_visibility_private_impl_method_on_another_module_1"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

pub mod bar {
pub struct Foo<T> {}
}

impl<T> bar::Foo<T> {
fn foo(self) {
let _ = self;
}

fn bar(self) {
self.foo();
}
}

fn main() {}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18298111048694488524
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[package]
name = "noirc_frontend_tests_visibility_private_impl_method_on_another_module_2"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

pub mod bar {
pub struct Foo<T> {}
}

impl bar::Foo<i32> {
fn foo(self) {
let _ = self;
}
}

impl bar::Foo<i64> {
fn bar(self) {
let _ = self;
let foo = bar::Foo::<i32> {};
foo.foo();
}
}

fn main() {}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13369966237074401506
1 change: 1 addition & 0 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ impl<'a> NodeFinder<'a> {

if is_primitive
&& !method_call_is_visible(
self.self_type.as_ref(),
typ,
func_id,
self.module_id,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading