Skip to content

Commit

Permalink
privacy: visit trait def id of projections
Browse files Browse the repository at this point in the history
A refactoring in rust-lang#117076 changed the `DefIdVisitorSkeleton` to avoid
calling `visit_projection_ty` for `ty::Projection` aliases, and instead
just iterate over the args - this makes sense, as `visit_projection_ty`
will indirectly visit all of the same args, but in doing so, will also
create a `TraitRef` containing the trait's `DefId`, which also gets
visited. The trait's `DefId` isn't visited when we only visit the
arguments without separating them into `TraitRef` and own args first.

Signed-off-by: David Wood <david@davidtw.co>
  • Loading branch information
davidtwco committed Dec 7, 2023
1 parent 7df0c21 commit d83d03c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
13 changes: 7 additions & 6 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,21 @@ where
return ControlFlow::Continue(());
}

let kind = match kind {
ty::Inherent | ty::Projection => "associated type",
ty::Weak => "type alias",
ty::Opaque => unreachable!(),
};
self.def_id_visitor.visit_def_id(
data.def_id,
kind,
match kind {
ty::Inherent | ty::Projection => "associated type",
ty::Weak => "type alias",
ty::Opaque => unreachable!(),
},
&LazyDefPathStr { def_id: data.def_id, tcx },
)?;

// This will also visit args if necessary, so we don't need to recurse.
return if V::SHALLOW {
ControlFlow::Continue(())
} else if kind == ty::Projection {
self.visit_projection_ty(data)
} else {
data.args.iter().try_for_each(|subst| subst.visit_with(self))
};
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/privacy/auxiliary/issue-117997.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// no-prefer-dynamic
// compile-flags: --crate-type=rlib

pub use impl_mod::TraitImplementer as Implementer;

pub use trait_mod::get_assoc;

mod impl_mod {
use crate::trait_mod::TraitWithAssocType;

pub struct TraitImplementer {}
pub struct AssociatedType {}

impl AssociatedType {
pub fn method_on_assoc(&self) -> i32 {
todo!()
}
}

impl TraitWithAssocType for TraitImplementer {
type AssocType = AssociatedType;
}
}

mod trait_mod {
use crate::Implementer;

pub fn get_assoc() -> <Implementer as TraitWithAssocType>::AssocType {
todo!()
}

pub trait TraitWithAssocType {
type AssocType;
}
}
8 changes: 8 additions & 0 deletions tests/ui/privacy/issue-117997.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// aux-build:issue-117997.rs
// build-pass

extern crate issue_117997;

pub fn main() {
issue_117997::get_assoc().method_on_assoc();
}

0 comments on commit d83d03c

Please sign in to comment.