Skip to content

Commit

Permalink
Merge branch 'master' into jz/docs-add-writing-noir
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaki authored Jul 30, 2024
2 parents 8d67f69 + 5ef9daa commit fb580a0
Show file tree
Hide file tree
Showing 19 changed files with 398 additions and 279 deletions.
120 changes: 0 additions & 120 deletions .github/ISSUE_TEMPLATE/bug_report.yml

This file was deleted.

71 changes: 0 additions & 71 deletions .github/ISSUE_TEMPLATE/feature_request.yml

This file was deleted.

12 changes: 12 additions & 0 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,18 @@ pub struct PathSegment {
pub span: Span,
}

impl PathSegment {
/// Returns the span where turbofish happen. For example:
///
/// foo::<T>
/// ~^^^^
///
/// Returns an empty span at the end of `foo` if there's no turbofish.
pub fn turbofish_span(&self) -> Span {
Span::from(self.ident.span().end()..self.span.end())
}
}

impl From<Ident> for PathSegment {
fn from(ident: Ident) -> PathSegment {
let span = ident.span();
Expand Down
28 changes: 9 additions & 19 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'context> Elaborator<'context> {
let (hir_expr, typ) = match expr.kind {
ExpressionKind::Literal(literal) => self.elaborate_literal(literal, expr.span),
ExpressionKind::Block(block) => self.elaborate_block(block),
ExpressionKind::Prefix(prefix) => return self.elaborate_prefix(*prefix),
ExpressionKind::Prefix(prefix) => return self.elaborate_prefix(*prefix, expr.span),
ExpressionKind::Index(index) => self.elaborate_index(*index),
ExpressionKind::Call(call) => self.elaborate_call(*call, expr.span),
ExpressionKind::MethodCall(call) => self.elaborate_method_call(*call, expr.span),
Expand Down Expand Up @@ -225,8 +225,7 @@ impl<'context> Elaborator<'context> {
(HirExpression::Literal(HirLiteral::FmtStr(str, fmt_str_idents)), typ)
}

fn elaborate_prefix(&mut self, prefix: PrefixExpression) -> (ExprId, Type) {
let span = prefix.rhs.span;
fn elaborate_prefix(&mut self, prefix: PrefixExpression, span: Span) -> (ExprId, Type) {
let (rhs, rhs_type) = self.elaborate_expression(prefix.rhs);
let trait_id = self.interner.get_prefix_operator_trait_method(&prefix.operator);

Expand Down Expand Up @@ -430,23 +429,14 @@ impl<'context> Elaborator<'context> {
}
};

let struct_generics = if let Some(turbofish_generics) = &last_segment.generics {
if turbofish_generics.len() == struct_generics.len() {
let struct_type = r#type.borrow();
self.resolve_turbofish_generics(&struct_type.generics, turbofish_generics.clone())
} else {
self.push_err(TypeCheckError::GenericCountMismatch {
item: format!("struct {}", last_segment.ident),
expected: struct_generics.len(),
found: turbofish_generics.len(),
span: Span::from(last_segment.ident.span().end()..last_segment.span.end()),
});
let turbofish_span = last_segment.turbofish_span();

struct_generics
}
} else {
struct_generics
};
let struct_generics = self.resolve_struct_turbofish_generics(
&r#type.borrow(),
struct_generics,
last_segment.generics,
turbofish_span,
);

let struct_type = r#type.clone();
let generics = struct_generics.clone();
Expand Down
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,11 @@ impl<'context> Elaborator<'context> {
self.resolve_trait_bound(&constraint.trait_bound, typ)
}

fn resolve_trait_bound(&mut self, bound: &TraitBound, typ: Type) -> Option<TraitConstraint> {
pub fn resolve_trait_bound(
&mut self,
bound: &TraitBound,
typ: Type,
) -> Option<TraitConstraint> {
let the_trait = self.lookup_trait_or_error(bound.trait_path.clone())?;

let resolved_generics = &the_trait.generics.clone();
Expand Down
41 changes: 39 additions & 2 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ impl<'context> Elaborator<'context> {
mutable: Option<Span>,
new_definitions: &mut Vec<HirIdent>,
) -> HirPattern {
let name_span = name.last_ident().span();
let is_self_type = name.last_ident().is_self_type_name();
let exclude_last_segment = true;
self.check_unsupported_turbofish_usage(&name, exclude_last_segment);

let last_segment = name.last_segment();
let name_span = last_segment.ident.span();
let is_self_type = last_segment.ident.is_self_type_name();

let error_identifier = |this: &mut Self| {
// Must create a name here to return a HirPattern::Identifier. Allowing
Expand All @@ -178,6 +182,15 @@ impl<'context> Elaborator<'context> {
}
};

let turbofish_span = last_segment.turbofish_span();

let generics = self.resolve_struct_turbofish_generics(
&struct_type.borrow(),
generics,
last_segment.generics,
turbofish_span,
);

let actual_type = Type::Struct(struct_type.clone(), generics);
let location = Location::new(span, self.file);

Expand Down Expand Up @@ -426,6 +439,30 @@ impl<'context> Elaborator<'context> {
})
}

pub(super) fn resolve_struct_turbofish_generics(
&mut self,
struct_type: &StructType,
generics: Vec<Type>,
unresolved_turbofish: Option<Vec<UnresolvedType>>,
span: Span,
) -> Vec<Type> {
let Some(turbofish_generics) = unresolved_turbofish else {
return generics;
};

if turbofish_generics.len() != generics.len() {
self.push_err(TypeCheckError::GenericCountMismatch {
item: format!("struct {}", struct_type.name),
expected: generics.len(),
found: turbofish_generics.len(),
span,
});
return generics;
}

self.resolve_turbofish_generics(&struct_type.generics, turbofish_generics)
}

pub(super) fn resolve_turbofish_generics(
&mut self,
generics: &[ResolvedGeneric],
Expand Down
Loading

0 comments on commit fb580a0

Please sign in to comment.