Skip to content

Commit e8644d0

Browse files
authored
feat: allow fn returning () without having to write -> () (#7717)
1 parent df20280 commit e8644d0

File tree

3 files changed

+20
-4
lines changed
  • compiler/noirc_frontend/src/parser/parser
  • test_programs/execution_success/reference_only_used_as_alias/src
  • tooling/nargo_fmt/src/formatter

3 files changed

+20
-4
lines changed

compiler/noirc_frontend/src/parser/parser/types.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ impl Parser<'_> {
314314
let ret = if self.eat(Token::Arrow) {
315315
self.parse_type_or_error()
316316
} else {
317-
self.expected_token(Token::Arrow);
318317
UnresolvedTypeData::Unit.with_location(self.location_at_previous_token_end())
319318
};
320319

@@ -705,6 +704,16 @@ mod tests {
705704
assert_eq!(ret.typ.to_string(), "Field");
706705
}
707706

707+
#[test]
708+
fn parses_function_type_without_return_type() {
709+
let src = "fn()";
710+
let typ = parse_type_no_errors(src);
711+
let UnresolvedTypeData::Function(_args, ret, _env, _unconstrained) = typ.typ else {
712+
panic!("Expected a function type")
713+
};
714+
assert_eq!(ret.typ.to_string(), "()");
715+
}
716+
708717
#[test]
709718
fn parses_function_type_with_env() {
710719
let src = "fn[Field]() -> Field";

test_programs/execution_success/reference_only_used_as_alias/src/main.nr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ struct ExampleEvent0 {
44
}
55

66
trait EventInterface {
7-
fn emit<Env>(self, _emit: fn[Env](Self) -> ());
7+
fn emit<Env>(self, _emit: fn[Env](Self));
88
}
99

1010
impl EventInterface for ExampleEvent0 {
11-
fn emit<Env>(self: ExampleEvent0, _emit: fn[Env](Self) -> ()) {
11+
fn emit<Env>(self: ExampleEvent0, _emit: fn[Env](Self)) {
1212
_emit(self);
1313
}
1414
}
@@ -56,7 +56,7 @@ where
5656
fn encode_event_with_randomness<Event>(
5757
context: &mut Context,
5858
randomness: Field,
59-
) -> fn[(&mut Context, Field)](Event) -> ()
59+
) -> fn[(&mut Context, Field)](Event)
6060
where
6161
Event: EventInterface,
6262
{

tooling/nargo_fmt/src/formatter/types.rs

+7
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ mod tests {
304304
assert_format_type(src, expected);
305305
}
306306

307+
#[test]
308+
fn format_function_type_without_return_type() {
309+
let src = " fn ( ) ";
310+
let expected = "fn()";
311+
assert_format_type(src, expected);
312+
}
313+
307314
#[test]
308315
fn format_tuple_type_one_type() {
309316
let src = " ( Field , )";

0 commit comments

Comments
 (0)