Skip to content

Commit

Permalink
fix: allow using Self for function calls (#5629)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #1791

## Summary

When resolving a path (any path), we now check if the first segment is
`Self` (a similar check is done in another places of our codebase). If
so, resolve the next path segments relative to that type. Only struct
types are supported here... in theory a trait type can also show up, but
that case is handled as a special case in the compiler. I'll create a
follow-up PR removing that special case and seeing if it works.

## Additional Context

None.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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.
  • Loading branch information
asterite authored Jul 31, 2024
1 parent f656681 commit b7e4f42
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
31 changes: 29 additions & 2 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use noirc_errors::{Location, Spanned};

use crate::ast::ERROR_IDENT;
use crate::ast::{PathKind, ERROR_IDENT};
use crate::hir::def_map::{LocalModuleId, ModuleId};
use crate::hir::resolution::path_resolver::{PathResolver, StandardPathResolver};
use crate::hir::scope::{Scope as GenericScope, ScopeTree as GenericScopeTree};
Expand Down Expand Up @@ -43,7 +43,34 @@ impl<'context> Elaborator<'context> {
}

pub(super) fn resolve_path(&mut self, path: Path) -> Result<ModuleDefId, ResolverError> {
let resolver = StandardPathResolver::new(self.module_id());
let mut module_id = self.module_id();
let mut path = path;

if path.kind == PathKind::Plain && path.first_name() == SELF_TYPE_NAME {
if let Some(Type::Struct(struct_type, _)) = &self.self_type {
let struct_type = struct_type.borrow();
if path.segments.len() == 1 {
return Ok(ModuleDefId::TypeId(struct_type.id));
}

module_id = struct_type.id.module_id();
path = Path {
segments: path.segments[1..].to_vec(),
kind: PathKind::Plain,
span: path.span(),
};
}
}

self.resolve_path_in_module(path, module_id)
}

fn resolve_path_in_module(
&mut self,
path: Path,
module_id: ModuleId,
) -> Result<ModuleDefId, ResolverError> {
let resolver = StandardPathResolver::new(module_id);
let path_resolution;

if self.interner.track_references {
Expand Down
80 changes: 80 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2729,3 +2729,83 @@ fn incorrect_generic_count_on_type_alias() {
assert_eq!(actual, 1);
assert_eq!(expected, 0);
}

#[test]
fn uses_self_type_for_struct_function_call() {
let src = r#"
struct S { }
impl S {
fn one() -> Field {
1
}
fn two() -> Field {
Self::one() + Self::one()
}
}
fn main() {}
"#;
assert_no_errors(src);
}

#[test]
fn uses_self_type_inside_trait() {
let src = r#"
trait Foo {
fn foo() -> Self {
Self::bar()
}
fn bar() -> Self;
}
impl Foo for Field {
fn bar() -> Self {
1
}
}
fn main() {
let _: Field = Foo::foo();
}
"#;
assert_no_errors(src);
}

#[test]
fn uses_self_type_in_trait_where_clause() {
let src = r#"
trait Trait {
fn trait_func() -> bool;
}
trait Foo where Self: Trait {
fn foo(self) -> bool {
self.trait_func()
}
}
struct Bar {
}
impl Foo for Bar {
}
fn main() {}
"#;

let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);

let CompilationError::TypeError(TypeCheckError::UnresolvedMethodCall { method_name, .. }) =
&errors[0].0
else {
panic!("Expected an unresolved method call error, got {:?}", errors[0].0);
};

assert_eq!(method_name, "trait_func");
}

0 comments on commit b7e4f42

Please sign in to comment.