Skip to content

Commit 7a8329f

Browse files
authored
Rollup merge of #59894 - Xanewok:save-assoc-ty-qpath, r=eddyb
save-analysis: Pull associated type definition using `qpath_def` Closes rust-lang/rls#1390 This (probably?) fixes the case where we run the save-analysis code on the following snippet: ```rust trait Test<'a> { type Thing: Test2; } trait Test2 { fn print(); } #[allow(unused)] fn example<T>(t: T) where T: for<'a> Test<'a> { T::Thing::print(); //~ ERROR cannot extract an associated type from a higher-ranked trait bound in this context // ^ only errors in save-analysis mode } ``` The chain is as follows: - culprit is `hir_ty_to_ty` - which calls `ast_ty_to_ty` in the `ItemCtxt` - which calls `associated_path_to_ty` - which finally fails on `projected_ty_from_poly_trait_ref` https://github.com/rust-lang/rust/blob/3de0106789468b211bcc3a25c09c0cf07119186d/src/librustc_typeck/collect.rs#L212-L224 I'm not exactly sure why `hir_ty_to_ty` fails - is it because it needs more setup from typeck to work correctly? I'm guessing the fix is okay since we just pull the already typeck'd data (we run save-analysis after all the analysis passes are complete) from the tables. With this change we can 'go to def' on all segments in the `T::Thing::print()` path.
2 parents dddcd92 + bcd263e commit 7a8329f

File tree

2 files changed

+5
-21
lines changed

2 files changed

+5
-21
lines changed

src/librustc_save_analysis/lib.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc::middle::cstore::ExternCrate;
2323
use rustc::session::config::{CrateType, Input, OutputType};
2424
use rustc::ty::{self, DefIdTree, TyCtxt};
2525
use rustc::{bug, span_bug};
26-
use rustc_typeck::hir_ty_to_ty;
2726
use rustc_codegen_utils::link::{filename_for_metadata, out_filename};
2827
use rustc_data_structures::sync::Lrc;
2928

@@ -648,6 +647,10 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
648647
Node::Pat(&hir::Pat {
649648
node: hir::PatKind::TupleStruct(ref qpath, ..),
650649
..
650+
}) |
651+
Node::Ty(&hir::Ty {
652+
node: hir::TyKind::Path(ref qpath),
653+
..
651654
}) => {
652655
let hir_id = self.tcx.hir().node_to_hir_id(id);
653656
self.tables.qpath_def(qpath, hir_id)
@@ -658,25 +661,6 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
658661
..
659662
}) => HirDef::Local(self.tcx.hir().hir_to_node_id(canonical_id)),
660663

661-
Node::Ty(ty) => if let hir::Ty {
662-
node: hir::TyKind::Path(ref qpath),
663-
..
664-
} = *ty
665-
{
666-
match *qpath {
667-
hir::QPath::Resolved(_, ref path) => path.def,
668-
hir::QPath::TypeRelative(..) => {
669-
let ty = hir_ty_to_ty(self.tcx, ty);
670-
if let ty::Projection(proj) = ty.sty {
671-
return HirDef::AssociatedTy(proj.item_def_id);
672-
}
673-
HirDef::Err
674-
}
675-
}
676-
} else {
677-
HirDef::Err
678-
},
679-
680664
_ => HirDef::Err,
681665
}
682666
}

src/librustc_typeck/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
379379
}
380380
}
381381

382-
/// A quasi-deprecated helper used in rustdoc and save-analysis to get
382+
/// A quasi-deprecated helper used in rustdoc and clippy to get
383383
/// the type from a HIR node.
384384
pub fn hir_ty_to_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, hir_ty: &hir::Ty) -> Ty<'tcx> {
385385
// In case there are any projections etc, find the "environment"

0 commit comments

Comments
 (0)