Skip to content

Commit

Permalink
Remove sess from CheckLoopVisitor.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Sep 13, 2024
1 parent 8d32578 commit 359b658
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions compiler/rustc_passes/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::query::Providers;
use rustc_middle::span_bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::hygiene::DesugaringKind;
use rustc_span::{BytePos, Span};
use Context::*;
Expand Down Expand Up @@ -65,7 +64,6 @@ impl fmt::Display for BreakContextKind {

#[derive(Clone)]
struct CheckLoopVisitor<'tcx> {
sess: &'tcx Session,
tcx: TyCtxt<'tcx>,
// Keep track of a stack of contexts, so that suggestions
// are not made for contexts where it would be incorrect,
Expand All @@ -76,12 +74,8 @@ struct CheckLoopVisitor<'tcx> {
}

fn check_mod_loops(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
let mut check = CheckLoopVisitor {
sess: tcx.sess,
tcx,
cx_stack: vec![Normal],
block_breaks: Default::default(),
};
let mut check =
CheckLoopVisitor { tcx, cx_stack: vec![Normal], block_breaks: Default::default() };
tcx.hir().visit_item_likes_in_module(module_def_id, &mut check);
check.report_outside_loop_error();
}
Expand Down Expand Up @@ -213,7 +207,7 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
Ok(loop_id) => Some(loop_id),
Err(hir::LoopIdError::OutsideLoopScope) => None,
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
self.sess.dcx().emit_err(UnlabeledCfInWhileCondition {
self.tcx.dcx().emit_err(UnlabeledCfInWhileCondition {
span: e.span,
cf_type: "break",
});
Expand Down Expand Up @@ -248,7 +242,7 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
.label
.map_or_else(String::new, |l| format!(" {}", l.ident))
);
self.sess.dcx().emit_err(BreakNonLoop {
self.tcx.dcx().emit_err(BreakNonLoop {
span: e.span,
head,
kind: kind.name(),
Expand Down Expand Up @@ -280,14 +274,14 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
match destination.target_id {
Ok(loop_id) => {
if let Node::Block(block) = self.tcx.hir_node(loop_id) {
self.sess.dcx().emit_err(ContinueLabeledBlock {
self.tcx.dcx().emit_err(ContinueLabeledBlock {
span: e.span,
block_span: block.span,
});
}
}
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
self.sess.dcx().emit_err(UnlabeledCfInWhileCondition {
self.tcx.dcx().emit_err(UnlabeledCfInWhileCondition {
span: e.span,
cf_type: "continue",
});
Expand Down Expand Up @@ -326,7 +320,7 @@ impl<'hir> CheckLoopVisitor<'hir> {
match self.cx_stack[cx_pos] {
LabeledBlock | Loop(_) => {}
Closure(closure_span) => {
self.sess.dcx().emit_err(BreakInsideClosure {
self.tcx.dcx().emit_err(BreakInsideClosure {
span,
closure_span,
name: &br_cx_kind.to_string(),
Expand All @@ -343,7 +337,7 @@ impl<'hir> CheckLoopVisitor<'hir> {
hir::CoroutineSource::Closure => "closure",
hir::CoroutineSource::Fn => "function",
};
self.sess.dcx().emit_err(BreakInsideCoroutine {
self.tcx.dcx().emit_err(BreakInsideCoroutine {
span,
coroutine_span,
name: &br_cx_kind.to_string(),
Expand All @@ -366,7 +360,7 @@ impl<'hir> CheckLoopVisitor<'hir> {
self.require_break_cx(br_cx_kind, span, break_span, cx_pos - 1);
}
Normal | AnonConst | Fn | UnlabeledBlock(_) | UnlabeledIfBlock(_) | ConstBlock => {
self.sess.dcx().emit_err(OutsideLoop {
self.tcx.dcx().emit_err(OutsideLoop {
spans: vec![span],
name: &br_cx_kind.to_string(),
is_break: br_cx_kind == BreakContextKind::Break,
Expand All @@ -386,15 +380,15 @@ impl<'hir> CheckLoopVisitor<'hir> {
&& self.cx_stack.last() == Some(&LabeledBlock)
&& label.label.is_none()
{
self.sess.dcx().emit_err(UnlabeledInLabeledBlock { span, cf_type });
self.tcx.dcx().emit_err(UnlabeledInLabeledBlock { span, cf_type });
return true;
}
false
}

fn report_outside_loop_error(&self) {
for (s, block) in &self.block_breaks {
self.sess.dcx().emit_err(OutsideLoop {
self.tcx.dcx().emit_err(OutsideLoop {
spans: block.spans.clone(),
name: &block.name,
is_break: true,
Expand Down

0 comments on commit 359b658

Please sign in to comment.