Add warn(unreachable_pub) to rustc_borrowck.

This commit is contained in:
Nicholas Nethercote 2024-07-06 21:53:49 +10:00
parent cb3f435699
commit 0685c97843
18 changed files with 74 additions and 68 deletions

View File

@ -9,7 +9,7 @@
use rustc_span::Span;
impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
pub fn dcx(&self) -> DiagCtxtHandle<'infcx> {
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'infcx> {
self.infcx.dcx()
}

View File

@ -14,7 +14,7 @@
use crate::{places_conflict, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext};
/// The results of the dataflow analyses used by the borrow checker.
pub struct BorrowckResults<'a, 'mir, 'tcx> {
pub(crate) struct BorrowckResults<'a, 'mir, 'tcx> {
pub(crate) borrows: Results<'tcx, Borrows<'a, 'mir, 'tcx>>,
pub(crate) uninits: Results<'tcx, MaybeUninitializedPlaces<'a, 'mir, 'tcx>>,
pub(crate) ever_inits: Results<'tcx, EverInitializedPlaces<'a, 'mir, 'tcx>>,
@ -22,7 +22,7 @@ pub struct BorrowckResults<'a, 'mir, 'tcx> {
/// The transient state of the dataflow analyses used by the borrow checker.
#[derive(Debug)]
pub struct BorrowckFlowState<'a, 'mir, 'tcx> {
pub(crate) struct BorrowckFlowState<'a, 'mir, 'tcx> {
pub(crate) borrows: <Borrows<'a, 'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
pub(crate) uninits: <MaybeUninitializedPlaces<'a, 'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
pub(crate) ever_inits: <EverInitializedPlaces<'a, 'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,

View File

@ -4,13 +4,13 @@
};
#[derive(Eq, PartialEq, Clone)]
pub enum DefUse {
pub(crate) enum DefUse {
Def,
Use,
Drop,
}
pub fn categorize(context: PlaceContext) -> Option<DefUse> {
pub(crate) fn categorize(context: PlaceContext) -> Option<DefUse> {
match context {
///////////////////////////////////////////////////////////////////////////
// DEFS

View File

@ -645,7 +645,7 @@ fn visit_expr(&mut self, e: &hir::Expr<'_>) {
}
}
pub fn suggest_reborrow(
pub(crate) fn suggest_reborrow(
&self,
err: &mut Diag<'infcx>,
span: Span,
@ -1891,10 +1891,10 @@ fn suggest_copy_for_type_in_cloned_ref(&self, err: &mut Diag<'infcx>, place: Pla
struct FindUselessClone<'tcx> {
tcx: TyCtxt<'tcx>,
typeck_results: &'tcx ty::TypeckResults<'tcx>,
pub clones: Vec<&'tcx hir::Expr<'tcx>>,
clones: Vec<&'tcx hir::Expr<'tcx>>,
}
impl<'tcx> FindUselessClone<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
Self { tcx, typeck_results: tcx.typeck(def_id), clones: vec![] }
}
}
@ -1916,7 +1916,7 @@ fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
let body = hir.body(body_id).value;
expr_finder.visit_expr(body);
pub struct Holds<'tcx> {
struct Holds<'tcx> {
ty: Ty<'tcx>,
}

View File

@ -58,11 +58,11 @@
pub(crate) use rustc_middle::util::CallKind;
pub(super) struct DescribePlaceOpt {
pub including_downcast: bool,
including_downcast: bool,
/// Enable/Disable tuple fields.
/// For example `x` tuple. if it's `true` `x.0`. Otherwise `x`
pub including_tuple_field: bool,
including_tuple_field: bool,
}
pub(super) struct IncludingTupleField(pub(super) bool);

View File

@ -16,7 +16,7 @@
use crate::MirBorrowckCtxt;
#[derive(Debug)]
pub enum IllegalMoveOriginKind<'tcx> {
pub(crate) enum IllegalMoveOriginKind<'tcx> {
/// Illegal move due to attempt to move from behind a reference.
BorrowedContent {
/// The place the reference refers to: if erroneous code was trying to

View File

@ -1374,7 +1374,7 @@ fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) -> Self::Result {
}
}
pub fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool {
fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool {
debug!("local_info: {:?}, ty.kind(): {:?}", local_decl.local_info, local_decl.ty.kind());
match *local_decl.local_info() {

View File

@ -31,7 +31,7 @@ enum SuggestedConstraint {
///
/// Adds a help note suggesting adding a where clause with the needed constraints.
#[derive(Default)]
pub struct OutlivesSuggestionBuilder {
pub(crate) struct OutlivesSuggestionBuilder {
/// The list of outlives constraints that need to be added. Specifically, we map each free
/// region to all other regions that it must outlive. I will use the shorthand `fr:
/// outlived_frs`. Not all of these regions will already have names necessarily. Some could be

View File

@ -72,22 +72,24 @@ fn description(&self) -> &'static str {
pub(crate) struct RegionErrors<'tcx>(Vec<(RegionErrorKind<'tcx>, ErrorGuaranteed)>, TyCtxt<'tcx>);
impl<'tcx> RegionErrors<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
pub(crate) fn new(tcx: TyCtxt<'tcx>) -> Self {
Self(vec![], tcx)
}
#[track_caller]
pub fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
pub(crate) fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
let val = val.into();
let guar = self.1.sess.dcx().delayed_bug(format!("{val:?}"));
self.0.push((val, guar));
}
pub fn is_empty(&self) -> bool {
pub(crate) fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn into_iter(self) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
pub(crate) fn into_iter(
self,
) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
self.0.into_iter()
}
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
pub(crate) fn has_errors(&self) -> Option<ErrorGuaranteed> {
self.0.get(0).map(|x| x.1)
}
}
@ -141,7 +143,7 @@ pub(crate) enum RegionErrorKind<'tcx> {
/// Information about the various region constraints involved in a borrow checker error.
#[derive(Clone, Debug)]
pub struct ErrorConstraintInfo<'tcx> {
pub(crate) struct ErrorConstraintInfo<'tcx> {
// fr: outlived_fr
pub(super) fr: RegionVid,
pub(super) fr_is_local: bool,

View File

@ -12,6 +12,7 @@
#![feature(rustdoc_internals)]
#![feature(stmt_expr_attributes)]
#![feature(try_blocks)]
#![warn(unreachable_pub)]
// tidy-alphabetical-end
#[macro_use]
@ -2444,7 +2445,7 @@ fn sort_span(&self) -> Span {
}
}
pub struct BorrowckDiags<'infcx, 'tcx> {
pub(crate) struct BorrowckDiags<'infcx, 'tcx> {
/// This field keeps track of move errors that are to be reported for given move indices.
///
/// There are situations where many errors can be reported for a single move out (see
@ -2468,7 +2469,7 @@ pub struct BorrowckDiags<'infcx, 'tcx> {
}
impl<'infcx, 'tcx> BorrowckDiags<'infcx, 'tcx> {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
BorrowckDiags {
buffered_move_errors: BTreeMap::new(),
buffered_mut_errors: Default::default(),
@ -2476,25 +2477,25 @@ pub fn new() -> Self {
}
}
pub fn buffer_error(&mut self, diag: Diag<'infcx>) {
pub(crate) fn buffer_error(&mut self, diag: Diag<'infcx>) {
self.buffered_diags.push(BufferedDiag::Error(diag));
}
pub fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
pub(crate) fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
self.buffered_diags.push(BufferedDiag::NonError(diag));
}
}
impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
pub fn buffer_error(&mut self, diag: Diag<'infcx>) {
pub(crate) fn buffer_error(&mut self, diag: Diag<'infcx>) {
self.diags.buffer_error(diag);
}
pub fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
pub(crate) fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
self.diags.buffer_non_error(diag);
}
pub fn buffer_move_error(
pub(crate) fn buffer_move_error(
&mut self,
move_out_indices: Vec<MoveOutIndex>,
place_and_err: (PlaceRef<'tcx>, Diag<'infcx>),
@ -2510,16 +2511,19 @@ pub fn buffer_move_error(
}
}
pub fn get_buffered_mut_error(&mut self, span: Span) -> Option<(Diag<'infcx>, usize)> {
pub(crate) fn get_buffered_mut_error(
&mut self,
span: Span,
) -> Option<(Diag<'infcx>, usize)> {
// FIXME(#120456) - is `swap_remove` correct?
self.diags.buffered_mut_errors.swap_remove(&span)
}
pub fn buffer_mut_error(&mut self, span: Span, diag: Diag<'infcx>, count: usize) {
pub(crate) fn buffer_mut_error(&mut self, span: Span, diag: Diag<'infcx>, count: usize) {
self.diags.buffered_mut_errors.insert(span, (diag, count));
}
pub fn emit_errors(&mut self) -> Option<ErrorGuaranteed> {
pub(crate) fn emit_errors(&mut self) -> Option<ErrorGuaranteed> {
let mut res = None;
// Buffer any move errors that we collected and de-duplicated.
@ -2553,7 +2557,7 @@ pub(crate) fn has_buffered_diags(&self) -> bool {
self.diags.buffered_diags.is_empty()
}
pub fn has_move_error(
pub(crate) fn has_move_error(
&self,
move_out_indices: &[MoveOutIndex],
) -> Option<&(PlaceRef<'tcx>, Diag<'infcx>)> {

View File

@ -8,7 +8,7 @@
use super::MirBorrowckCtxt;
pub trait IsPrefixOf<'tcx> {
pub(crate) trait IsPrefixOf<'tcx> {
fn is_prefix_of(&self, other: PlaceRef<'tcx>) -> bool;
}

View File

@ -42,9 +42,9 @@
mod opaque_types;
mod reverse_sccs;
pub mod values;
pub(crate) mod values;
pub type ConstraintSccs = Sccs<RegionVid, ConstraintSccIndex, RegionTracker>;
pub(crate) type ConstraintSccs = Sccs<RegionVid, ConstraintSccIndex, RegionTracker>;
/// An annotation for region graph SCCs that tracks
/// the values of its elements.
@ -226,7 +226,7 @@ pub(crate) struct AppliedMemberConstraint {
}
#[derive(Debug)]
pub struct RegionDefinition<'tcx> {
pub(crate) struct RegionDefinition<'tcx> {
/// What kind of variable is this -- a free region? existential
/// variable? etc. (See the `NllRegionVariableOrigin` for more
/// info.)
@ -288,7 +288,7 @@ pub(crate) enum Cause {
/// `InferCtxt::process_registered_region_obligations` and
/// `InferCtxt::type_must_outlive` in `rustc_infer::infer::InferCtxt`.
#[derive(Clone, Debug)]
pub struct TypeTest<'tcx> {
pub(crate) struct TypeTest<'tcx> {
/// The type `T` that must outlive the region.
pub generic_kind: GenericKind<'tcx>,
@ -320,7 +320,7 @@ enum Trace<'tcx> {
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ExtraConstraintInfo {
pub(crate) enum ExtraConstraintInfo {
PlaceholderFromPredicate(Span),
}
@ -2259,7 +2259,7 @@ fn new(universe: ty::UniverseIndex, rv_origin: RegionVariableOrigin) -> Self {
}
#[derive(Clone, Debug)]
pub struct BlameConstraint<'tcx> {
pub(crate) struct BlameConstraint<'tcx> {
pub category: ConstraintCategory<'tcx>,
pub from_closure: bool,
pub cause: ObligationCause<'tcx>,

View File

@ -473,20 +473,20 @@ struct LazyOpaqueTyEnv<'tcx> {
}
impl<'tcx> LazyOpaqueTyEnv<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
Self { tcx, def_id, canonical_args: std::cell::OnceCell::new() }
}
pub fn param_equal_static(&self, param_index: usize) -> bool {
fn param_equal_static(&self, param_index: usize) -> bool {
self.get_canonical_args()[param_index].expect_region().is_static()
}
pub fn params_equal(&self, param1: usize, param2: usize) -> bool {
fn params_equal(&self, param1: usize, param2: usize) -> bool {
let canonical_args = self.get_canonical_args();
canonical_args[param1] == canonical_args[param2]
}
pub fn param_is_error(&self, param_index: usize) -> Result<(), ErrorGuaranteed> {
fn param_is_error(&self, param_index: usize) -> Result<(), ErrorGuaranteed> {
self.get_canonical_args()[param_index].error_reported()
}

View File

@ -86,7 +86,7 @@ macro_rules! span_mirbug_and_err {
mod canonical;
mod constraint_conversion;
pub mod free_region_relations;
pub(crate) mod free_region_relations;
mod input_output;
pub(crate) mod liveness;
mod relate_tys;

View File

@ -57,7 +57,7 @@ pub(super) fn eq_args(
}
}
pub struct NllTypeRelating<'me, 'bccx, 'tcx> {
struct NllTypeRelating<'me, 'bccx, 'tcx> {
type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
/// Where (and why) is this relation taking place?
@ -82,7 +82,7 @@ pub struct NllTypeRelating<'me, 'bccx, 'tcx> {
}
impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
pub fn new(
fn new(
type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
locations: Locations,
category: ConstraintCategory<'tcx>,

View File

@ -39,7 +39,7 @@
use crate::BorrowckInferCtxt;
#[derive(Debug)]
pub struct UniversalRegions<'tcx> {
pub(crate) struct UniversalRegions<'tcx> {
indices: UniversalRegionIndices<'tcx>,
/// The vid assigned to `'static`
@ -95,7 +95,7 @@ pub struct UniversalRegions<'tcx> {
/// regions appear free in the defining type and late-bound regions
/// appear bound in the signature.
#[derive(Copy, Clone, Debug)]
pub enum DefiningTy<'tcx> {
pub(crate) enum DefiningTy<'tcx> {
/// The MIR is a closure. The signature is found via
/// `ClosureArgs::closure_sig_ty`.
Closure(DefId, GenericArgsRef<'tcx>),
@ -131,7 +131,7 @@ impl<'tcx> DefiningTy<'tcx> {
/// not a closure or coroutine, there are no upvars, and hence it
/// will be an empty list. The order of types in this list will
/// match up with the upvar order in the HIR, typesystem, and MIR.
pub fn upvar_tys(self) -> &'tcx ty::List<Ty<'tcx>> {
pub(crate) fn upvar_tys(self) -> &'tcx ty::List<Ty<'tcx>> {
match self {
DefiningTy::Closure(_, args) => args.as_closure().upvar_tys(),
DefiningTy::CoroutineClosure(_, args) => args.as_coroutine_closure().upvar_tys(),
@ -145,7 +145,7 @@ pub fn upvar_tys(self) -> &'tcx ty::List<Ty<'tcx>> {
/// Number of implicit inputs -- notably the "environment"
/// parameter for closures -- that appear in MIR but not in the
/// user's code.
pub fn implicit_inputs(self) -> usize {
pub(crate) fn implicit_inputs(self) -> usize {
match self {
DefiningTy::Closure(..)
| DefiningTy::CoroutineClosure(..)
@ -154,15 +154,15 @@ pub fn implicit_inputs(self) -> usize {
}
}
pub fn is_fn_def(&self) -> bool {
pub(crate) fn is_fn_def(&self) -> bool {
matches!(*self, DefiningTy::FnDef(..))
}
pub fn is_const(&self) -> bool {
pub(crate) fn is_const(&self) -> bool {
matches!(*self, DefiningTy::Const(..) | DefiningTy::InlineConst(..))
}
pub fn def_id(&self) -> DefId {
pub(crate) fn def_id(&self) -> DefId {
match *self {
DefiningTy::Closure(def_id, ..)
| DefiningTy::CoroutineClosure(def_id, ..)
@ -196,7 +196,7 @@ struct UniversalRegionIndices<'tcx> {
}
#[derive(Debug, PartialEq)]
pub enum RegionClassification {
pub(crate) enum RegionClassification {
/// A **global** region is one that can be named from
/// anywhere. There is only one, `'static`.
Global,
@ -246,7 +246,7 @@ impl<'tcx> UniversalRegions<'tcx> {
/// MIR -- that is, all the regions that appear in the function's
/// signature. This will also compute the relationships that are
/// known between those regions.
pub fn new(
pub(crate) fn new(
infcx: &BorrowckInferCtxt<'tcx>,
mir_def: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
@ -263,7 +263,7 @@ pub fn new(
/// if the `ClosureRegionRequirements` contains something like
/// `'1: '2`, then the caller would impose the constraint that
/// `V[1]: V[2]`.
pub fn closure_mapping(
pub(crate) fn closure_mapping(
tcx: TyCtxt<'tcx>,
closure_args: GenericArgsRef<'tcx>,
expected_num_vars: usize,
@ -289,13 +289,13 @@ pub fn closure_mapping(
}
/// Returns `true` if `r` is a member of this set of universal regions.
pub fn is_universal_region(&self, r: RegionVid) -> bool {
pub(crate) fn is_universal_region(&self, r: RegionVid) -> bool {
(FIRST_GLOBAL_INDEX..self.num_universals).contains(&r.index())
}
/// Classifies `r` as a universal region, returning `None` if this
/// is not a member of this set of universal regions.
pub fn region_classification(&self, r: RegionVid) -> Option<RegionClassification> {
pub(crate) fn region_classification(&self, r: RegionVid) -> Option<RegionClassification> {
let index = r.index();
if (FIRST_GLOBAL_INDEX..self.first_extern_index).contains(&index) {
Some(RegionClassification::Global)
@ -310,17 +310,17 @@ pub fn region_classification(&self, r: RegionVid) -> Option<RegionClassification
/// Returns an iterator over all the RegionVids corresponding to
/// universally quantified free regions.
pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
pub(crate) fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
(FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::from_usize)
}
/// Returns `true` if `r` is classified as a local region.
pub fn is_local_free_region(&self, r: RegionVid) -> bool {
pub(crate) fn is_local_free_region(&self, r: RegionVid) -> bool {
self.region_classification(r) == Some(RegionClassification::Local)
}
/// Returns the number of universal regions created in any category.
pub fn len(&self) -> usize {
pub(crate) fn len(&self) -> usize {
self.num_universals
}
@ -329,19 +329,19 @@ pub fn len(&self) -> usize {
/// closure type (versus those bound in the closure
/// signature). They are therefore the regions between which the
/// closure may impose constraints that its creator must verify.
pub fn num_global_and_external_regions(&self) -> usize {
pub(crate) fn num_global_and_external_regions(&self) -> usize {
self.first_local_index
}
/// Gets an iterator over all the early-bound regions that have names.
pub fn named_universal_regions<'s>(
pub(crate) fn named_universal_regions<'s>(
&'s self,
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {
self.indices.indices.iter().map(|(&r, &v)| (r, v))
}
/// See `UniversalRegionIndices::to_region_vid`.
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
pub(crate) fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
self.indices.to_region_vid(r)
}
@ -416,7 +416,7 @@ pub(crate) fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diag<'_, ()>) {
}
}
pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
pub(crate) fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
self.indices.tainted_by_errors.get()
}
}
@ -880,7 +880,7 @@ fn insert_late_bound_region(&mut self, r: ty::Region<'tcx>, vid: ty::RegionVid)
/// reference those regions from the `ParamEnv`. It is also used
/// during initialization. Relies on the `indices` map having been
/// fully initialized.
pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
if let ty::ReVar(..) = *r {
r.as_var()
} else if let ty::ReError(guar) = *r {
@ -899,7 +899,7 @@ pub fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
/// Replaces all free regions in `value` with region vids, as
/// returned by `to_region_vid`.
pub fn fold_to_region_vids<T>(&self, tcx: TyCtxt<'tcx>, value: T) -> T
fn fold_to_region_vids<T>(&self, tcx: TyCtxt<'tcx>, value: T) -> T
where
T: TypeFoldable<TyCtxt<'tcx>>,
{

View File

@ -1,7 +1,7 @@
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::{Body, Local, Location};
pub trait FindAssignments {
pub(crate) trait FindAssignments {
// Finds all statements that assign directly to local (i.e., X = ...)
// and returns their locations.
fn find_assignments(&self, local: Local) -> Vec<Location>;

View File

@ -1,3 +1,3 @@
mod collect_writes;
pub use collect_writes::FindAssignments;
pub(crate) use collect_writes::FindAssignments;