Skip to content

Commit

Permalink
Fix first regression
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jan 10, 2025
1 parent e54010c commit cc15211
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
5 changes: 3 additions & 2 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,11 +1904,12 @@ impl<'context> Elaborator<'context> {
let impl_id = self.interner.next_trait_impl_id();
self.current_trait_impl = Some(impl_id);

// Fetch trait constraints here
let path_span = trait_impl.trait_path.span;
let (ordered_generics, named_generics) = trait_impl
.trait_id
.map(|trait_id| {
self.resolve_type_args(trait_generics, trait_id, trait_impl.trait_path.span)
// Check for missing generics & associated types for the trait being implemented
self.resolve_trait_args_from_trait_impl(trait_generics, trait_id, path_span)
})
.unwrap_or_default();

Expand Down
45 changes: 39 additions & 6 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,32 @@ impl<'context> Elaborator<'context> {
}
}

/// Identical to `resolve_type_args` but does not allow
/// associated types to be elided since trait impls must specify them.
pub(super) fn resolve_trait_args_from_trait_impl(
&mut self,
args: GenericTypeArgs,
item: TraitId,
span: Span,
) -> (Vec<Type>, Vec<NamedType>) {
self.resolve_type_args_inner(args, item, span, false)
}

pub(super) fn resolve_type_args(
&mut self,
args: GenericTypeArgs,
item: impl Generic,
span: Span,
) -> (Vec<Type>, Vec<NamedType>) {
self.resolve_type_args_inner(args, item, span, true)
}

pub(super) fn resolve_type_args_inner(
&mut self,
mut args: GenericTypeArgs,
item: impl Generic,
span: Span,
allow_implicit_named_args: bool,
) -> (Vec<Type>, Vec<NamedType>) {
let expected_kinds = item.generics(self.interner);

Expand All @@ -336,7 +357,12 @@ impl<'context> Elaborator<'context> {
let mut associated = Vec::new();

if item.accepts_named_type_args() {
associated = self.resolve_associated_type_args(args.named_args, item, span);
associated = self.resolve_associated_type_args(
args.named_args,
item,
span,
allow_implicit_named_args,
);
} else if !args.named_args.is_empty() {
let item_kind = item.item_kind();
self.push_err(ResolverError::NamedTypeArgs { span, item_kind });
Expand All @@ -350,6 +376,7 @@ impl<'context> Elaborator<'context> {
args: Vec<(Ident, UnresolvedType)>,
item: impl Generic,
span: Span,
allow_implicit_named_args: bool,
) -> Vec<NamedType> {
let mut seen_args = HashMap::default();
let mut required_args = item.named_generics(self.interner);
Expand Down Expand Up @@ -379,13 +406,19 @@ impl<'context> Elaborator<'context> {
resolved.push(NamedType { name, typ });
}

// Anything that hasn't been removed yet is missing, fill it in to avoid a panic.
// An error should already be issued earlier.
// Anything that hasn't been removed yet is missing.
// Fill it in to avoid a panic if we allow named args to be elided, otherwise error.
for generic in required_args {
let name = generic.name.clone();
let name = Ident::new(name.as_ref().clone(), span);
let typ = self.interner.next_type_variable();
resolved.push(NamedType { name, typ });

if allow_implicit_named_args {
let name = Ident::new(name.as_ref().clone(), span);
let typ = self.interner.next_type_variable();
resolved.push(NamedType { name, typ });
} else {
let item = item.item_name(self.interner);
self.push_err(TypeCheckError::MissingNamedTypeArg { item, span, name });
}
}

resolved
Expand Down

0 comments on commit cc15211

Please sign in to comment.