Skip to content

add: optional-fn-return-type#406

Open
xonx4l wants to merge 1 commit into
rust-lang:mainfrom
xonx4l:optional-fn-return-type
Open

add: optional-fn-return-type#406
xonx4l wants to merge 1 commit into
rust-lang:mainfrom
xonx4l:optional-fn-return-type

Conversation

@xonx4l

@xonx4l xonx4l commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR implements optional function return type as discussed in #393

e.g., closes #393

How does it work, what questions do you have?

Functions can now be written fn foo() { } instead of requiringfn foo() -> () { }. When the return type is omitted, it defaults to the unit type ().

Added tests demonstrating that fn run() {trusted} parses identically tofn run() -> () {trusted}.

AI disclosure

  • I did not use any AI tools

@rustbot

rustbot commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks for contributing to formality! :)
A reviewer will take a look at your PR within a week or two. If not, come talk to us on https://rust-lang.zulipchat.com/#narrow/channel/402470-t-types.2Fformality

@xonx4l
xonx4l force-pushed the optional-fn-return-type branch from c3ce639 to 84015de Compare June 25, 2026 11:54

@nikomatsakis nikomatsakis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I have one suggested improvement

View changes since this review

Comment on lines +517 to +519
let rest: Vec<TokenStream> =
chars.map(|c| quote!(__p.expect_char(#c)?;)).collect();
(quote!(__p.expect_char(#first).is_ok()), quote!(#(#rest)*))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let rest: Vec<TokenStream> =
chars.map(|c| quote!(__p.expect_char(#c)?;)).collect();
(quote!(__p.expect_char(#first).is_ok()), quote!(#(#rest)*))
(
quote!(__p.expect_char(#first).is_ok()),
quote!(#(__p.expect_char(#chars)?;)*)
)

I think maybe maybe you can just do this?

}
}

/// The default type is the unit type `()`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I am not sure if we want a default for Ty that is always unit. It's convenient here but it might not be correct elsewhere. Let's have a new type like

struct OutputTy { ty: Ty }

impl Upcast<Ty> for OutputTy { }

impl Default for OutputTy { }

and then change output_ty: OutputTy

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok , will do . Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make function without return type nicer

4 participants