From 795fa0a006bed5526e762a8bb11284da96ddf57d Mon Sep 17 00:00:00 2001 From: marmeladema Date: Sun, 12 Apr 2020 21:22:30 +0100 Subject: [PATCH 1/5] Remove useless calls to `assemble_extension_candidates_for_traits_in_scope` Calls to `assemble_extension_candidates_for_traits_in_scope` with `DUMMY_HIR_ID` as `expr_hir_id` are useless because the first thing that this function does is to return `Ok(())` in this case. --- src/librustc_typeck/check/method/probe.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 7e7d84c1996..0b1aff7e806 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -1513,7 +1513,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { ); pcx.allow_similar_names = true; pcx.assemble_inherent_candidates(); - pcx.assemble_extension_candidates_for_traits_in_scope(hir::DUMMY_HIR_ID)?; let method_names = pcx.candidate_method_names(); pcx.allow_similar_names = false; @@ -1523,10 +1522,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { pcx.reset(); pcx.method_name = Some(method_name); pcx.assemble_inherent_candidates(); - pcx.assemble_extension_candidates_for_traits_in_scope(hir::DUMMY_HIR_ID) - .map_or(None, |_| { - pcx.pick_core().and_then(|pick| pick.ok()).map(|pick| pick.item) - }) + pcx.pick_core().and_then(|pick| pick.ok()).map(|pick| pick.item) }) .collect(); From 830e4fde0f11c8e8cf177729efac356429ffb0eb Mon Sep 17 00:00:00 2001 From: marmeladema Date: Mon, 13 Apr 2020 11:12:57 +0100 Subject: [PATCH 2/5] Remove usage of `DUMMY_HIR_ID` in some visitors --- src/librustc_lint/builtin.rs | 8 ++++---- src/librustc_lint/types.rs | 10 +++++----- src/librustc_privacy/lib.rs | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 910d53880f2..627a438c2c3 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1354,7 +1354,7 @@ declare_lint! { } pub struct UnnameableTestItems { - boundary: hir::HirId, // HirId of the item under which things are not nameable + boundary: Option, // HirId of the item under which things are not nameable items_nameable: bool, } @@ -1362,7 +1362,7 @@ impl_lint_pass!(UnnameableTestItems => [UNNAMEABLE_TEST_ITEMS]); impl UnnameableTestItems { pub fn new() -> Self { - Self { boundary: hir::DUMMY_HIR_ID, items_nameable: true } + Self { boundary: None, items_nameable: true } } } @@ -1372,7 +1372,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnameableTestItems { if let hir::ItemKind::Mod(..) = it.kind { } else { self.items_nameable = false; - self.boundary = it.hir_id; + self.boundary = Some(it.hir_id); } return; } @@ -1385,7 +1385,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnameableTestItems { } fn check_item_post(&mut self, _cx: &LateContext<'_, '_>, it: &hir::Item<'_>) { - if !self.items_nameable && self.boundary == it.hir_id { + if !self.items_nameable && self.boundary == Some(it.hir_id) { self.items_nameable = true; } } diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index aa805a2f2db..ee2ed8826ba 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -43,14 +43,14 @@ declare_lint! { #[derive(Copy, Clone)] pub struct TypeLimits { /// Id of the last visited negated expression - negated_expr_id: hir::HirId, + negated_expr_id: Option, } impl_lint_pass!(TypeLimits => [UNUSED_COMPARISONS, OVERFLOWING_LITERALS]); impl TypeLimits { pub fn new() -> TypeLimits { - TypeLimits { negated_expr_id: hir::DUMMY_HIR_ID } + TypeLimits { negated_expr_id: None } } } @@ -244,7 +244,7 @@ fn lint_int_literal<'a, 'tcx>( let int_type = t.normalize(cx.sess().target.ptr_width); let (min, max) = int_ty_range(int_type); let max = max as u128; - let negative = type_limits.negated_expr_id == e.hir_id; + let negative = type_limits.negated_expr_id == Some(e.hir_id); // Detect literal value out of range [min, max] inclusive // avoiding use of -min to prevent overflow/panic @@ -397,8 +397,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { match e.kind { hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => { // propagate negation, if the negation itself isn't negated - if self.negated_expr_id != e.hir_id { - self.negated_expr_id = expr.hir_id; + if self.negated_expr_id != Some(e.hir_id) { + self.negated_expr_id = Some(expr.hir_id); } } hir::ExprKind::Binary(binop, ref l, ref r) => { diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index a6d880667ad..e02e97be19d 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -1012,7 +1012,7 @@ impl DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { struct NamePrivacyVisitor<'a, 'tcx> { tcx: TyCtxt<'tcx>, tables: &'a ty::TypeckTables<'tcx>, - current_item: hir::HirId, + current_item: Option, empty_tables: &'a ty::TypeckTables<'tcx>, } @@ -1028,7 +1028,7 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> { ) { // definition of the field let ident = Ident::new(kw::Invalid, use_ctxt); - let current_hir = self.current_item; + let current_hir = self.current_item.unwrap(); let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did, current_hir).1; if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) { let label = if in_update_syntax { @@ -1074,7 +1074,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> { } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { - let orig_current_item = mem::replace(&mut self.current_item, item.hir_id); + let orig_current_item = mem::replace(&mut self.current_item, Some(item.hir_id)); let orig_tables = mem::replace(&mut self.tables, item_tables(self.tcx, item.hir_id, self.empty_tables)); intravisit::walk_item(self, item); @@ -2059,7 +2059,7 @@ fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: DefId) { let mut visitor = NamePrivacyVisitor { tcx, tables: &empty_tables, - current_item: hir::DUMMY_HIR_ID, + current_item: None, empty_tables: &empty_tables, }; let (module, span, hir_id) = tcx.hir().get_module(module_def_id); From 95fb7bf1081850873a7db513a1087e5cc00897b5 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Sun, 12 Apr 2020 22:49:34 +0100 Subject: [PATCH 3/5] Just `unwrap()` instead of `unwrap_or(DUMMY_HIR_ID)`. A valid hir id should always be returned in this case. --- src/librustc_infer/infer/error_reporting/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs index db81ceea43f..4189570a0da 100644 --- a/src/librustc_infer/infer/error_reporting/mod.rs +++ b/src/librustc_infer/infer/error_reporting/mod.rs @@ -191,7 +191,7 @@ fn msg_span_from_early_bound_and_free_regions( let sm = tcx.sess.source_map(); let scope = region.free_region_binding_scope(tcx); - let node = tcx.hir().as_local_hir_id(scope).unwrap_or(hir::DUMMY_HIR_ID); + let node = tcx.hir().as_local_hir_id(scope).unwrap(); let tag = match tcx.hir().find(node) { Some(Node::Block(_)) | Some(Node::Expr(_)) => "body", Some(Node::Item(it)) => item_scope_tag(&it), From e08ad7ae58b4b94e29a71dedf36d73ed7f93dd79 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Mon, 13 Apr 2020 11:09:25 +0100 Subject: [PATCH 4/5] Use `CRATE_HIR_ID` instead of `DUMMY_HIR_ID` when appropriate. Those usage ends up forwarded to a `ObligationClause` which uses `CRATE_HIR_ID` for dummy value as seen in `ObligationClause::dummy`. --- src/librustc_traits/implied_outlives_bounds.rs | 2 +- src/librustc_ty/ty.rs | 2 +- src/librustc_typeck/check/method/probe.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc_traits/implied_outlives_bounds.rs b/src/librustc_traits/implied_outlives_bounds.rs index 33ecbe72a8c..6db2e557fea 100644 --- a/src/librustc_traits/implied_outlives_bounds.rs +++ b/src/librustc_traits/implied_outlives_bounds.rs @@ -62,7 +62,7 @@ fn compute_implied_outlives_bounds<'tcx>( // unresolved inference variables here anyway, but there might be // during typeck under some circumstances.) let obligations = - wf::obligations(infcx, param_env, hir::DUMMY_HIR_ID, ty, DUMMY_SP).unwrap_or(vec![]); + wf::obligations(infcx, param_env, hir::CRATE_HIR_ID, ty, DUMMY_SP).unwrap_or(vec![]); // N.B., all of these predicates *ought* to be easily proven // true. In fact, their correctness is (mostly) implied by diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index aefe61f60b8..4cb7b7d8fda 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -265,7 +265,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { let unnormalized_env = ty::ParamEnv::new(tcx.intern_predicates(&predicates), traits::Reveal::UserFacing, None); - let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::DUMMY_HIR_ID, |id| { + let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::CRATE_HIR_ID, |id| { tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id) }); let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id); diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 0b1aff7e806..a77d8981d02 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -452,7 +452,7 @@ fn method_autoderef_steps<'tcx>( tcx.infer_ctxt().enter_with_canonical(DUMMY_SP, &goal, |ref infcx, goal, inference_vars| { let ParamEnvAnd { param_env, value: self_ty } = goal; - let mut autoderef = Autoderef::new(infcx, param_env, hir::DUMMY_HIR_ID, DUMMY_SP, self_ty) + let mut autoderef = Autoderef::new(infcx, param_env, hir::CRATE_HIR_ID, DUMMY_SP, self_ty) .include_raw_pointers() .silence_errors(); let mut reached_raw_pointer = false; From d0409ead2f44306fd54b12ed0ac83a4f5ee44910 Mon Sep 17 00:00:00 2001 From: marmeladema Date: Mon, 13 Apr 2020 17:57:39 +0100 Subject: [PATCH 5/5] Remove usage of `DUMMY_HIR_ID` in `FnCtxt::get_conversion_methods` --- src/librustc_typeck/check/demand.rs | 10 +++------- src/librustc_typeck/check/mod.rs | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 369bb183bcd..781d25099fd 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -217,14 +217,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { span: Span, expected: Ty<'tcx>, checked_ty: Ty<'tcx>, + hir_id: hir::HirId, ) -> Vec { - let mut methods = self.probe_for_return_type( - span, - probe::Mode::MethodCall, - expected, - checked_ty, - hir::DUMMY_HIR_ID, - ); + let mut methods = + self.probe_for_return_type(span, probe::Mode::MethodCall, expected, checked_ty, hir_id); methods.retain(|m| { self.has_no_input_arg(m) && self diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 4b5953b5e95..5857ccf5897 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -5000,7 +5000,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else if !self.check_for_cast(err, expr, found, expected) { let is_struct_pat_shorthand_field = self.is_hir_id_from_struct_pattern_shorthand_field(expr.hir_id, expr.span); - let methods = self.get_conversion_methods(expr.span, expected, found); + let methods = self.get_conversion_methods(expr.span, expected, found, expr.hir_id); if let Ok(expr_text) = self.sess().source_map().span_to_snippet(expr.span) { let mut suggestions = iter::repeat(&expr_text) .zip(methods.iter())