Skip to content

Commit 423e601

Browse files
TomAFrenchjfecher
andauthored
chore: make spans non-optional on UnresolvedType (#5773)
--- # Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* This PR pulls out the change to make spans on `UnresolvedType` mandatory to reduce the diff from #5739 ## Additional Context ## 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. --------- Co-authored-by: Jake Fecher <jake@aztecprotocol.com>
1 parent 090501d commit 423e601

File tree

23 files changed

+107
-152
lines changed

23 files changed

+107
-152
lines changed

aztec_macros/src/transforms/functions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn create_inputs(ty: &str) -> Param {
267267
let path_snippet = ty.to_case(Case::Snake); // e.g. private_context_inputs
268268
let type_path = chained_dep!("aztec", "context", "inputs", &path_snippet, ty);
269269

270-
let context_type = make_type(UnresolvedTypeData::Named(type_path, vec![], true));
270+
let context_type = make_type(UnresolvedTypeData::Named(type_path, Default::default(), true));
271271
let visibility = Visibility::Private;
272272

273273
Param { pattern: context_pattern, typ: context_type, visibility, span: Span::default() }
@@ -396,7 +396,7 @@ fn serialize_to_hasher(
396396
Signedness::Unsigned,
397397
ast::IntegerBitSize::ThirtyTwo,
398398
),
399-
span: None,
399+
span: Span::default(),
400400
},
401401
hasher_name,
402402
))
@@ -595,7 +595,7 @@ fn abstract_return_values(func: &NoirFunction) -> Result<Option<Vec<Statement>>,
595595
serialize_to_hasher(&ident(return_value_name), &current_return_type, hasher_name)
596596
.ok_or_else(|| AztecMacroError::UnsupportedFunctionReturnType {
597597
typ: current_return_type.clone(),
598-
span: func.return_type().span.unwrap_or_default(),
598+
span: func.return_type().span,
599599
})?;
600600

601601
replacement_statements.extend(serialization_statements);

aztec_macros/src/transforms/note_interface.rs

+30-29
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ pub fn generate_note_interface_impl(
6262
note_struct.name.0.contents
6363
)),
6464
})?;
65-
let note_interface_impl_span: Option<Span> =
66-
if empty_spans { None } else { trait_impl.object_type.span };
65+
let note_interface_impl_span =
66+
if empty_spans { Span::default() } else { trait_impl.object_type.span };
67+
6768
// Look for the note struct implementation, generate a default one if it doesn't exist (in order to append methods to it)
6869
let existing_impl = module.impls.iter_mut().find(|r#impl| match &r#impl.object_type.typ {
6970
UnresolvedTypeData::Named(path, _, _) => path.last_ident().eq(&note_struct.name),
@@ -94,7 +95,7 @@ pub fn generate_note_interface_impl(
9495
Ok(val.to_string())
9596
}
9697
_ => Err(AztecMacroError::CouldNotImplementNoteInterface {
97-
span: trait_impl.object_type.span,
98+
span: Some(trait_impl.object_type.span),
9899
secondary_message: Some(format!(
99100
"NoteInterface must be generic over NOTE_LEN and NOTE_BYTES_LEN: {}",
100101
note_type
@@ -231,7 +232,7 @@ fn generate_note_to_be_bytes(
231232
note_type: &String,
232233
byte_length: &str,
233234
serialized_length: &str,
234-
impl_span: Option<Span>,
235+
impl_span: Span,
235236
empty_spans: bool,
236237
) -> Result<NoirFunction, AztecMacroError> {
237238
let function_source = format!(
@@ -268,21 +269,21 @@ fn generate_note_to_be_bytes(
268269
dbg!(errors);
269270
return Err(AztecMacroError::CouldNotImplementNoteInterface {
270271
secondary_message: Some("Failed to parse Noir macro code (fn to_be_bytes). This is either a bug in the compiler or the Noir macro code".to_string()),
271-
span: impl_span
272+
span: Some(impl_span)
272273
});
273274
}
274275

275276
let mut function_ast = function_ast.into_sorted();
276277
let mut noir_fn = function_ast.functions.remove(0);
277-
noir_fn.def.span = impl_span.unwrap_or_default();
278+
noir_fn.def.span = impl_span;
278279
noir_fn.def.visibility = ItemVisibility::Public;
279280
Ok(noir_fn)
280281
}
281282

282283
fn generate_note_get_header(
283284
note_type: &String,
284285
note_header_field_name: &String,
285-
impl_span: Option<Span>,
286+
impl_span: Span,
286287
empty_spans: bool,
287288
) -> Result<NoirFunction, AztecMacroError> {
288289
let function_source = format!(
@@ -300,21 +301,21 @@ fn generate_note_get_header(
300301
dbg!(errors);
301302
return Err(AztecMacroError::CouldNotImplementNoteInterface {
302303
secondary_message: Some("Failed to parse Noir macro code (fn get_header). This is either a bug in the compiler or the Noir macro code".to_string()),
303-
span: impl_span
304+
span: Some(impl_span)
304305
});
305306
}
306307

307308
let mut function_ast = function_ast.into_sorted();
308309
let mut noir_fn = function_ast.functions.remove(0);
309-
noir_fn.def.span = impl_span.unwrap_or_default();
310+
noir_fn.def.span = impl_span;
310311
noir_fn.def.visibility = ItemVisibility::Public;
311312
Ok(noir_fn)
312313
}
313314

314315
fn generate_note_set_header(
315316
note_type: &String,
316317
note_header_field_name: &String,
317-
impl_span: Option<Span>,
318+
impl_span: Span,
318319
empty_spans: bool,
319320
) -> Result<NoirFunction, AztecMacroError> {
320321
let function_source = format!(
@@ -331,13 +332,13 @@ fn generate_note_set_header(
331332
dbg!(errors);
332333
return Err(AztecMacroError::CouldNotImplementNoteInterface {
333334
secondary_message: Some("Failed to parse Noir macro code (fn set_header). This is either a bug in the compiler or the Noir macro code".to_string()),
334-
span: impl_span
335+
span: Some(impl_span)
335336
});
336337
}
337338

338339
let mut function_ast = function_ast.into_sorted();
339340
let mut noir_fn = function_ast.functions.remove(0);
340-
noir_fn.def.span = impl_span.unwrap_or_default();
341+
noir_fn.def.span = impl_span;
341342
noir_fn.def.visibility = ItemVisibility::Public;
342343
Ok(noir_fn)
343344
}
@@ -346,7 +347,7 @@ fn generate_note_set_header(
346347
// of the conversion of the characters in the note's struct name to unsigned integers.
347348
fn generate_get_note_type_id(
348349
note_type_id: u32,
349-
impl_span: Option<Span>,
350+
impl_span: Span,
350351
empty_spans: bool,
351352
) -> Result<NoirFunction, AztecMacroError> {
352353
// TODO(#7165): replace {} with dep::aztec::protocol_types::abis::note_selector::compute_note_selector(\"{}\") in the function source below
@@ -365,13 +366,13 @@ fn generate_get_note_type_id(
365366
dbg!(errors);
366367
return Err(AztecMacroError::CouldNotImplementNoteInterface {
367368
secondary_message: Some("Failed to parse Noir macro code (fn get_note_type_id). This is either a bug in the compiler or the Noir macro code".to_string()),
368-
span: impl_span
369+
span: Some(impl_span)
369370
});
370371
}
371372

372373
let mut function_ast = function_ast.into_sorted();
373374
let mut noir_fn = function_ast.functions.remove(0);
374-
noir_fn.def.span = impl_span.unwrap_or_default();
375+
noir_fn.def.span = impl_span;
375376
noir_fn.def.visibility = ItemVisibility::Public;
376377
Ok(noir_fn)
377378
}
@@ -389,7 +390,7 @@ fn generate_note_properties_struct(
389390
note_type: &str,
390391
note_fields: &[(String, String)],
391392
note_header_field_name: &String,
392-
impl_span: Option<Span>,
393+
impl_span: Span,
393394
empty_spans: bool,
394395
) -> Result<NoirStruct, AztecMacroError> {
395396
let struct_source =
@@ -400,7 +401,7 @@ fn generate_note_properties_struct(
400401
dbg!(errors);
401402
return Err(AztecMacroError::CouldNotImplementNoteInterface {
402403
secondary_message: Some(format!("Failed to parse Noir macro code (struct {}Properties). This is either a bug in the compiler or the Noir macro code", note_type)),
403-
span: impl_span
404+
span: Some(impl_span)
404405
});
405406
}
406407

@@ -423,7 +424,7 @@ fn generate_note_deserialize_content(
423424
note_fields: &[(String, String)],
424425
note_serialize_len: &String,
425426
note_header_field_name: &String,
426-
impl_span: Option<Span>,
427+
impl_span: Span,
427428
empty_spans: bool,
428429
) -> Result<NoirFunction, AztecMacroError> {
429430
let function_source = generate_note_deserialize_content_source(
@@ -438,13 +439,13 @@ fn generate_note_deserialize_content(
438439
dbg!(errors);
439440
return Err(AztecMacroError::CouldNotImplementNoteInterface {
440441
secondary_message: Some("Failed to parse Noir macro code (fn deserialize_content). This is either a bug in the compiler or the Noir macro code".to_string()),
441-
span: impl_span
442+
span: Some(impl_span)
442443
});
443444
}
444445

445446
let mut function_ast = function_ast.into_sorted();
446447
let mut noir_fn = function_ast.functions.remove(0);
447-
noir_fn.def.span = impl_span.unwrap_or_default();
448+
noir_fn.def.span = impl_span;
448449
noir_fn.def.visibility = ItemVisibility::Public;
449450
Ok(noir_fn)
450451
}
@@ -461,7 +462,7 @@ fn generate_note_serialize_content(
461462
note_fields: &[(String, String)],
462463
note_serialize_len: &String,
463464
note_header_field_name: &String,
464-
impl_span: Option<Span>,
465+
impl_span: Span,
465466
empty_spans: bool,
466467
) -> Result<NoirFunction, AztecMacroError> {
467468
let function_source = generate_note_serialize_content_source(
@@ -476,13 +477,13 @@ fn generate_note_serialize_content(
476477
dbg!(errors);
477478
return Err(AztecMacroError::CouldNotImplementNoteInterface {
478479
secondary_message: Some("Failed to parse Noir macro code (fn serialize_content). This is either a bug in the compiler or the Noir macro code".to_string()),
479-
span: impl_span
480+
span: Some(impl_span)
480481
});
481482
}
482483

483484
let mut function_ast = function_ast.into_sorted();
484485
let mut noir_fn = function_ast.functions.remove(0);
485-
noir_fn.def.span = impl_span.unwrap_or_default();
486+
noir_fn.def.span = impl_span;
486487
noir_fn.def.visibility = ItemVisibility::Public;
487488
Ok(noir_fn)
488489
}
@@ -492,7 +493,7 @@ fn generate_note_properties_fn(
492493
note_type: &str,
493494
note_fields: &[(String, String)],
494495
note_header_field_name: &String,
495-
impl_span: Option<Span>,
496+
impl_span: Span,
496497
empty_spans: bool,
497498
) -> Result<NoirFunction, AztecMacroError> {
498499
let function_source =
@@ -502,12 +503,12 @@ fn generate_note_properties_fn(
502503
dbg!(errors);
503504
return Err(AztecMacroError::CouldNotImplementNoteInterface {
504505
secondary_message: Some("Failed to parse Noir macro code (fn properties). This is either a bug in the compiler or the Noir macro code".to_string()),
505-
span: impl_span
506+
span: Some(impl_span)
506507
});
507508
}
508509
let mut function_ast = function_ast.into_sorted();
509510
let mut noir_fn = function_ast.functions.remove(0);
510-
noir_fn.def.span = impl_span.unwrap_or_default();
511+
noir_fn.def.span = impl_span;
511512
noir_fn.def.visibility = ItemVisibility::Public;
512513
Ok(noir_fn)
513514
}
@@ -519,7 +520,7 @@ fn generate_note_properties_fn(
519520
//
520521
fn generate_compute_note_hiding_point(
521522
note_type: &String,
522-
impl_span: Option<Span>,
523+
impl_span: Span,
523524
empty_spans: bool,
524525
) -> Result<NoirFunction, AztecMacroError> {
525526
// TODO(#7771): update this to do only 1 MSM call
@@ -541,12 +542,12 @@ fn generate_compute_note_hiding_point(
541542
dbg!(errors);
542543
return Err(AztecMacroError::CouldNotImplementNoteInterface {
543544
secondary_message: Some("Failed to parse Noir macro code (fn compute_note_hiding_point). This is either a bug in the compiler or the Noir macro code".to_string()),
544-
span: impl_span
545+
span: Some(impl_span)
545546
});
546547
}
547548
let mut function_ast = function_ast.into_sorted();
548549
let mut noir_fn = function_ast.functions.remove(0);
549-
noir_fn.def.span = impl_span.unwrap_or_default();
550+
noir_fn.def.span = impl_span;
550551
noir_fn.def.visibility = ItemVisibility::Public;
551552
Ok(noir_fn)
552553
}

aztec_macros/src/transforms/storage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub fn generate_storage_implementation(
238238
vec![generic_context_type.clone()],
239239
true,
240240
),
241-
span: Some(Span::default()),
241+
span: Span::default(),
242242
},
243243
type_span: Span::default(),
244244
generics: vec![generic_context_ident.into()],

aztec_macros/src/utils/ast_utils.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,14 @@ pub fn assignment_with_type(
108108
}
109109

110110
pub fn return_type(path: Path) -> FunctionReturnType {
111-
let ty = make_type(UnresolvedTypeData::Named(path, vec![], true));
111+
let ty = make_type(UnresolvedTypeData::Named(path, Default::default(), true));
112112
FunctionReturnType::Ty(ty)
113113
}
114114

115115
pub fn lambda(parameters: Vec<(Pattern, UnresolvedType)>, body: Expression) -> Expression {
116116
expression(ExpressionKind::Lambda(Box::new(Lambda {
117117
parameters,
118-
return_type: UnresolvedType {
119-
typ: UnresolvedTypeData::Unspecified,
120-
span: Some(Span::default()),
121-
},
118+
return_type: UnresolvedType { typ: UnresolvedTypeData::Unspecified, span: Span::default() },
122119
body,
123120
})))
124121
}
@@ -179,7 +176,7 @@ pub fn cast(lhs: Expression, ty: UnresolvedTypeData) -> Expression {
179176
}
180177

181178
pub fn make_type(typ: UnresolvedTypeData) -> UnresolvedType {
182-
UnresolvedType { typ, span: Some(Span::default()) }
179+
UnresolvedType { typ, span: Span::default() }
183180
}
184181

185182
pub fn index_array(array: Ident, index: &str) -> Expression {

compiler/noirc_frontend/src/ast/expression.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ impl UnresolvedGeneric {
6969
pub fn span(&self) -> Span {
7070
match self {
7171
UnresolvedGeneric::Variable(ident) => ident.0.span(),
72-
UnresolvedGeneric::Numeric { ident, typ } => {
73-
ident.0.span().merge(typ.span.unwrap_or_default())
74-
}
72+
UnresolvedGeneric::Numeric { ident, typ } => ident.0.span().merge(typ.span),
7573
UnresolvedGeneric::Resolved(_, span) => *span,
7674
}
7775
}
@@ -791,7 +789,7 @@ impl FunctionDefinition {
791789
visibility: Visibility::Private,
792790
pattern: Pattern::Identifier(ident.clone()),
793791
typ: unresolved_type.clone(),
794-
span: ident.span().merge(unresolved_type.span.unwrap()),
792+
span: ident.span().merge(unresolved_type.span),
795793
})
796794
.collect();
797795

@@ -848,7 +846,7 @@ impl FunctionReturnType {
848846
pub fn get_type(&self) -> Cow<UnresolvedType> {
849847
match self {
850848
FunctionReturnType::Default(span) => {
851-
Cow::Owned(UnresolvedType { typ: UnresolvedTypeData::Unit, span: Some(*span) })
849+
Cow::Owned(UnresolvedType { typ: UnresolvedTypeData::Unit, span: *span })
852850
}
853851
FunctionReturnType::Ty(typ) => Cow::Borrowed(typ),
854852
}

compiler/noirc_frontend/src/ast/function.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ impl NoirFunction {
6161

6262
pub fn return_type(&self) -> UnresolvedType {
6363
match &self.def.return_type {
64-
FunctionReturnType::Default(_) => {
65-
UnresolvedType::without_span(UnresolvedTypeData::Unit)
66-
}
64+
FunctionReturnType::Default(span) => UnresolvedTypeData::Unit.with_span(*span),
6765
FunctionReturnType::Ty(ty) => ty.clone(),
6866
}
6967
}

compiler/noirc_frontend/src/ast/mod.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ pub enum UnresolvedTypeData {
148148
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
149149
pub struct UnresolvedType {
150150
pub typ: UnresolvedTypeData,
151-
152-
// The span is None in the cases where the User omitted a type:
153-
// fn Foo() {} --- return type is UnresolvedType::Unit without a span
154-
// let x = 100; --- type is UnresolvedType::Unspecified without a span
155-
pub span: Option<Span>,
151+
pub span: Span,
156152
}
157153

158154
/// Type wrapper for a member access
@@ -184,7 +180,7 @@ pub enum UnresolvedTypeExpression {
184180

185181
impl Recoverable for UnresolvedType {
186182
fn error(span: Span) -> Self {
187-
UnresolvedType { typ: UnresolvedTypeData::Error, span: Some(span) }
183+
UnresolvedType { typ: UnresolvedTypeData::Error, span }
188184
}
189185
}
190186

@@ -280,14 +276,6 @@ impl UnresolvedType {
280276
}
281277
}
282278

283-
pub fn without_span(typ: UnresolvedTypeData) -> UnresolvedType {
284-
UnresolvedType { typ, span: None }
285-
}
286-
287-
pub fn unspecified() -> UnresolvedType {
288-
UnresolvedType { typ: UnresolvedTypeData::Unspecified, span: None }
289-
}
290-
291279
pub(crate) fn is_type_expression(&self) -> bool {
292280
matches!(&self.typ, UnresolvedTypeData::Expression(_))
293281
}
@@ -309,7 +297,7 @@ impl UnresolvedTypeData {
309297
}
310298

311299
pub fn with_span(&self, span: Span) -> UnresolvedType {
312-
UnresolvedType { typ: self.clone(), span: Some(span) }
300+
UnresolvedType { typ: self.clone(), span }
313301
}
314302
}
315303

0 commit comments

Comments
 (0)