Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make spans non-optional on UnresolvedType #5773

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn create_inputs(ty: &str) -> Param {
let path_snippet = ty.to_case(Case::Snake); // e.g. private_context_inputs
let type_path = chained_dep!("aztec", "context", "inputs", &path_snippet, ty);

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

Param { pattern: context_pattern, typ: context_type, visibility, span: Span::default() }
Expand Down Expand Up @@ -396,7 +396,7 @@ fn serialize_to_hasher(
Signedness::Unsigned,
ast::IntegerBitSize::ThirtyTwo,
),
span: None,
span: Span::default(),
},
hasher_name,
))
Expand Down Expand Up @@ -595,7 +595,7 @@ fn abstract_return_values(func: &NoirFunction) -> Result<Option<Vec<Statement>>,
serialize_to_hasher(&ident(return_value_name), &current_return_type, hasher_name)
.ok_or_else(|| AztecMacroError::UnsupportedFunctionReturnType {
typ: current_return_type.clone(),
span: func.return_type().span.unwrap_or_default(),
span: func.return_type().span,
})?;

replacement_statements.extend(serialization_statements);
Expand Down
59 changes: 30 additions & 29 deletions aztec_macros/src/transforms/note_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ pub fn generate_note_interface_impl(
note_struct.name.0.contents
)),
})?;
let note_interface_impl_span: Option<Span> =
if empty_spans { None } else { trait_impl.object_type.span };
let note_interface_impl_span =
if empty_spans { Span::default() } else { trait_impl.object_type.span };

// Look for the note struct implementation, generate a default one if it doesn't exist (in order to append methods to it)
let existing_impl = module.impls.iter_mut().find(|r#impl| match &r#impl.object_type.typ {
UnresolvedTypeData::Named(path, _, _) => path.last_ident().eq(&note_struct.name),
Expand Down Expand Up @@ -94,7 +95,7 @@ pub fn generate_note_interface_impl(
Ok(val.to_string())
}
_ => Err(AztecMacroError::CouldNotImplementNoteInterface {
span: trait_impl.object_type.span,
span: Some(trait_impl.object_type.span),
secondary_message: Some(format!(
"NoteInterface must be generic over NOTE_LEN and NOTE_BYTES_LEN: {}",
note_type
Expand Down Expand Up @@ -231,7 +232,7 @@ fn generate_note_to_be_bytes(
note_type: &String,
byte_length: &str,
serialized_length: &str,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let function_source = format!(
Expand Down Expand Up @@ -268,21 +269,21 @@ fn generate_note_to_be_bytes(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}

let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}

fn generate_note_get_header(
note_type: &String,
note_header_field_name: &String,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let function_source = format!(
Expand All @@ -300,21 +301,21 @@ fn generate_note_get_header(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}

let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}

fn generate_note_set_header(
note_type: &String,
note_header_field_name: &String,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let function_source = format!(
Expand All @@ -331,13 +332,13 @@ fn generate_note_set_header(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}

let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}
Expand All @@ -346,7 +347,7 @@ fn generate_note_set_header(
// of the conversion of the characters in the note's struct name to unsigned integers.
fn generate_get_note_type_id(
note_type_id: u32,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
// TODO(#7165): replace {} with dep::aztec::protocol_types::abis::note_selector::compute_note_selector(\"{}\") in the function source below
Expand All @@ -365,13 +366,13 @@ fn generate_get_note_type_id(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}

let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}
Expand All @@ -389,7 +390,7 @@ fn generate_note_properties_struct(
note_type: &str,
note_fields: &[(String, String)],
note_header_field_name: &String,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirStruct, AztecMacroError> {
let struct_source =
Expand All @@ -400,7 +401,7 @@ fn generate_note_properties_struct(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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)),
span: impl_span
span: Some(impl_span)
});
}

Expand All @@ -423,7 +424,7 @@ fn generate_note_deserialize_content(
note_fields: &[(String, String)],
note_serialize_len: &String,
note_header_field_name: &String,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let function_source = generate_note_deserialize_content_source(
Expand All @@ -438,13 +439,13 @@ fn generate_note_deserialize_content(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}

let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}
Expand All @@ -461,7 +462,7 @@ fn generate_note_serialize_content(
note_fields: &[(String, String)],
note_serialize_len: &String,
note_header_field_name: &String,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let function_source = generate_note_serialize_content_source(
Expand All @@ -476,13 +477,13 @@ fn generate_note_serialize_content(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}

let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}
Expand All @@ -492,7 +493,7 @@ fn generate_note_properties_fn(
note_type: &str,
note_fields: &[(String, String)],
note_header_field_name: &String,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
let function_source =
Expand All @@ -502,12 +503,12 @@ fn generate_note_properties_fn(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}
let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}
Expand All @@ -519,7 +520,7 @@ fn generate_note_properties_fn(
//
fn generate_compute_note_hiding_point(
note_type: &String,
impl_span: Option<Span>,
impl_span: Span,
empty_spans: bool,
) -> Result<NoirFunction, AztecMacroError> {
// TODO(#7771): update this to do only 1 MSM call
Expand All @@ -541,12 +542,12 @@ fn generate_compute_note_hiding_point(
dbg!(errors);
return Err(AztecMacroError::CouldNotImplementNoteInterface {
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()),
span: impl_span
span: Some(impl_span)
});
}
let mut function_ast = function_ast.into_sorted();
let mut noir_fn = function_ast.functions.remove(0);
noir_fn.def.span = impl_span.unwrap_or_default();
noir_fn.def.span = impl_span;
noir_fn.def.visibility = ItemVisibility::Public;
Ok(noir_fn)
}
Expand Down
2 changes: 1 addition & 1 deletion aztec_macros/src/transforms/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
///
/// To:
///
/// impl<Context> Storage<Contex> {

Check warning on line 174 in aztec_macros/src/transforms/storage.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Contex)
/// fn init(context: Context) -> Self {
/// Storage {
/// a_map: Map::new(context, 0, |context, slot| {
Expand Down Expand Up @@ -238,7 +238,7 @@
vec![generic_context_type.clone()],
true,
),
span: Some(Span::default()),
span: Span::default(),
},
type_span: Span::default(),
generics: vec![generic_context_ident.into()],
Expand Down
9 changes: 3 additions & 6 deletions aztec_macros/src/utils/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,14 @@ pub fn assignment_with_type(
}

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

pub fn lambda(parameters: Vec<(Pattern, UnresolvedType)>, body: Expression) -> Expression {
expression(ExpressionKind::Lambda(Box::new(Lambda {
parameters,
return_type: UnresolvedType {
typ: UnresolvedTypeData::Unspecified,
span: Some(Span::default()),
},
return_type: UnresolvedType { typ: UnresolvedTypeData::Unspecified, span: Span::default() },
body,
})))
}
Expand Down Expand Up @@ -179,7 +176,7 @@ pub fn cast(lhs: Expression, ty: UnresolvedTypeData) -> Expression {
}

pub fn make_type(typ: UnresolvedTypeData) -> UnresolvedType {
UnresolvedType { typ, span: Some(Span::default()) }
UnresolvedType { typ, span: Span::default() }
}

pub fn index_array(array: Ident, index: &str) -> Expression {
Expand Down
8 changes: 3 additions & 5 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ impl UnresolvedGeneric {
pub fn span(&self) -> Span {
match self {
UnresolvedGeneric::Variable(ident) => ident.0.span(),
UnresolvedGeneric::Numeric { ident, typ } => {
ident.0.span().merge(typ.span.unwrap_or_default())
}
UnresolvedGeneric::Numeric { ident, typ } => ident.0.span().merge(typ.span),
UnresolvedGeneric::Resolved(_, span) => *span,
}
}
Expand Down Expand Up @@ -791,7 +789,7 @@ impl FunctionDefinition {
visibility: Visibility::Private,
pattern: Pattern::Identifier(ident.clone()),
typ: unresolved_type.clone(),
span: ident.span().merge(unresolved_type.span.unwrap()),
span: ident.span().merge(unresolved_type.span),
})
.collect();

Expand Down Expand Up @@ -848,7 +846,7 @@ impl FunctionReturnType {
pub fn get_type(&self) -> Cow<UnresolvedType> {
match self {
FunctionReturnType::Default(span) => {
Cow::Owned(UnresolvedType { typ: UnresolvedTypeData::Unit, span: Some(*span) })
Cow::Owned(UnresolvedType { typ: UnresolvedTypeData::Unit, span: *span })
}
FunctionReturnType::Ty(typ) => Cow::Borrowed(typ),
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/noirc_frontend/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ impl NoirFunction {

pub fn return_type(&self) -> UnresolvedType {
match &self.def.return_type {
FunctionReturnType::Default(_) => {
UnresolvedType::without_span(UnresolvedTypeData::Unit)
}
FunctionReturnType::Default(span) => UnresolvedTypeData::Unit.with_span(*span),
FunctionReturnType::Ty(ty) => ty.clone(),
}
}
Expand Down
18 changes: 3 additions & 15 deletions compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
/*unconstrained:*/ bool,
),

/// The type of quoted code for metaprogramming

Check warning on line 133 in compiler/noirc_frontend/src/ast/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (metaprogramming)
Quoted(crate::QuotedType),

/// An "as Trait" path leading to an associated type.
Expand All @@ -148,11 +148,7 @@
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct UnresolvedType {
pub typ: UnresolvedTypeData,

// The span is None in the cases where the User omitted a type:
// fn Foo() {} --- return type is UnresolvedType::Unit without a span
// let x = 100; --- type is UnresolvedType::Unspecified without a span
pub span: Option<Span>,
pub span: Span,
}

/// Type wrapper for a member access
Expand Down Expand Up @@ -184,7 +180,7 @@

impl Recoverable for UnresolvedType {
fn error(span: Span) -> Self {
UnresolvedType { typ: UnresolvedTypeData::Error, span: Some(span) }
UnresolvedType { typ: UnresolvedTypeData::Error, span }
}
}

Expand Down Expand Up @@ -280,14 +276,6 @@
}
}

pub fn without_span(typ: UnresolvedTypeData) -> UnresolvedType {
UnresolvedType { typ, span: None }
}

pub fn unspecified() -> UnresolvedType {
UnresolvedType { typ: UnresolvedTypeData::Unspecified, span: None }
}

pub(crate) fn is_type_expression(&self) -> bool {
matches!(&self.typ, UnresolvedTypeData::Expression(_))
}
Expand All @@ -309,7 +297,7 @@
}

pub fn with_span(&self, span: Span) -> UnresolvedType {
UnresolvedType { typ: self.clone(), span: Some(span) }
UnresolvedType { typ: self.clone(), span }
}
}

Expand Down Expand Up @@ -432,7 +420,7 @@
Self::Public => write!(f, "pub"),
Self::Private => write!(f, "priv"),
Self::CallData(id) => write!(f, "calldata{id}"),
Self::ReturnData => write!(f, "returndata"),

Check warning on line 423 in compiler/noirc_frontend/src/ast/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (returndata)
}
}
}
Loading
Loading