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

An associated type magically adds a destructor? #29821

Closed
alevy opened this issue Nov 13, 2015 · 10 comments · Fixed by #81843
Closed

An associated type magically adds a destructor? #29821

alevy opened this issue Nov 13, 2015 · 10 comments · Fixed by #81843
Assignees
Labels
A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alevy
Copy link
Contributor

alevy commented Nov 13, 2015

These two code snippets should functionally be doing the same thing, but the type checker seems to think one has a destructor. The difference seems to be in the reliance on an associated type.

rustc complains that myfoo cannot be declared a mutable static because it has a desctructor:

pub trait Foo {
    type FooAssoc;
}

pub struct Bar<F: Foo> {
    id: F::FooAssoc
}

pub struct Baz;

impl Foo for Baz {
    type FooAssoc = usize;
}

static mut myfoo : Bar<Baz> = Bar { id: 0 };

While this, unsurprisingly, works:

pub struct Bar<A> {
    id: A
}

static mut myfoo : Bar<usize> = Bar { id: 0 };

Here are links to non-compiling/compiling examples on play.rust-lang:

@nikomatsakis nikomatsakis added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 13, 2015
@nikomatsakis
Copy link
Contributor

(@alevy expressed interest in working on this, I will mentor.)

@nikomatsakis nikomatsakis added A-traits Area: Trait system I-wrong labels Nov 13, 2015
@bstrie
Copy link
Contributor

bstrie commented Oct 8, 2016

Triage: still relevant.

@alevy
Copy link
Contributor Author

alevy commented Oct 9, 2016

I'm still interested in helping to fix this. @nikomatsakis you mentioned at one point that you had a hunch for where the problem was. Perhaps a pointer would be enough to get the ball rolling?

@nikomatsakis
Copy link
Contributor

@alevy Hmm. So, the error is emitted by the "constant qualification" code, which walks the MIR for generating a constant value and looks for things that should be disallowed. One of those things is the creation of a struct which has a destructor.

This code is in src/librustc_mir/transform/qualify_consts.rs. I think where I would start is trying to insert some debug! to track out exactly what is happening. My guess would be that we are walking down this branch, but I'm actually not sure.

cc @eddyb, author of the code in question

@eddyb
Copy link
Member

eddyb commented Oct 12, 2016

It might be the type contents, if we use them for "needs drop". Aren't they conservative wrt projections?

@nikomatsakis
Copy link
Contributor

@eddyb that was my first assumption, that it was something like that. When I was poking about in the code I didn't see the place where we branch into that, but maybe I just wasn't reading very carefully.

@eddyb
Copy link
Member

eddyb commented Oct 13, 2016

Qualif::restrict calls into TyCtxt::type_needs_drop_given_env which references #22815.
cc @pnkfelix This could at least try to normalize projections, which would fix the problem here.
Btw we can now remove "type contents" and replace it with two cached flags (UnsafeCell & needs-drop).

@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. and removed I-wrong labels Jul 24, 2017
@ObsidianMinor
Copy link
Contributor

This doesn't seem to be an issue anymore. Running the provided "non-working" code doesn't trigger a compiler error.

@alevy
Copy link
Contributor Author

alevy commented Feb 4, 2021

Indeed, this not longer seems to be an issue. Happy to close the issue :)

@bstrie
Copy link
Contributor

bstrie commented Feb 4, 2021

Should we add a regression test first?

@bstrie bstrie self-assigned this Feb 6, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 7, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 7, 2021
…laumeGomez

Rollup of 5 pull requests

Successful merges:

 - rust-lang#81526 (btree: use Option's unwrap_unchecked())
 - rust-lang#81742 (Add a note about the correctness and the effect on unsafe code to the `ExactSizeIterator` docs)
 - rust-lang#81830 (Add long error explanation for E0542)
 - rust-lang#81835 (Improve long explanation for E0546)
 - rust-lang#81843 (Add regression test for rust-lang#29821)

Failed merges:

 - rust-lang#81836 (Add long explanation for E0547)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in d2a3c04 Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants