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: Allow shadowing by default #2000

Merged
merged 1 commit into from
Jul 23, 2023
Merged
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
25 changes: 21 additions & 4 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ impl<'a> Resolver<'a> {
warn_if_unused: bool,
definition: DefinitionKind,
) -> HirIdent {
let allow_shadowing = allow_shadowing || &name == "_";

if definition.is_global() {
return self.add_global_variable_decl(name, definition);
}
Expand Down Expand Up @@ -952,7 +950,7 @@ impl<'a> Resolver<'a> {
let decl = this.add_variable_decl(
identifier,
false,
false,
true,
DefinitionKind::Local(None),
);
(decl, this.resolve_expression(block))
Expand Down Expand Up @@ -1053,7 +1051,7 @@ impl<'a> Resolver<'a> {
(Some(_), DefinitionKind::Local(_)) => DefinitionKind::Local(None),
(_, other) => other,
};
let id = self.add_variable_decl(name, mutable.is_some(), false, definition);
let id = self.add_variable_decl(name, mutable.is_some(), true, definition);
HirPattern::Identifier(id)
}
Pattern::Mutable(pattern, span) => {
Expand Down Expand Up @@ -1535,6 +1533,25 @@ mod test {
assert!(errors.is_empty());
}

#[test]
fn resolve_shadowing() {
let src = r#"
fn main(x : Field) {
let x = foo(x);
let x = x;
let (x, x) = (x, x);
let _ = x;
}

fn foo(x : Field) -> Field {
x
}
"#;

let errors = resolve_src_code(src, vec!["main", "foo"]);
assert!(errors.is_empty());
}

fn path_unresolved_error(err: ResolverError, expected_unresolved_path: &str) {
match err {
ResolverError::PathResolutionError(PathResolutionError::Unresolved(name)) => {
Expand Down