-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Conversation
This comment has been minimized.
This comment has been minimized.
src/librustc_mir/shim.rs
Outdated
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> { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_mir/shim.rs
Outdated
// (return as Variant).field0 = arg0; | ||
// (return as Variant).field1 = arg1; | ||
// | ||
// discriminant(return) = variant_index; |
There was a problem hiding this comment.
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
?
This comment has been minimized.
This comment has been minimized.
03c42ed
to
0068ea0
Compare
src/test/ui/consts/const_constructor/feature-gate-const_constructor.stderr
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
r=me with feature gates cleaned up |
0068ea0
to
bcf8365
Compare
@bors r=oli-obk |
📌 Commit bcf8365 has been approved by |
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
☀️ Test successful - checks-travis, status-appveyor |
Mir construction special cases
Ctor(...)
to be lowered asCtor { 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