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

feat: catch imports of private definitions in resolution #4491

Closed
wants to merge 24 commits into from

Conversation

TomAFrench
Copy link
Member

Description

Problem*

Resolves

Summary*

Currently Noir only defines import visibility for functions, while everything else is implicitly public. This PR is an experiment on adding proper import visibility to Noir so that we can.

I've modified ItemScope so that it can hold both public and private definitions and during as we traverse modules in resolution we check that dep:: paths don't enter any private modules. I've added pub mod syntax to allow this to be shown.

I've set the prelude to be pub mod so that we can compile most programs but all other modules within the stdlib are private to show how errors look like.

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [Exceptional Case] Documentation to be submitted in a separate PR.
    I'll document this closer to the time of this being merged.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that we're breaking a bunch of Rust visibility rules here. The prelude is importing things from private modules.

@TomAFrench
Copy link
Member Author

TomAFrench commented Mar 7, 2024

Heads up that I'm going to be chopping this PR up into chunks to get some less potentially controversial sections through quicker and reduce the diff. You're welcome to look and comment on it but reviews may become obsolete.

github-merge-queue bot pushed a commit that referenced this pull request Mar 7, 2024
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This is some groundwork pulled out from
#4491

We're going to want to apply visibility modifiers to module declarations
so this PR creates a proper struct for these so it's easier for us to
add an extra field to hold the visibility.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@TomAFrench TomAFrench changed the base branch from master to tf/less-visibility-hardcoding March 7, 2024 22:20
@Savio-Sou
Copy link
Collaborator

Is this an extension of the open and internal keywords to general Noir programs?

@TomAFrench
Copy link
Member Author

Nope this is an equivalent of https://doc.rust-lang.org/reference/visibility-and-privacy.html

github-merge-queue bot pushed a commit that referenced this pull request Mar 12, 2024
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This pulls out a renaming from #4491.

We have the concept of `FunctionVisibility` which determines whether a
function can be called from other modules in the same crate/other
crates.

If we're expanding the same concept of visibility to items other than
functions we need a generic name for this which doesn't mention
functions. I've then renamed it to `ModuleVisibility` to reflect that it
shows how this item is visible in other modules but I'm open to
suggestions.


## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
Base automatically changed from tf/less-visibility-hardcoding to master March 13, 2024 15:39
github-merge-queue bot pushed a commit that referenced this pull request Mar 14, 2024
#4559)

# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

I just had a test pass when it should have failed in #4491 because
despite compiling when it shouldn't have, it failed in execution
(because I didn't provide a `Prover.toml` but the circuit took
arguments).

We're then masking potential test failures by not separating circuits
which we expect to fail to compile vs fail to execute. I've created an
`execution_failure` directory to hold all of the latter and updated the
`compile_failure` tests to only compile the circuit rather than
executing it.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
* master:
  chore: separate tests for execution failures from compilation failures (#4559)
  feat: remove curly braces with fmt  (#4529)
  fix: Make `nargo` the default binary for cargo run (#4554)
  fix: Evaluate operators in globals in types (#4537)
  chore: Add more `Hash` impls to stdlib (#4470)
  feat: Sync from aztec-packages (#4546)
  chore: Add `Instruction::DecrementRc` (#4525)
  feat: add `nargo compile --watch` command (#4464)
  fix: Substitute generics when checking the field count of a type (#4547)
  feat: optimize sha2 implementation (#4441)
github-merge-queue bot pushed a commit that referenced this pull request Mar 15, 2024
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

Running tests within the Noir stdlib is a little complicated as nargo
will not accept it as a regular crate as the stdlib uses features which
are disallowed in other crates. This means that in a number of cases
we're placing stdlib unit tests inside of the `test_programs` instead of
just using the `#[test]` format inside of the stdlib itself.

This is suboptimal as it separates the implementation from the tests in
the repository and once #4491 is
merged, it will be impossible to use this method to test any private
functions within the stdlib.

This PR then adds a new integration test which is equivalent to running
`nargo test` on the stdlib and moves some tests over to the stdlib as an
example.

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@TomAFrench TomAFrench marked this pull request as ready for review March 15, 2024 16:16
@TomAFrench
Copy link
Member Author

I think that this is close enough to be ready for reviews now.

Comment on lines +374 to +392
fn import_visibility() -> impl NoirParser<ItemVisibility> {
let is_pub_crate = (keyword(Keyword::Pub)
.then_ignore(just(Token::LeftParen))
.then_ignore(keyword(Keyword::Crate))
.then_ignore(just(Token::RightParen)))
.map(|_| ItemVisibility::PublicCrate);

let is_pub_super = (keyword(Keyword::Pub)
.then_ignore(just(Token::LeftParen))
.then_ignore(keyword(Keyword::Super))
.then_ignore(just(Token::RightParen)))
.map(|_| ItemVisibility::PublicSuper);

let is_pub = keyword(Keyword::Pub).map(|_| ItemVisibility::Public);

let is_private = empty().map(|_| ItemVisibility::Private);

choice((is_pub_crate, is_pub_super, is_pub, is_private))
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a bit confusing as we're overloading pub for both zk visibiilty and standard programming language visibility.

An alternative would be to rename this to export so we'd have export(super) and export(crate) to avoid this overloading.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a break from rust but a relatively weak one as it's just a keyword change.

Copy link
Collaborator

@Savio-Sou Savio-Sou Mar 19, 2024

Choose a reason for hiding this comment

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

a break from rust

pub for ZK is a break anyway; the benefits of more intuitive keywords almost always outweigh the costs.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jfecher what are your thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

It sounds like using pub is a compromise either way. pub for item visibility is much more common within Noir while pub for zk is only used on entry-point function parameters. Changing either would be breaking but changing pub visibility may be less so just because it is newer. Using export fn, export(crate) fn, etc would make Noir less rust-like which could be a stumbling block. I'm fine with both uses of pub for now but if there's ever a future scenario where we could want to apply pub (zk) to a function somehow then things would get confusing real fast.

compiler/noirc_frontend/src/ast/mod.rs Show resolved Hide resolved
compiler/noirc_frontend/src/ast/statement.rs Outdated Show resolved Hide resolved
compiler/noirc_frontend/src/lexer/token.rs Outdated Show resolved Hide resolved
compiler/noirc_frontend/src/parser/parser.rs Outdated Show resolved Hide resolved
compiler/noirc_frontend/src/parser/parser.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

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

LGTM

github-merge-queue bot pushed a commit that referenced this pull request Apr 2, 2024
…on (#4622)

# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR pulls out the actual changes to path resolution from #4491,
omitting the changes which add visibility modifiers to modules.

We now determine whether a function is private (and so a warning or
error should be emitted) during def collection. This is necessary as to
properly determine whether a function is public or private we need
information on whether all the modules in the function's path relative
to where it's being used allow its contents to be used from this module
- information which doesn't exist in the typechecking pass.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
TomAFrench added a commit that referenced this pull request Apr 3, 2024
…on (#4622)

# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR pulls out the actual changes to path resolution from #4491,
omitting the changes which add visibility modifiers to modules.

We now determine whether a function is private (and so a warning or
error should be emitted) during def collection. This is necessary as to
properly determine whether a function is public or private we need
information on whether all the modules in the function's path relative
to where it's being used allow its contents to be used from this module
- information which doesn't exist in the typechecking pass.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@Savio-Sou
Copy link
Collaborator

@TomAFrench would you like to supplement docs before merging the PR in?

@Savio-Sou
Copy link
Collaborator

Also will this be a breaking change in terms of libraries without pub will turn from public-by-default to private-by-default after the PR?

TomAFrench and others added 2 commits June 19, 2024 14:07
* master: (105 commits)
  chore: unbundle `check_array_is_initialized` (#5451)
  feat: Sync from aztec-packages (#5467)
  chore: bump bb to 0.45.1 (#5469)
  feat: prefix operator overload trait dispatch (#5423)
  feat: add CLI argument for debugging comptime blocks (#5192)
  chore: document EmbeddedCurvePoint (#5468)
  feat: lsp rename/find-all-references for struct members (#5443)
  feat(optimization): Deduplicate more instructions (#5457)
  fix: remove compile-time error for invalid indices (#5466)
  feat: lsp rename/find-all-references for local variables (#5439)
  feat: remove duplicated array reads at constant indices (#5445)
  fix: Account for the expected kind when resolving turbofish generics (#5448)
  fix: Fix issue with unresolved results (#5453)
  feat: apply `no_predicates` in stdlib (#5454)
  fix: prevent `no_predicates` from removing predicates in calling function (#5452)
  feat: lsp rename/find-all-references for globals (#5415)
  feat: remove redundant `EnableSideEffects` instructions (#5440)
  fix: Change panic to error in interpreter (#5446)
  feat: Add more slice methods to the stdlib (#5424)
  feat: Unquote multiple items from annotations (#5441)
  ...
@TomAFrench
Copy link
Member Author

Closing as superceded by more recent work.

@TomAFrench TomAFrench closed this Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants