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

Don't substitute a GAT that has mismatched generics in OpaqueTypeCollector #112876

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Jun 21, 2023

Fixes #111828

I didn't put up minimized UI tests for #112510 or #112873 because they'd minimize to literally the same code, but with different substs on the trait/impl. I don't think that warrants duplicate tests given the nature of the fix.

r? @oli-obk


Side-note: I checked, and this isn't fixed by #112652 -- I think we discussed whether or not that PR fixed it either intentionally or by accident. The code here isn't really touched by that PR either as far as I can tell?

Also, sorry, did some other drive-bys. Hope it doesn't make rebasing #112652 too difficult 😅

@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 Jun 21, 2023
@rust-cloud-vms rust-cloud-vms bot force-pushed the check-subst-compat-in-OpaqueTypeCollector branch from 65f0898 to 9932eab Compare June 21, 2023 05:38
Comment on lines -2381 to -2386
// Verify that the trait item and its implementation have compatible substs lists
fn check_substs_compatible<'tcx>(
tcx: TyCtxt<'tcx>,
assoc_item: ty::AssocItem,
substs: ty::SubstsRef<'tcx>,
) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Future nit: please do such moves in separate commits

Comment on lines +115 to +118
// If the trait ref of the associated item and the impl differs,
// then we can't use the impl's identity substitutions below, so
// just skip.
if alias_ty.trait_ref(self.tcx) == parent_trait_ref {
Copy link
Contributor

Choose a reason for hiding this comment

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

how does that happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

you can have something like -> <Self as Trait<NotIdentitySubsts...>>::Assoc, though I'm not sure if it ever results in anything but a cycle.

Copy link
Member Author

@compiler-errors compiler-errors Jun 21, 2023

Choose a reason for hiding this comment

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

Actually I think I can come up with an example where the old implementation results in surprising behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, here's an example that should error but does not:

#![feature(impl_trait_in_assoc_type)]

trait Foo<T> {
    type Assoc;

    fn test() -> u32;
}

struct DefinesOpaque;
impl Foo<DefinesOpaque> for () {
    type Assoc = impl Sized;

    fn test() -> <() as Foo<NoOpaques>>::Assoc {
        let _: <Self as Foo<DefinesOpaque>>::Assoc = "";
    
        1
    }
}

struct NoOpaques;
impl Foo<NoOpaques> for () {
    type Assoc = u32;

    fn test() -> u32 { 1 }
}

Comment on lines +127 to +131
// If the type is further specializable, then the type_of
// is not actually correct below.
if !assoc.defaultness(self.tcx).is_final() {
continue;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

At this point of complexity, should we just figure out how to normalize?

I tried before, but it requires duplicating some of the normalization logic 🙃

Basically I'd want exactly what the normalizer does, but be able to hook into fold_ty

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, I don't think there is a good way of doing it right now...

Do you have any examples where this current approach of manually substituting is not enough? Like code that isn't currently passing but we want it to?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope. I was just hoping to always go the normalization route like I need to with TAITs

@rust-cloud-vms rust-cloud-vms bot force-pushed the check-subst-compat-in-OpaqueTypeCollector branch from 9932eab to c4cd607 Compare June 21, 2023 16:42
@oli-obk
Copy link
Contributor

oli-obk commented Jun 21, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jun 21, 2023

📌 Commit c4cd607 has been approved by oli-obk

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-review Status: Awaiting review from the assignee but also interested parties. labels Jun 21, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 22, 2023
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#112876 (Don't substitute a GAT that has mismatched generics in `OpaqueTypeCollector`)
 - rust-lang#112906 (rustdoc: render the body of associated types before the where-clause)
 - rust-lang#112907 (Update cargo)
 - rust-lang#112908 (Print def_id on EarlyBoundRegion debug)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit cc93c5f into rust-lang:master Jun 22, 2023
11 checks passed
@rustbot rustbot added this to the 1.72.0 milestone Jun 22, 2023
@compiler-errors compiler-errors deleted the check-subst-compat-in-OpaqueTypeCollector branch August 11, 2023 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

ICE: impl_trait_in_assoc_type: expected type for .. but found Lifetime ...
4 participants