diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
commit | 837b550238aa671a591ccf282dddeab29cadb206 (patch) | |
tree | 914b6b8862bace72bd3245ca184d374b08d8a672 /compiler/rustc_passes/src/liveness.rs | |
parent | Adding debian version 1.70.0+dfsg2-1. (diff) | |
download | rustc-837b550238aa671a591ccf282dddeab29cadb206.tar.xz rustc-837b550238aa671a591ccf282dddeab29cadb206.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_passes/src/liveness.rs')
-rw-r--r-- | compiler/rustc_passes/src/liveness.rs | 242 |
1 files changed, 101 insertions, 141 deletions
diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index a8471ce3b..63b1578d4 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -81,23 +81,24 @@ //! We generate various special nodes for various, well, special purposes. //! These are described in the `Liveness` struct. +use crate::errors; + use self::LiveNodeKind::*; use self::VarKind::*; use rustc_ast::InlineAsmOptions; use rustc_data_structures::fx::FxIndexMap; -use rustc_errors::Applicability; -use rustc_errors::Diagnostic; use rustc_hir as hir; use rustc_hir::def::*; -use rustc_hir::def_id::{DefId, LocalDefId}; +use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{Expr, HirId, HirIdMap, HirIdSet}; -use rustc_index::vec::IndexVec; -use rustc_middle::ty::query::Providers; +use rustc_index::IndexVec; +use rustc_middle::query::Providers; use rustc_middle::ty::{self, RootVariableMinCaptureList, Ty, TyCtxt}; use rustc_session::lint; use rustc_span::symbol::{kw, sym, Symbol}; +use rustc_span::DUMMY_SP; use rustc_span::{BytePos, Span}; use std::collections::VecDeque; @@ -137,14 +138,9 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String { } } -fn check_liveness(tcx: TyCtxt<'_>, def_id: DefId) { - let local_def_id = match def_id.as_local() { - None => return, - Some(def_id) => def_id, - }; - +fn check_liveness(tcx: TyCtxt<'_>, def_id: LocalDefId) { // Don't run unused pass for #[derive()] - let parent = tcx.local_parent(local_def_id); + let parent = tcx.local_parent(def_id); if let DefKind::Impl { .. } = tcx.def_kind(parent) && tcx.has_attr(parent, sym::automatically_derived) { @@ -152,12 +148,12 @@ fn check_liveness(tcx: TyCtxt<'_>, def_id: DefId) { } // Don't run unused pass for #[naked] - if tcx.has_attr(def_id, sym::naked) { + if tcx.has_attr(def_id.to_def_id(), sym::naked) { return; } let mut maps = IrMaps::new(tcx); - let body_id = tcx.hir().body_owned_by(local_def_id); + let body_id = tcx.hir().body_owned_by(def_id); let hir_id = tcx.hir().body_owner(body_id); let body = tcx.hir().body(body_id); @@ -173,7 +169,7 @@ fn check_liveness(tcx: TyCtxt<'_>, def_id: DefId) { maps.visit_body(body); // compute liveness - let mut lsets = Liveness::new(&mut maps, local_def_id); + let mut lsets = Liveness::new(&mut maps, def_id); let entry_ln = lsets.compute(&body, hir_id); lsets.log_liveness(entry_ln, body_id.hir_id); @@ -331,7 +327,7 @@ impl<'tcx> IrMaps<'tcx> { pats.extend(inner_pat.iter()); } Struct(_, fields, _) => { - let (short, not_short): (Vec<_>, _) = + let (short, not_short): (Vec<hir::PatField<'_>>, _) = fields.iter().partition(|f| f.is_shorthand); shorthand_field_ids.extend(short.iter().map(|f| f.pat.hir_id)); pats.extend(not_short.iter().map(|f| f.pat)); @@ -473,6 +469,7 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> { | hir::ExprKind::Struct(..) | hir::ExprKind::Repeat(..) | hir::ExprKind::InlineAsm(..) + | hir::ExprKind::OffsetOf(..) | hir::ExprKind::Type(..) | hir::ExprKind::Err(_) | hir::ExprKind::Path(hir::QPath::TypeRelative(..)) @@ -591,8 +588,13 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { } fn assigned_on_exit(&self, ln: LiveNode, var: Variable) -> bool { - let successor = self.successors[ln].unwrap(); - self.assigned_on_entry(successor, var) + match self.successors[ln] { + Some(successor) => self.assigned_on_entry(successor, var), + None => { + self.ir.tcx.sess.delay_span_bug(DUMMY_SP, "no successor"); + true + } + } } fn write_vars<F>(&self, wr: &mut dyn Write, mut test: F) -> io::Result<()> @@ -1129,7 +1131,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { | hir::ExprKind::ConstBlock(..) | hir::ExprKind::Err(_) | hir::ExprKind::Path(hir::QPath::TypeRelative(..)) - | hir::ExprKind::Path(hir::QPath::LangItem(..)) => succ, + | hir::ExprKind::Path(hir::QPath::LangItem(..)) + | hir::ExprKind::OffsetOf(..) => succ, // Note that labels have been resolved, so we don't need to look // at the label ident @@ -1294,13 +1297,13 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { self.exit_ln } - fn warn_about_unreachable( + fn warn_about_unreachable<'desc>( &mut self, orig_span: Span, orig_ty: Ty<'tcx>, expr_span: Span, expr_id: HirId, - descr: &str, + descr: &'desc str, ) { if !orig_ty.is_never() { // Unreachable code warnings are already emitted during type checking. @@ -1313,22 +1316,15 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { // that we do not emit the same warning twice if the uninhabited type // is indeed `!`. - let msg = format!("unreachable {}", descr); - self.ir.tcx.struct_span_lint_hir( + self.ir.tcx.emit_spanned_lint( lint::builtin::UNREACHABLE_CODE, expr_id, expr_span, - &msg, - |diag| { - diag.span_label(expr_span, &msg) - .span_label(orig_span, "any code following this expression is unreachable") - .span_note( - orig_span, - &format!( - "this expression has type `{}`, which is uninhabited", - orig_ty - ), - ) + errors::UnreachableDueToUninhabited { + expr: expr_span, + orig: orig_span, + descr, + ty: orig_ty, }, ); } @@ -1418,6 +1414,7 @@ fn check_expr<'tcx>(this: &mut Liveness<'_, 'tcx>, expr: &'tcx Expr<'tcx>) { | hir::ExprKind::ConstBlock(..) | hir::ExprKind::Block(..) | hir::ExprKind::AddrOf(..) + | hir::ExprKind::OffsetOf(..) | hir::ExprKind::Struct(..) | hir::ExprKind::Repeat(..) | hir::ExprKind::Closure { .. } @@ -1479,23 +1476,21 @@ impl<'tcx> Liveness<'_, 'tcx> { if self.used_on_entry(entry_ln, var) { if !self.live_on_entry(entry_ln, var) { if let Some(name) = self.should_warn(var) { - self.ir.tcx.struct_span_lint_hir( + self.ir.tcx.emit_spanned_lint( lint::builtin::UNUSED_ASSIGNMENTS, var_hir_id, vec![span], - format!("value captured by `{}` is never read", name), - |lint| lint.help("did you mean to capture by reference instead?"), + errors::UnusedCaptureMaybeCaptureRef { name }, ); } } } else { if let Some(name) = self.should_warn(var) { - self.ir.tcx.struct_span_lint_hir( + self.ir.tcx.emit_spanned_lint( lint::builtin::UNUSED_VARIABLES, var_hir_id, vec![span], - format!("unused variable: `{}`", name), - |lint| lint.help("did you mean to capture by reference instead?"), + errors::UnusedVarMaybeCaptureRef { name }, ); } } @@ -1510,11 +1505,14 @@ impl<'tcx> Liveness<'_, 'tcx> { Some(entry_ln), Some(body), |spans, hir_id, ln, var| { - if !self.live_on_entry(ln, var) { - self.report_unused_assign(hir_id, spans, var, |name| { - format!("value passed to `{}` is never read", name) - }); - } + if !self.live_on_entry(ln, var) + && let Some(name) = self.should_warn(var) { + self.ir.tcx.emit_spanned_lint( + lint::builtin::UNUSED_ASSIGNMENTS, + hir_id, + spans, + errors::UnusedAssignPassed { name }, + ); } }, ); } @@ -1583,39 +1581,35 @@ impl<'tcx> Liveness<'_, 'tcx> { if ln == self.exit_ln { false } else { self.assigned_on_exit(ln, var) }; if is_assigned { - self.ir.tcx.struct_span_lint_hir( + self.ir.tcx.emit_spanned_lint( lint::builtin::UNUSED_VARIABLES, first_hir_id, hir_ids_and_spans .into_iter() .map(|(_, _, ident_span)| ident_span) .collect::<Vec<_>>(), - format!("variable `{}` is assigned to, but never used", name), - |lint| lint.note(&format!("consider using `_{}` instead", name)), + errors::UnusedVarAssignedOnly { name }, ) } else if can_remove { - self.ir.tcx.struct_span_lint_hir( + let spans = hir_ids_and_spans + .iter() + .map(|(_, pat_span, _)| { + let span = self + .ir + .tcx + .sess + .source_map() + .span_extend_to_next_char(*pat_span, ',', true); + span.with_hi(BytePos(span.hi().0 + 1)) + }) + .collect(); + self.ir.tcx.emit_spanned_lint( lint::builtin::UNUSED_VARIABLES, first_hir_id, hir_ids_and_spans.iter().map(|(_, pat_span, _)| *pat_span).collect::<Vec<_>>(), - format!("unused variable: `{}`", name), - |lint| { - lint.multipart_suggestion( - "try removing the field", - hir_ids_and_spans - .iter() - .map(|(_, pat_span, _)| { - let span = self - .ir - .tcx - .sess - .source_map() - .span_extend_to_next_char(*pat_span, ',', true); - (span.with_hi(BytePos(span.hi().0 + 1)), String::new()) - }) - .collect(), - Applicability::MachineApplicable, - ) + errors::UnusedVarRemoveField { + name, + sugg: errors::UnusedVarRemoveFieldSugg { spans }, }, ); } else { @@ -1629,55 +1623,46 @@ impl<'tcx> Liveness<'_, 'tcx> { // the field" message, and suggest `_` for the non-shorthands. If we only // have non-shorthand, then prefix with an underscore instead. if !shorthands.is_empty() { - let shorthands = shorthands - .into_iter() - .map(|(_, pat_span, _)| (pat_span, format!("{}: _", name))) - .chain( - non_shorthands - .into_iter() - .map(|(_, pat_span, _)| (pat_span, "_".to_string())), - ) - .collect::<Vec<_>>(); + let shorthands = + shorthands.into_iter().map(|(_, pat_span, _)| pat_span).collect(); + let non_shorthands = + non_shorthands.into_iter().map(|(_, pat_span, _)| pat_span).collect(); - self.ir.tcx.struct_span_lint_hir( + self.ir.tcx.emit_spanned_lint( lint::builtin::UNUSED_VARIABLES, first_hir_id, hir_ids_and_spans .iter() .map(|(_, pat_span, _)| *pat_span) .collect::<Vec<_>>(), - format!("unused variable: `{}`", name), - |lint| { - lint.multipart_suggestion( - "try ignoring the field", + errors::UnusedVarTryIgnore { + sugg: errors::UnusedVarTryIgnoreSugg { shorthands, - Applicability::MachineApplicable, - ) + non_shorthands, + name, + }, }, ); } else { let non_shorthands = non_shorthands .into_iter() - .map(|(_, _, ident_span)| (ident_span, format!("_{}", name))) + .map(|(_, _, ident_span)| ident_span) .collect::<Vec<_>>(); - - self.ir.tcx.struct_span_lint_hir( + let suggestions = self.string_interp_suggestions(&name, opt_body); + self.ir.tcx.emit_spanned_lint( lint::builtin::UNUSED_VARIABLES, first_hir_id, hir_ids_and_spans .iter() .map(|(_, _, ident_span)| *ident_span) .collect::<Vec<_>>(), - format!("unused variable: `{}`", name), - |lint| { - if self.has_added_lit_match_name_span(&name, opt_body, lint) { - lint.span_label(pat.span, "unused variable"); - } - lint.multipart_suggestion( - "if this is intentional, prefix it with an underscore", - non_shorthands, - Applicability::MachineApplicable, - ) + errors::UnusedVariableTryPrefix { + label: if !suggestions.is_empty() { Some(pat.span) } else { None }, + sugg: errors::UnusedVariableTryPrefixSugg { + spans: non_shorthands, + name, + }, + string_interp: suggestions, }, ); } @@ -1685,65 +1670,40 @@ impl<'tcx> Liveness<'_, 'tcx> { } } - fn has_added_lit_match_name_span( + fn string_interp_suggestions( &self, name: &str, opt_body: Option<&hir::Body<'_>>, - err: &mut Diagnostic, - ) -> bool { - let mut has_litstring = false; - let Some(opt_body) = opt_body else {return false;}; + ) -> Vec<errors::UnusedVariableStringInterp> { + let mut suggs = Vec::new(); + let Some(opt_body) = opt_body else { return suggs; }; let mut visitor = CollectLitsVisitor { lit_exprs: vec![] }; intravisit::walk_body(&mut visitor, opt_body); for lit_expr in visitor.lit_exprs { let hir::ExprKind::Lit(litx) = &lit_expr.kind else { continue }; let rustc_ast::LitKind::Str(syb, _) = litx.node else{ continue; }; let name_str: &str = syb.as_str(); - let mut name_pa = String::from("{"); - name_pa.push_str(&name); - name_pa.push('}'); + let name_pa = format!("{{{name}}}"); if name_str.contains(&name_pa) { - err.span_label( - lit_expr.span, - "you might have meant to use string interpolation in this string literal", - ); - err.multipart_suggestion( - "string interpolation only works in `format!` invocations", - vec![ - (lit_expr.span.shrink_to_lo(), "format!(".to_string()), - (lit_expr.span.shrink_to_hi(), ")".to_string()), - ], - Applicability::MachineApplicable, - ); - has_litstring = true; + suggs.push(errors::UnusedVariableStringInterp { + lit: lit_expr.span, + lo: lit_expr.span.shrink_to_lo(), + hi: lit_expr.span.shrink_to_hi(), + }); } } - has_litstring + suggs } fn warn_about_dead_assign(&self, spans: Vec<Span>, hir_id: HirId, ln: LiveNode, var: Variable) { - if !self.live_on_exit(ln, var) { - self.report_unused_assign(hir_id, spans, var, |name| { - format!("value assigned to `{}` is never read", name) - }); - } - } - - fn report_unused_assign( - &self, - hir_id: HirId, - spans: Vec<Span>, - var: Variable, - message: impl Fn(&str) -> String, - ) { - if let Some(name) = self.should_warn(var) { - self.ir.tcx.struct_span_lint_hir( - lint::builtin::UNUSED_ASSIGNMENTS, - hir_id, - spans, - message(&name), - |lint| lint.help("maybe it is overwritten before being read?"), - ) - } + if !self.live_on_exit(ln, var) + && let Some(name) = self.should_warn(var) { + self.ir.tcx.emit_spanned_lint( + lint::builtin::UNUSED_ASSIGNMENTS, + hir_id, + spans, + errors::UnusedAssign { name }, + ); + } } } |