Skip to content

add: optional-fn-return-type - #406

Open
xonx4l wants to merge 2 commits into
rust-lang:mainfrom
xonx4l:optional-fn-return-type
Open

add: optional-fn-return-type#406
xonx4l wants to merge 2 commits 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

@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 thread crates/formality-macros/src/parse.rs Outdated
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?

Comment thread crates/formality-rust/src/grammar/ty.rs Outdated
}
}

/// 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!

@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.

(Still waiting for change to have an OutputTy that carries the Default impl.)

View changes since this review

@xonx4l

xonx4l commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@nikomatsakis I am sorry , I really got a bit busy and forgot about this . I will try to implement the suggested changes asap. Thank you!

@nikomatsakis

Copy link
Copy Markdown
Contributor

@xonx4l no worries!

@rustbot

This comment has been minimized.

@xonx4l
xonx4l force-pushed the optional-fn-return-type branch from 84015de to cb28bf5 Compare August 24, 2026 17:54
@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@xonx4l

xonx4l commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

@nikomatsakis sorry it took a while , made all the requested changes . Thank you!

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