Skip to content

Commit 7d6e6df

Browse files
committed
PR feedback
1 parent 4fbe621 commit 7d6e6df

7 files changed

Lines changed: 94 additions & 26 deletions

File tree

compiler/noirc_frontend/src/elaborator/comptime.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ use noirc_errors::Location;
1919

2020
use crate::{
2121
Type, TypeBindings,
22-
ast::{Documented, Expression, ExpressionKind, TypeImpl, UnresolvedGenerics, UnresolvedType},
22+
ast::{
23+
Documented, Expression, ExpressionKind, TypeImpl, UnresolvedGenerics,
24+
UnresolvedTraitConstraint, UnresolvedType,
25+
},
2326
hir::{
2427
comptime::{Interpreter, InterpreterError, Value},
2528
def_collector::{
@@ -58,6 +61,7 @@ struct AttributeContext {
5861
struct AttributeImplTarget {
5962
object_type: UnresolvedType,
6063
generics: UnresolvedGenerics,
64+
where_clause: Vec<UnresolvedTraitConstraint>,
6165
type_location: Location,
6266
}
6367

@@ -246,20 +250,7 @@ impl<'context> Elaborator<'context> {
246250
}
247251

248252
self.collect_attributes_on_functions(functions, None, &mut attributes_to_run);
249-
for ((object_type, _impl_module), impls_in_module) in impls {
250-
for (generics, type_location, methods) in impls_in_module {
251-
let impl_target = AttributeImplTarget {
252-
object_type: object_type.clone(),
253-
generics: generics.clone(),
254-
type_location: *type_location,
255-
};
256-
self.collect_attributes_on_functions(
257-
std::slice::from_ref(methods),
258-
Some(&impl_target),
259-
&mut attributes_to_run,
260-
);
261-
}
262-
}
253+
self.collect_attributes_on_impls(impls, &mut attributes_to_run);
263254
self.collect_attributes_on_modules(module_attributes, &mut attributes_to_run);
264255

265256
self.sort_attributes_by_run_order(&mut attributes_to_run);
@@ -304,6 +295,29 @@ impl<'context> Elaborator<'context> {
304295
}
305296
}
306297

298+
#[tracing::instrument(level = "trace", skip_all)]
299+
fn collect_attributes_on_impls(
300+
&mut self,
301+
impls: &ImplMap,
302+
attributes_to_run: &mut CollectedAttributes,
303+
) {
304+
for ((object_type, _impl_module), impls_in_module) in impls {
305+
for (generics, where_clause, type_location, methods) in impls_in_module {
306+
let impl_target = AttributeImplTarget {
307+
object_type: object_type.clone(),
308+
generics: generics.clone(),
309+
where_clause: where_clause.clone(),
310+
type_location: *type_location,
311+
};
312+
self.collect_attributes_on_functions(
313+
std::slice::from_ref(methods),
314+
Some(&impl_target),
315+
attributes_to_run,
316+
);
317+
}
318+
}
319+
}
320+
307321
#[tracing::instrument(level = "trace", skip_all)]
308322
fn collect_attributes_on_functions(
309323
&mut self,
@@ -651,7 +665,7 @@ impl<'context> Elaborator<'context> {
651665
object_type: target.object_type.clone(),
652666
type_location: target.type_location,
653667
generics: target.generics.clone(),
654-
where_clause: Vec::new(),
668+
where_clause: target.where_clause.clone(),
655669
methods: vec![(Documented::new(function, item.doc_comments), location)],
656670
};
657671
let module = self.module_id();

compiler/noirc_frontend/src/elaborator/function.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,16 @@ impl Elaborator<'_> {
134134
&mut self,
135135
self_type: &UnresolvedType,
136136
local_module: LocalModuleId,
137-
function_sets: &mut Vec<(UnresolvedGenerics, Location, UnresolvedFunctions)>,
137+
function_sets: &mut Vec<(
138+
UnresolvedGenerics,
139+
Vec<UnresolvedTraitConstraint>,
140+
Location,
141+
UnresolvedFunctions,
142+
)>,
138143
) {
139144
self.local_module = Some(local_module);
140145

141-
for (generics, _, function_set) in function_sets {
146+
for (generics, _, _, function_set) in function_sets {
142147
// Prepare the impl: adds the impl generics to scope so the self type can
143148
// reference them, then resolve the self type.
144149
self.add_generics(generics);

compiler/noirc_frontend/src/elaborator/impls.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use noirc_errors::Location;
8080

8181
use crate::{
8282
Type,
83-
ast::{UnresolvedGenerics, UnresolvedType},
83+
ast::{UnresolvedGenerics, UnresolvedTraitConstraint, UnresolvedType},
8484
hir::{
8585
def_collector::{dc_crate::UnresolvedFunctions, errors::DefCollectorErrorKind},
8686
def_map::LocalModuleId,
@@ -109,12 +109,17 @@ impl Elaborator<'_> {
109109
pub(super) fn collect_impls(
110110
&mut self,
111111
module: LocalModuleId,
112-
impls: &mut [(UnresolvedGenerics, Location, UnresolvedFunctions)],
112+
impls: &mut [(
113+
UnresolvedGenerics,
114+
Vec<UnresolvedTraitConstraint>,
115+
Location,
116+
UnresolvedFunctions,
117+
)],
113118
self_type: &UnresolvedType,
114119
) {
115120
self.local_module = Some(module);
116121

117-
for (generics, location, unresolved) in impls {
122+
for (generics, _, location, unresolved) in impls {
118123
self.check_generics_appear_in_types(generics, &[self_type], &[]);
119124

120125
self.recover_generics(|this| {

compiler/noirc_frontend/src/elaborator/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use std::{
5858

5959
use crate::{
6060
Type,
61-
ast::UnresolvedGenerics,
61+
ast::{UnresolvedGenerics, UnresolvedTraitConstraint},
6262
elaborator::types::WildcardDisallowedContext,
6363
graph::CrateId,
6464
hir::{
@@ -805,8 +805,16 @@ impl<'context> Elaborator<'context> {
805805
}
806806

807807
#[tracing::instrument(level = "trace", skip_all)]
808-
fn elaborate_impls(&mut self, impls: Vec<(UnresolvedGenerics, Location, UnresolvedFunctions)>) {
809-
for (_, _, functions) in impls {
808+
fn elaborate_impls(
809+
&mut self,
810+
impls: Vec<(
811+
UnresolvedGenerics,
812+
Vec<UnresolvedTraitConstraint>,
813+
Location,
814+
UnresolvedFunctions,
815+
)>,
816+
) {
817+
for (_, _, _, functions) in impls {
810818
self.recover_generics(|this| this.elaborate_functions(functions));
811819
}
812820
}

compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl CollectedItems {
187187
/// since it would be non-deterministic.
188188
pub(crate) type ImplMap = HashMap<
189189
(UnresolvedType, LocalModuleId),
190-
Vec<(UnresolvedGenerics, Location, UnresolvedFunctions)>,
190+
Vec<(UnresolvedGenerics, Vec<UnresolvedTraitConstraint>, Location, UnresolvedFunctions)>,
191191
>;
192192

193193
/// Wraps a list of compilation errors.

compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,12 @@ pub fn collect_impl(
14651465

14661466
let key = (r#impl.object_type, module_id.local_id);
14671467
let methods = items.impls.entry(key).or_default();
1468-
methods.push((r#impl.generics, r#impl.type_location, unresolved_functions));
1468+
methods.push((
1469+
r#impl.generics,
1470+
r#impl.where_clause,
1471+
r#impl.type_location,
1472+
unresolved_functions,
1473+
));
14691474
}
14701475

14711476
fn find_module(

compiler/noirc_frontend/src/tests/metaprogramming.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,37 @@ fn generate_function_with_macros_on_impl_method() {
234234
");
235235
}
236236

237+
// Regression for asterite's review on #12649: when an attribute on an impl method
238+
// generates a new function, the impl's `where_clause` must be carried into the
239+
// synthetic impl so the generated function can use the bounded generics.
240+
#[test]
241+
fn generate_function_with_macros_on_impl_method_carries_where_clause() {
242+
let src = "
243+
pub trait MyDefault {
244+
fn my_default() -> Self;
245+
}
246+
247+
pub struct Foo<T> {}
248+
249+
impl<T> Foo<T> where T: MyDefault {
250+
#[generate_bar]
251+
pub fn foo() {}
252+
}
253+
254+
pub comptime fn generate_bar(_f: FunctionDefinition) -> Quoted {
255+
quote {
256+
pub fn bar() -> T {
257+
T::my_default()
258+
}
259+
}
260+
}
261+
262+
fn main() {}
263+
";
264+
265+
assert_no_errors(src);
266+
}
267+
237268
#[test]
238269
fn generate_function_with_macros_on_trait() {
239270
let src = "

0 commit comments

Comments
 (0)