Skip to content

[legacy] [incorrect approach] pretty-print inherent projections "correctly" - #4

Closed
fmease wants to merge 1 commit into
masterfrom
pp-inh-proj-legacy
Closed

fmease wants to merge 1 commit into
masterfrom
pp-inh-proj-legacy

Conversation

@fmease

@fmease fmease commented May 17, 2023 •

Copy link
Copy Markdown
Owner

Given the these anonymous constants

  • Parent::<1>::Mapping::<{ 2 * 5 }>::{constant#0}, substs: [1, {2 * 5}] (1 parent subst, 1 own subst)
  • <impl at dump_mir_2.rs:5:1: 5:37>::Mapping::{constant#0}, substs: [] (0 parent substs, 0 own substs)

the branch added in this PR & labeled // Inherent projections is not taken (which is good) since

if let DefPathData::TypeNs(_) = def_path_data // true
    && let DefKind::Impl { of_trait: false } = self.tcx().def_kind(parent_def_id) // true
    && let Some(self_ty) = substs.get(0).and_then(|subst| subst.as_type()) // FALSE (!)

However, when the first parent subst is a type, we incorrectly assume it to be the self type and
the branch is taken which is bad.

For example:

#![feature(inherent_associated_types, generic_const_exprs)]

struct Parent<T, const A: usize>(T);

impl<T, const A: usize> Parent<T, A> {
    type Mapping<const I: usize> = Store<{ A + I }>
    where
        [(); A + I]:
    ;
}

struct Store<const N: usize>;

impl<const N: usize> Store<N> {
    fn reify() -> usize {
        N
    }
}

fn main() {
    let _ = Parent::<(), 3>::Mapping::<{ 2 * 5 }>::reify();
}

Here (via -Zdump-mir=all) we incorrectly print the anonymous constant corresp. to A + I (I guess) inside of Store<{…}> assigned to Mapping as <()>::Mapping::<3, { 2 * 5 }>::{constant#0} instead of Parent::<(), 3>::Mapping::<{ 2 * 5 }>::{constant#0}.

Conclusion: Don't use print_def_path for inherent projections!

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant