Skip to content

Commit

Permalink
Adds visibility check for fully qualified function (FuelLabs#4241)
Browse files Browse the repository at this point in the history
## Description
The issue was that it was possible to call private methods in another
module by calling them with fully qualified paths.

The fix was to use resolve_call_path_with_visibility_check instead of
resolve_call_path.

Closes FuelLabs#4231.

## 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: Mohammad Fawaz <[email protected]>
  • Loading branch information
esdrubal and mohammadfawaz authored Mar 8, 2023
1 parent 0cafc16 commit fd94ccc
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 6 deletions.
10 changes: 10 additions & 0 deletions sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ impl Namespace {
self.root.resolve_call_path(&self.mod_path, call_path)
}

/// Short-hand for calling [Root::resolve_call_path_with_visibility_check] on `root` with the `mod_path`.
pub(crate) fn resolve_call_path_with_visibility_check(
&self,
engines: Engines<'_>,
call_path: &CallPath,
) -> CompileResult<&ty::TyDeclaration> {
self.root
.resolve_call_path_with_visibility_check(engines, &self.mod_path, call_path)
}

/// Short-hand for calling [Root::resolve_type_with_self] on `root` with the `mod_path`.
pub(crate) fn resolve_type_with_self(
&mut self,
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ impl Root {
name: call_path.suffix.clone(),
span: call_path.suffix.span(),
});
return err(warnings, errors);
// Returns ok with error, this allows functions which call this to
// also access the returned TyDeclaration and throw more suitable errors.
return ok(decl, warnings, errors);
}
}

Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/type_system/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ impl TypeBinding<CallPath> {

// grab the declaration
let unknown_decl = check!(
ctx.namespace.resolve_call_path(&self.inner).cloned(),
ctx.namespace
.resolve_call_path_with_visibility_check(engines, &self.inner)
.cloned(),
return err(warnings, errors),
warnings,
errors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library inner_lib;

const C = 42;
pub const C = 42;

pub fn func() -> bool {
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'private_function_fully_qualified'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "private_function_fully_qualified"
entry = "main.sw"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
script;

dep pkga;

fn main() {
pkga::foo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library pkga;

fn foo() { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "fail"

# check: $()Symbol "foo" is private.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library bar;

enum Bar {
pub enum Bar {
Baz: bool,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library baz;

fn return_1() -> u32 {
pub fn return_1() -> u32 {
1u32
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library testlib;

fn foo() {
pub fn foo() {
log(1);
}

0 comments on commit fd94ccc

Please sign in to comment.