Skip to content

Commit 69862b7

Browse files
authored
Rollup merge of #71069 - marmeladema:dummy-hir-id-obligation-clause, r=eddyb
Remove some usage of `DUMMY_HIR_ID` Use `ObligationClause::dummy()` when appropriate or replace `hir::DUMMY_HIR_ID`by `hir::CRATE_HIR_ID`, as used in `ObligationClause::dummy()`.
2 parents ebb1a8b + 0634789 commit 69862b7

File tree

7 files changed

+27
-36
lines changed

7 files changed

+27
-36
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ pub(super) fn note_and_explain_region(
9393
let unknown_scope =
9494
|| format!("{}unknown scope: {:?}{}. Please report a bug.", prefix, scope, suffix);
9595
let span = scope.span(tcx, region_scope_tree);
96-
let tag = match tcx.hir().find(scope.hir_id(region_scope_tree)) {
96+
let hir_id = scope.hir_id(region_scope_tree);
97+
let tag = match hir_id.and_then(|hir_id| tcx.hir().find(hir_id)) {
9798
Some(Node::Block(_)) => "block",
9899
Some(Node::Expr(expr)) => match expr.kind {
99100
hir::ExprKind::Call(..) => "call",

src/librustc_middle/middle/region.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,20 @@ impl Scope {
159159
self.id
160160
}
161161

162-
pub fn hir_id(&self, scope_tree: &ScopeTree) -> hir::HirId {
163-
match scope_tree.root_body {
164-
Some(hir_id) => hir::HirId { owner: hir_id.owner, local_id: self.item_local_id() },
165-
None => hir::DUMMY_HIR_ID,
166-
}
162+
pub fn hir_id(&self, scope_tree: &ScopeTree) -> Option<hir::HirId> {
163+
scope_tree
164+
.root_body
165+
.map(|hir_id| hir::HirId { owner: hir_id.owner, local_id: self.item_local_id() })
167166
}
168167

169168
/// Returns the span of this `Scope`. Note that in general the
170169
/// returned span may not correspond to the span of any `NodeId` in
171170
/// the AST.
172171
pub fn span(&self, tcx: TyCtxt<'_>, scope_tree: &ScopeTree) -> Span {
173-
let hir_id = self.hir_id(scope_tree);
174-
if hir_id == hir::DUMMY_HIR_ID {
175-
return DUMMY_SP;
176-
}
172+
let hir_id = match self.hir_id(scope_tree) {
173+
Some(hir_id) => hir_id,
174+
None => return DUMMY_SP,
175+
};
177176
let span = tcx.hir().span(hir_id);
178177
if let ScopeData::Remainder(first_statement_index) = self.data {
179178
if let Node::Block(ref blk) = tcx.hir().get(hir_id) {

src/librustc_passes/check_attr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_errors::struct_span_err;
1414
use rustc_hir as hir;
1515
use rustc_hir::def_id::DefId;
1616
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
17-
use rustc_hir::DUMMY_HIR_ID;
1817
use rustc_hir::{self, HirId, Item, ItemKind, TraitItem};
1918
use rustc_hir::{MethodKind, Target};
2019
use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
@@ -360,7 +359,7 @@ impl CheckAttrVisitor<'tcx> {
360359
if let hir::StmtKind::Local(ref l) = stmt.kind {
361360
for attr in l.attrs.iter() {
362361
if attr.check_name(sym::inline) {
363-
self.check_inline(DUMMY_HIR_ID, attr, &stmt.span, Target::Statement);
362+
self.check_inline(l.hir_id, attr, &stmt.span, Target::Statement);
364363
}
365364
if attr.check_name(sym::repr) {
366365
self.emit_repr_error(
@@ -381,7 +380,7 @@ impl CheckAttrVisitor<'tcx> {
381380
};
382381
for attr in expr.attrs.iter() {
383382
if attr.check_name(sym::inline) {
384-
self.check_inline(DUMMY_HIR_ID, attr, &expr.span, target);
383+
self.check_inline(expr.hir_id, attr, &expr.span, target);
385384
}
386385
if attr.check_name(sym::repr) {
387386
self.emit_repr_error(

src/librustc_passes/loops.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,31 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
7777
}
7878

7979
let loop_id = match label.target_id {
80-
Ok(loop_id) => loop_id,
81-
Err(hir::LoopIdError::OutsideLoopScope) => hir::DUMMY_HIR_ID,
80+
Ok(loop_id) => Some(loop_id),
81+
Err(hir::LoopIdError::OutsideLoopScope) => None,
8282
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
8383
self.emit_unlabled_cf_in_while_condition(e.span, "break");
84-
hir::DUMMY_HIR_ID
84+
None
8585
}
86-
Err(hir::LoopIdError::UnresolvedLabel) => hir::DUMMY_HIR_ID,
86+
Err(hir::LoopIdError::UnresolvedLabel) => None,
8787
};
8888

89-
if loop_id != hir::DUMMY_HIR_ID {
89+
if let Some(loop_id) = loop_id {
9090
if let Node::Block(_) = self.hir_map.find(loop_id).unwrap() {
9191
return;
9292
}
9393
}
9494

9595
if opt_expr.is_some() {
96-
let loop_kind = if loop_id == hir::DUMMY_HIR_ID {
97-
None
98-
} else {
96+
let loop_kind = if let Some(loop_id) = loop_id {
9997
Some(match self.hir_map.expect_expr(loop_id).kind {
10098
hir::ExprKind::Loop(_, _, source) => source,
10199
ref r => {
102100
span_bug!(e.span, "break label resolved to a non-loop: {:?}", r)
103101
}
104102
})
103+
} else {
104+
None
105105
};
106106
match loop_kind {
107107
None | Some(hir::LoopSource::Loop) => (),

src/librustc_trait_selection/traits/auto_trait.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
187187
// to store all of the necessary region/lifetime bounds in the InferContext, as well as
188188
// an additional sanity check.
189189
let mut fulfill = FulfillmentContext::new();
190-
fulfill.register_bound(
191-
&infcx,
192-
full_env,
193-
ty,
194-
trait_did,
195-
ObligationCause::misc(DUMMY_SP, hir::DUMMY_HIR_ID),
196-
);
190+
fulfill.register_bound(&infcx, full_env, ty, trait_did, ObligationCause::dummy());
197191
fulfill.select_all_or_error(&infcx).unwrap_or_else(|e| {
198192
panic!("Unable to fulfill trait {:?} for '{:?}': {:?}", trait_did, ty, e)
199193
});
@@ -292,7 +286,7 @@ impl AutoTraitFinder<'tcx> {
292286
user_env.caller_bounds.iter().cloned().collect();
293287

294288
let mut new_env = param_env;
295-
let dummy_cause = ObligationCause::misc(DUMMY_SP, hir::DUMMY_HIR_ID);
289+
let dummy_cause = ObligationCause::dummy();
296290

297291
while let Some(pred) = predicates.pop_front() {
298292
infcx.clear_caches();
@@ -615,7 +609,7 @@ impl AutoTraitFinder<'tcx> {
615609
select: &mut SelectionContext<'_, 'tcx>,
616610
only_projections: bool,
617611
) -> bool {
618-
let dummy_cause = ObligationCause::misc(DUMMY_SP, hir::DUMMY_HIR_ID);
612+
let dummy_cause = ObligationCause::dummy();
619613

620614
for (obligation, mut predicate) in nested.map(|o| (o.clone(), o.predicate)) {
621615
let is_new_pred = fresh_preds.insert(self.clean_pred(select.infcx(), predicate));

src/librustc_trait_selection/traits/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_middle::middle::region;
3131
use rustc_middle::ty::fold::TypeFoldable;
3232
use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
3333
use rustc_middle::ty::{self, GenericParamDefKind, ToPredicate, Ty, TyCtxt, WithConstness};
34-
use rustc_span::{Span, DUMMY_SP};
34+
use rustc_span::Span;
3535

3636
use std::fmt::Debug;
3737

@@ -136,7 +136,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
136136
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
137137
let obligation = Obligation {
138138
param_env,
139-
cause: ObligationCause::misc(span, hir::DUMMY_HIR_ID),
139+
cause: ObligationCause::misc(span, hir::CRATE_HIR_ID),
140140
recursion_depth: 0,
141141
predicate: trait_ref.without_const().to_predicate(),
142142
};
@@ -163,7 +163,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
163163
// We can use a dummy node-id here because we won't pay any mind
164164
// to region obligations that arise (there shouldn't really be any
165165
// anyhow).
166-
let cause = ObligationCause::misc(span, hir::DUMMY_HIR_ID);
166+
let cause = ObligationCause::misc(span, hir::CRATE_HIR_ID);
167167

168168
fulfill_cx.register_bound(infcx, param_env, ty, def_id, cause);
169169

src/librustc_traits/normalize_projection_ty.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use rustc_hir as hir;
21
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
32
use rustc_infer::infer::TyCtxtInferExt;
43
use rustc_infer::traits::TraitEngineExt as _;
54
use rustc_middle::ty::query::Providers;
65
use rustc_middle::ty::{ParamEnvAnd, TyCtxt};
7-
use rustc_span::DUMMY_SP;
86
use rustc_trait_selection::infer::InferCtxtBuilderExt;
97
use rustc_trait_selection::traits::query::{
108
normalize::NormalizationResult, CanonicalProjectionGoal, NoSolution,
@@ -27,7 +25,7 @@ fn normalize_projection_ty<'tcx>(
2725
&goal,
2826
|infcx, fulfill_cx, ParamEnvAnd { param_env, value: goal }| {
2927
let selcx = &mut SelectionContext::new(infcx);
30-
let cause = ObligationCause::misc(DUMMY_SP, hir::DUMMY_HIR_ID);
28+
let cause = ObligationCause::dummy();
3129
let mut obligations = vec![];
3230
let answer = traits::normalize_projection_type(
3331
selcx,

0 commit comments

Comments
 (0)