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

Add support for gen fn #118457

Merged
merged 12 commits into from
Dec 5, 2023
Merged

Add support for gen fn #118457

merged 12 commits into from
Dec 5, 2023

Conversation

eholk
Copy link
Contributor

@eholk eholk commented Nov 29, 2023

This builds on #116447 to add support for gen fn functions. For the most part we follow the same approach as desugaring async fn, but replacing Future with Iterator and async {} with gen {} for the body.

The version implemented here uses the return type of a gen fn as the yield type. For example:

gen fn count_to_three() -> i32 {
    yield 1;
    yield 2;
    yield 3;
}

In the future, I think we should experiment with a syntax like gen fn count_to_three() yield i32 { ... }, but that can go in another PR.

cc @oli-obk @compiler-errors

@rustbot
Copy link
Collaborator

rustbot commented Nov 29, 2023

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 29, 2023
@eholk
Copy link
Contributor Author

eholk commented Nov 29, 2023

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 29, 2023
@compiler-errors
Copy link
Member

r? compiler-errors

I'll review this

@rustbot rustbot assigned compiler-errors and unassigned b-naber Nov 29, 2023
@compiler-errors
Copy link
Member

compiler-errors commented Nov 29, 2023

Make sure something reasonable happens when the user writes async gen fn

I would not worry too much about this, since I can add support for async gen fn on top of #118420 if this lands first (which it very likely will)

You can make it a diagnostic tho I guess, or just leave it as a span-bug lol. I don't even think we parse async gen fn front matters correctly...?

@rust-log-analyzer

This comment has been minimized.

@eholk
Copy link
Contributor Author

eholk commented Nov 29, 2023

You can make it a diagnostic tho I guess, or just leave it as a span-bug lol. I don't even think we parse async gen fn front matters correctly...?

I don't know, so that's the thing to check :)

The main thing is I want to make sure if you write async gen fn it doesn't silently try to become just an async fn, which is what I suspect will happen with a lot of the code now. If we give an error during parsing, that would be good enough for me.

@rust-log-analyzer

This comment has been minimized.

@@ -2536,6 +2547,8 @@ impl<'a> Parser<'a> {
}
}

// FIXME(eholk): add keyword recovery logic for genness
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm considering leaving this for a later PR that's focused on diagnostic improvements around gen fn, so that this one can focus on the baseline functionality.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's fine I guess

Please change the FIXME though to not reference a single person, since this is associated with feature work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@eholk eholk changed the title WIP: Add support for gen fn Add support for gen fn Dec 1, 2023
@eholk eholk marked this pull request as ready for review December 1, 2023 00:41
@rustbot
Copy link
Collaborator

rustbot commented Dec 1, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@eholk
Copy link
Contributor Author

eholk commented Dec 1, 2023

I'm ready to have this reviewed now.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 1, 2023
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

More nits and reviews

Also raised the question whether this needs to be blocked on the RFC landing or if it is still okay to land this pre-RFC: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Generators.2C.20lang-team.20experiments.2C.20RFCs.2C.20oh.20my!/near/405406740 edit: seems ok

compiler/rustc_builtin_macros/src/test.rs Show resolved Hide resolved
compiler/rustc_ast_passes/src/ast_validation.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/path.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late.rs Outdated Show resolved Hide resolved
@@ -940,7 +942,8 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
this.visit_generics(generics);

let declaration = &sig.decl;
let async_node_id = sig.header.asyncness.opt_return_id();
let async_node_id =
Copy link
Member

Choose a reason for hiding this comment

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

var name

Copy link
Member

Choose a reason for hiding this comment

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

Please add a test that makes sure that gen fn captures lifetimes in its signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added gen_fn_lifetime_capture.rs, but I'm not sure it completely exercises everything it should.

compiler/rustc_resolve/src/def_collector.rs Outdated Show resolved Hide resolved
@@ -2536,6 +2547,8 @@ impl<'a> Parser<'a> {
}
}

// FIXME(eholk): add keyword recovery logic for genness
Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's fine I guess

Please change the FIXME though to not reference a single person, since this is associated with feature work

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 1, 2023
@bors
Copy link
Contributor

bors commented Dec 2, 2023

☔ The latest upstream changes (presumably #118507) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Contributor Author

@eholk eholk left a comment

Choose a reason for hiding this comment

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

I think I've addressed all your comments. I'd appreciate a double check on the lifetime capture test though.

compiler/rustc_ast/src/mut_visit.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/item.rs Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/def_collector.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/late.rs Outdated Show resolved Hide resolved
src/tools/rustfmt/src/closures.rs Outdated Show resolved Hide resolved
@@ -940,7 +942,8 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
this.visit_generics(generics);

let declaration = &sig.decl;
let async_node_id = sig.header.asyncness.opt_return_id();
let async_node_id =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added gen_fn_lifetime_capture.rs, but I'm not sure it completely exercises everything it should.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

In the future, do you mind cleaning up this PR history a bit? Commits like 09f0741 don't really seem necessary -- you could've probably just amended that commit into whatever bad merge you performed in the first place.

@compiler-errors
Copy link
Member

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Dec 5, 2023

📌 Commit 2c8dbd9 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 5, 2023
@eholk
Copy link
Contributor Author

eholk commented Dec 5, 2023

Thanks for the r+!

I'll clean things up better in the future. The various "Fix CI" commits could also be squashed into something else.

@bors
Copy link
Contributor

bors commented Dec 5, 2023

⌛ Testing commit 2c8dbd9 with merge 56278a6...

@bors
Copy link
Contributor

bors commented Dec 5, 2023

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing 56278a6 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 5, 2023
@bors bors merged commit 56278a6 into rust-lang:master Dec 5, 2023
12 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Dec 5, 2023
@eholk eholk deleted the genfn branch December 5, 2023 21:11
@eholk eholk restored the genfn branch December 5, 2023 21:12
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (56278a6): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.5% [3.5%, 3.5%] 1
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.5% [3.5%, 3.5%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 675.106s -> 675.495s (0.06%)
Artifact size: 314.16 MiB -> 314.16 MiB (0.00%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants