rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.24k stars 12.57k forks source link

Inference failure with `type_changing_struct_update` #101970

Open crlf0710 opened 2 years ago

crlf0710 commented 2 years ago

In the crater run #98456, fajita crate regression exhibited inference failure. Here's a minimal repro: // when feature gate is enabled below, the compilation fails.

#![feature(type_changing_struct_update)]

#[derive(Default)]
pub struct Foo<P, T> {
    pub t: T,
    pub v: P
}

impl<P: Default, T:Default> Foo<P, T> {
    pub fn o(t: T) -> Self {
        Foo { t, ..Default::default() }
    }
}
crlf0710 commented 2 years ago

Just creating an issue for easier tracking.

cc #86555

nikomatsakis commented 2 years ago

We certainly knew this was a possibility when authoring the RFC -- based on the Zulip conversation, I believe that there were a number of inference failures of this kind, right?

The options on the table that I see are:

My sense was that there was a significant amount of breakage, making the first not an option. In that case, if we want to proceed, we should probably do the work to plan how we can make this an edition change (in particular, can we do migration?). I think we should be able to.

I'm going to nominate this issue to make the @rust-lang/lang team aware, but I'd appreciate it @crlf0710 if you or perhaps @compiler-errors could drop in more details about the scale of breakage found on crater.

nikomatsakis commented 2 years ago

@rustbot labels +I-lang-nomianted

nikomatsakis commented 2 years ago

@rustbot label +I-lang-nominated

WaffleLapkin commented 2 years ago

For some context, there is a zulip thread about finishing work on type-changing struct update.

And here is a MRE of this particular problem:

#![feature(type_changing_struct_update)]

#[derive(Default)]
struct Struct<T>(T);

fn _f() { Struct { 0: (), ..Default::default() }; }

If I understood correctly, @compiler-errors has an idea how we can hack inference to allow such examples. It's a hack though, so we may prefer to just do an editional breakage...

compiler-errors commented 2 years ago

For context, here's the experimentation I have done:

But even with those inference hacks there are still cases where we get failures.

The inference hack is in https://github.com/rust-lang/rust/pull/98456/commits/a6e4db2e6fa559fb9d8955f180bf2f5d7f7a4543 but it's very much a hack.

SparrowLii commented 2 years ago

I understood the cause of the problem to some extent and should be able to submit a PR soon to fix it

SparrowLii commented 2 years ago

@rustbot claim

crlf0710 commented 2 years ago

Posted my thoughts in Zulip https://rust-lang.zulipchat.com/#narrow/stream/293397-project-type-changing-struct-update/topic/Work.20needed.20to.20finish.20out.20RFC/near/299733175

scottmcm commented 1 year ago

We discussed this in the lang meeting this week.

I don't think we had agreement on what the way forward should be, but we had consensus that

@rustbot label -I-lang-nominated

SparrowLii commented 1 year ago

I came up with my solution, but it looks like the language team wants a different approach. So unassigned myself.

Crazytieguy commented 2 months ago

Are there plans for trying to get this in to the 2024 edition?

compiler-errors commented 2 months ago

I don't think so. The inference fallout is really dramatic without hacks, and (speaking as an individual contributor, and not for the whole types team) I don't believe there is really a good, principled hack to implement here that would make the types team happy.