Skip to content

Commit

Permalink
PR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Feb 10, 2023
1 parent 6bd052b commit 29c82e1
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 24 deletions.
11 changes: 0 additions & 11 deletions crates/noirc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,3 @@ pub struct FileDiagnostic {
pub file_id: fm::FileId,
pub diagnostic: CustomDiagnostic,
}

/// Extension trait just to enable the syntax err.in_file(..) for CustomDiagnostic
pub trait IntoFileDiagnostic {
fn in_file(self, id: fm::FileId) -> FileDiagnostic;
}

impl IntoFileDiagnostic for CustomDiagnostic {
fn in_file(self, file_id: fm::FileId) -> FileDiagnostic {
FileDiagnostic { file_id, diagnostic: self }
}
}
4 changes: 4 additions & 0 deletions crates/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl CustomDiagnostic {
}
}

pub fn in_file(self, file_id: fm::FileId) -> FileDiagnostic {
FileDiagnostic { file_id, diagnostic: self }
}

pub fn add_note(&mut self, message: String) {
self.notes.push(message);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum ExpressionKind {
Error,
}

/// A Vec of unresolved names for type variables.
/// For `fn foo<A, B>(...)` this corresponds to vec!["A", "B"].
pub type UnresolvedGenerics = Vec<Ident>;

impl ExpressionKind {
Expand Down
2 changes: 2 additions & 0 deletions crates/noirc_frontend/src/ast/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{Ident, NoirFunction, UnresolvedGenerics, UnresolvedType};
use iter_extended::vecmap;
use noirc_errors::Span;

/// Ast node for a struct
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NoirStruct {
pub name: Ident,
Expand All @@ -23,6 +24,7 @@ impl NoirStruct {
}
}

/// Ast node for an impl
#[derive(Clone, Debug)]
pub struct NoirImpl {
pub object_type: UnresolvedType,
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::{
};
use fm::FileId;
use iter_extended::vecmap;
use noirc_errors::Span;
use noirc_errors::{CustomDiagnostic, FileDiagnostic};
use noirc_errors::{IntoFileDiagnostic, Span};
use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;

Expand Down
1 change: 0 additions & 1 deletion crates/noirc_frontend/src/hir/def_collector/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{hir::resolution::import::ImportDirective, Ident};

use noirc_errors::CustomDiagnostic as Diagnostic;
use noirc_errors::FileDiagnostic;
use noirc_errors::IntoFileDiagnostic;
use noirc_errors::Span;
use thiserror::Error;

Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::node_interner::FuncId;
use crate::parser::{parse_program, ParsedModule};
use arena::{Arena, Index};
use fm::{FileId, FileManager};
use noirc_errors::{FileDiagnostic, IntoFileDiagnostic};
use noirc_errors::FileDiagnostic;
use std::collections::HashMap;

mod module_def;
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use noirc_errors::Span;
use noirc_errors::{CustomDiagnostic as Diagnostic, FileDiagnostic, IntoFileDiagnostic};
use noirc_errors::{CustomDiagnostic as Diagnostic, FileDiagnostic};
use thiserror::Error;

use crate::{Ident, Type};
Expand Down
24 changes: 15 additions & 9 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ use crate::hir_def::{

use super::errors::ResolverError;

const SELF_TYPE_NAME: &str = "Self";

type Scope = GenericScope<String, ResolverMeta>;
type ScopeTree = GenericScopeTree<String, ResolverMeta>;
type ScopeForest = GenericScopeForest<String, ResolverMeta>;
Expand Down Expand Up @@ -354,7 +356,7 @@ impl<'a> Resolver<'a> {
}
}

if name == "Self" {
if name == SELF_TYPE_NAME {
if let Some(self_type) = self.self_type.clone() {
if !args.is_empty() {
self.push_err(ResolverError::GenericsOnSelfType { span: path.span() });
Expand Down Expand Up @@ -486,6 +488,8 @@ impl<'a> Resolver<'a> {
}
}

/// Add the given generics to scope.
/// Each generic will have a fresh Shared<TypeBinding> associated with it.
pub fn add_generics(&mut self, generics: &UnresolvedGenerics) -> Generics {
vecmap(generics, |generic| {
// Map the generic to a fresh type variable
Expand Down Expand Up @@ -856,19 +860,21 @@ impl<'a> Resolver<'a> {
HirPattern::Tuple(fields, span)
}
Pattern::Struct(name, fields, span) => {
let error_ident = |this: &mut Self| {
// Must create a name here to return a HirPattern::Identifer. $error is chosen
// since it is an invalid identifier that will not crash with any user variables.
let ident = this.add_variable_decl("$error".into(), false, true, definition);
HirPattern::Identifier(ident)
let error_identifier = |this: &mut Self| {
// Must create a name here to return a HirPattern::Identifer. Allowing
// shadowing here lets us avoid further errors if we define ERROR_IDENT
// multiple times.
let name = ERROR_IDENT.into();
let identifier = this.add_variable_decl(name, false, true, definition);
HirPattern::Identifier(identifier)
};

let (struct_type, generics) = match self.lookup_type_or_error(name) {
Some(Type::Struct(struct_type, generics)) => (struct_type, generics),
None => return error_ident(self),
None => return error_identifier(self),
Some(typ) => {
self.push_err(ResolverError::NonStructUsedInConstructor { typ, span });
return error_ident(self);
return error_identifier(self);
}
};

Expand Down Expand Up @@ -1000,7 +1006,7 @@ impl<'a> Resolver<'a> {
/// This will also instantiate any struct types found.
fn lookup_type_or_error(&mut self, path: Path) -> Option<Type> {
let ident = path.as_ident();
if ident.map_or(false, |i| i == "Self") {
if ident.map_or(false, |i| i == SELF_TYPE_NAME) {
if let Some(typ) = &self.self_type {
return Some(typ.clone());
}
Expand Down
4 changes: 4 additions & 0 deletions crates/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ impl NodeInterner {
})
}

/// Add a method to a type.
/// This will panic for non-struct types currently as we do not support methods
/// for primitives. We could allow this in the future however.
pub fn add_method(
&mut self,
self_type: &Type,
Expand All @@ -598,6 +601,7 @@ impl NodeInterner {
}
}

/// Search by name for a method on the given struct
pub fn lookup_method(&self, id: StructId, method_name: &str) -> Option<FuncId> {
self.struct_methods.get(&(id, method_name.to_owned())).map(|(_, id)| *id)
}
Expand Down

0 comments on commit 29c82e1

Please sign in to comment.