Rollup merge of #59216 - stepnivlk:type_dependent_defs-wrappers, r=oli-obk
Type dependent defs wrappers First of all, forgive me if something would seem lame to you or I offend some rule (although I tried to read through docs), this is my first PR. Issue: https://github.com/rust-lang/rust/issues/59094 This PR adds 3 helper methods to `TypeckTables`: * `opt_type_dependent_def` * `opt_type_dependent_def_id` * `type_dependent_def_id` I didn't add `type_dependent_def` as was proposed in the issue simply because it wasn't used anywhere in the code. Only non-option wrapped`type_dependent_defs()[]` accesses were found in clippy which always called `def_id()` on result. Speaking of clippy, should I open separate PR in its own repo, given it's used as submodule here? Sry it took me so long, as I said I'm new here and I had tough week :).
This commit is contained in:
commit
4720fcac4c
@ -98,8 +98,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
|
||||
}
|
||||
|
||||
fn lookup_and_handle_method(&mut self, id: hir::HirId) {
|
||||
if let Some(def) = self.tables.type_dependent_defs().get(id) {
|
||||
self.check_def_id(def.def_id());
|
||||
if let Some(def_id) = self.tables.type_dependent_def_id(id) {
|
||||
self.check_def_id(def_id);
|
||||
} else {
|
||||
bug!("no type-dependent def for method");
|
||||
}
|
||||
|
@ -559,8 +559,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
|
||||
}
|
||||
ty::Error => { }
|
||||
_ => {
|
||||
if let Some(def) = self.mc.tables.type_dependent_defs().get(call.hir_id) {
|
||||
let def_id = def.def_id();
|
||||
if let Some(def_id) = self.mc.tables.type_dependent_def_id(call.hir_id) {
|
||||
let call_scope = region::Scope {
|
||||
id: call.hir_id.local_id,
|
||||
data: region::ScopeData::Node
|
||||
|
@ -97,7 +97,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {
|
||||
Some(self.tables.qpath_def(qpath, expr.hir_id))
|
||||
}
|
||||
hir::ExprKind::MethodCall(..) => {
|
||||
self.tables.type_dependent_defs().get(expr.hir_id).cloned()
|
||||
self.tables.type_dependent_def(expr.hir_id)
|
||||
}
|
||||
_ => None
|
||||
};
|
||||
|
@ -482,6 +482,15 @@ impl<'tcx> TypeckTables<'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_dependent_def(&self, id: HirId) -> Option<Def> {
|
||||
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
|
||||
self.type_dependent_defs.get(&id.local_id).cloned()
|
||||
}
|
||||
|
||||
pub fn type_dependent_def_id(&self, id: HirId) -> Option<DefId> {
|
||||
self.type_dependent_def(id).map(|def| def.def_id())
|
||||
}
|
||||
|
||||
pub fn type_dependent_defs_mut(&mut self) -> LocalTableInContextMut<'_, Def> {
|
||||
LocalTableInContextMut {
|
||||
local_id_root: self.local_id_root,
|
||||
|
@ -112,7 +112,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
|
||||
}
|
||||
},
|
||||
hir::ExprKind::MethodCall(..) => {
|
||||
cx.tables.type_dependent_defs().get(expr.hir_id).cloned()
|
||||
cx.tables.type_dependent_def(expr.hir_id)
|
||||
},
|
||||
_ => None
|
||||
};
|
||||
|
@ -834,13 +834,11 @@ fn method_callee<'a, 'gcx, 'tcx>(
|
||||
let (def_id, substs, user_ty) = match overloaded_callee {
|
||||
Some((def_id, substs)) => (def_id, substs, None),
|
||||
None => {
|
||||
let type_dependent_defs = cx.tables().type_dependent_defs();
|
||||
let def = type_dependent_defs
|
||||
.get(expr.hir_id)
|
||||
let def = cx.tables().type_dependent_def(expr.hir_id)
|
||||
.unwrap_or_else(|| {
|
||||
span_bug!(expr.span, "no type-dependent def for method callee")
|
||||
});
|
||||
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, def);
|
||||
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
|
||||
debug!("method_callee: user_ty={:?}", user_ty);
|
||||
(def.def_id(), cx.tables().node_substs(expr.hir_id), user_ty)
|
||||
}
|
||||
|
@ -405,8 +405,7 @@ fn check_expr_kind<'a, 'tcx>(
|
||||
for index in hirvec.iter() {
|
||||
method_call_result &= v.check_expr(index);
|
||||
}
|
||||
if let Some(def) = v.tables.type_dependent_defs().get(e.hir_id) {
|
||||
let def_id = def.def_id();
|
||||
if let Some(def_id) = v.tables.type_dependent_def_id(e.hir_id) {
|
||||
match v.tcx.associated_item(def_id).container {
|
||||
ty::ImplContainer(_) => method_call_result & v.handle_const_fn_call(def_id),
|
||||
ty::TraitContainer(_) => NotPromotable,
|
||||
|
@ -1053,8 +1053,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
|
||||
hir::ExprKind::MethodCall(_, span, _) => {
|
||||
// Method calls have to be checked specially.
|
||||
self.span = span;
|
||||
if let Some(def) = self.tables.type_dependent_defs().get(expr.hir_id) {
|
||||
if self.visit(self.tcx.type_of(def.def_id())) {
|
||||
if let Some(def_id) = self.tables.type_dependent_def_id(expr.hir_id) {
|
||||
if self.visit(self.tcx.type_of(def_id)) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
@ -1083,7 +1083,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
|
||||
_ => None,
|
||||
}
|
||||
hir::QPath::TypeRelative(..) => {
|
||||
self.tables.type_dependent_defs().get(id).cloned()
|
||||
self.tables.type_dependent_def(id)
|
||||
}
|
||||
};
|
||||
if let Some(def) = def {
|
||||
|
@ -573,8 +573,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
|
||||
}
|
||||
ast::ExprKind::MethodCall(ref seg, ..) => {
|
||||
let expr_hir_id = self.tcx.hir().definitions().node_to_hir_id(expr.id);
|
||||
let method_id = match self.tables.type_dependent_defs().get(expr_hir_id) {
|
||||
Some(id) => id.def_id(),
|
||||
let method_id = match self.tables.type_dependent_def_id(expr_hir_id) {
|
||||
Some(id) => id,
|
||||
None => {
|
||||
debug!("Could not resolve method id for {:?}", expr);
|
||||
return None;
|
||||
|
@ -4802,10 +4802,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
(self.to_ty(qself), qself, segment)
|
||||
}
|
||||
};
|
||||
if let Some(cached_def) = self.tables.borrow().type_dependent_defs().get(hir_id) {
|
||||
if let Some(cached_def) = self.tables.borrow().type_dependent_def(hir_id) {
|
||||
// Return directly on cache hit. This is useful to avoid doubly reporting
|
||||
// errors with default match binding modes. See #44614.
|
||||
return (*cached_def, Some(ty), slice::from_ref(&**item_segment))
|
||||
return (cached_def, Some(ty), slice::from_ref(&**item_segment))
|
||||
}
|
||||
let item_name = item_segment.ident;
|
||||
let def = match self.resolve_ufcs(span, item_name, ty, hir_id) {
|
||||
|
Loading…
Reference in New Issue
Block a user