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

Make tuple constructors real const fns #61209

Merged
merged 3 commits into from
Jun 7, 2019

Conversation

matthewjasper
Copy link
Contributor

@matthewjasper matthewjasper commented May 26, 2019

Mir construction special cases Ctor(...) to be lowered as Ctor { 0: ... }, which means this doesn't come up much in practice, but it seems inconsistent not to allow this.

Tracking issue: #61456

r? @oli-obk

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 26, 2019
@Centril Centril added this to the 1.37 milestone May 26, 2019
@Centril Centril added relnotes Marks issues that should be documented in the release notes of the next release. T-lang Relevant to the language team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. labels May 26, 2019
@Centril

This comment has been minimized.

src/librustc_mir/shim.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/mod.rs Show resolved Hide resolved
let gcx = tcx.global_tcx();
let def_id = tcx.hir().local_def_id_from_hir_id(ctor_id);
let param_env = gcx.param_env(def_id);
pub fn build_adt_ctor<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>, ctor_id: DefId) -> &'gcx Mir<'gcx> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change makes me wonder whether it would be possible to generalize build_adt_ctor to the point where we can scrap mir::AggregateKind::Adt and just call the part of build_adt_ctor that generates the field and discr initialization.

Additionally, if I'm reading this correctly, build_adt_ctor could generate a shim that allows initializing arbitrary ty::Adt. So there's nothing implementation wise that would speak against the lang team figuring out a way to obtain initialization functions for struct variants or just plain structs. cc @Centril

Copy link
Contributor

Choose a reason for hiding this comment

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

cc @rust-lang/lang Not suggesting that we add this or anything, but wanted to let you know that we can now generate a function

fn foo(a: A, b: B, c: C) -> Foo {
    Foo { a, b, c }
}

from a struct definition

struct Foo {
    a: A,
    b: B,
    c: C,
}

In case this ever comes up as something that may be useful

Copy link
Member

Choose a reason for hiding this comment

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

@oli-obk FWIW we already have an optimization that expands Rvalue::Aggregate into this instruction sequence so this code is already duplicated, I think?

Copy link
Contributor

Choose a reason for hiding this comment

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

iirc that optimization is very expensive to run. I know we want to do little work at MIR building time, but in this case I believe it's equivalent in complexity. We can scrap that optimization if we just do it all at MIR building time.

Copy link
Member

Choose a reason for hiding this comment

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

I meant that we could put the code generating this in that optimization until we get rid of it (rather than relying on running the optimization).

src/librustc/ty/constness.rs Show resolved Hide resolved
src/test/run-pass/consts/const-construct-call.rs Outdated Show resolved Hide resolved
@Centril Centril removed this from the 1.37 milestone May 26, 2019
@Centril Centril removed needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. labels May 26, 2019
src/librustc_mir/shim.rs Outdated Show resolved Hide resolved
// (return as Variant).field0 = arg0;
// (return as Variant).field1 = arg1;
//
// discriminant(return) = variant_index;
Copy link
Member

Choose a reason for hiding this comment

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

So I guess this is pre-optimized? As opposed to Rvalue::Aggregate?

@bors

This comment has been minimized.

src/librustc_mir/shim.rs Outdated Show resolved Hide resolved
@bors

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 6, 2019

r=me with feature gates cleaned up

@matthewjasper
Copy link
Contributor Author

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Jun 7, 2019

📌 Commit bcf8365 has been approved by oli-obk

@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-review Status: Awaiting review from the assignee but also interested parties. labels Jun 7, 2019
@bors
Copy link
Contributor

bors commented Jun 7, 2019

⌛ Testing commit bcf8365 with merge c1c60d2...

bors added a commit that referenced this pull request Jun 7, 2019
Make tuple constructors real const fns

Mir construction special cases `Ctor(...)` to be lowered as `Ctor { 0: ... }`, which means this doesn't come up much in practice, but it seems inconsistent not to allow this.

r? @oli-obk
@bors
Copy link
Contributor

bors commented Jun 7, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: oli-obk
Pushing c1c60d2 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 7, 2019
@bors bors merged commit bcf8365 into rust-lang:master Jun 7, 2019
@matthewjasper matthewjasper deleted the const-tuple-constructors branch July 29, 2019 19:54
@mexus mexus mentioned this pull request Aug 26, 2019
25 tasks
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-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants