pub(crate) struct MirBorrowckCtxt<'cx, 'tcx> {
Show 22 fields pub(crate) infcx: &'cx InferCtxt<'tcx>, pub(crate) param_env: ParamEnv<'tcx>, pub(crate) body: &'cx Body<'tcx>, pub(crate) move_data: &'cx MoveData<'tcx>, pub(crate) location_table: &'cx LocationTable, pub(crate) movable_generator: bool, pub(crate) locals_are_invalidated_at_exit: bool, pub(crate) access_place_error_reported: FxHashSet<(Place<'tcx>, Span)>, pub(crate) reservation_error_reported: FxHashSet<Place<'tcx>>, pub(crate) fn_self_span_reported: FxHashSet<Span>, pub(crate) uninitialized_error_reported: FxHashSet<PlaceRef<'tcx>>, pub(crate) used_mut: FxHashSet<Local>, pub(crate) used_mut_upvars: SmallVec<[Field; 8]>, pub(crate) regioncx: Rc<RegionInferenceContext<'tcx>>, pub(crate) borrow_set: Rc<BorrowSet<'tcx>>, pub(crate) dominators: Dominators<BasicBlock>, pub(crate) upvars: Vec<Upvar<'tcx>>, pub(crate) local_names: IndexVec<Local, Option<Symbol>>, pub(crate) region_names: RefCell<FxHashMap<RegionVid, RegionName>>, pub(crate) next_region_name: RefCell<usize>, pub(crate) polonius_output: Option<Rc<PoloniusOutput>>, pub(crate) errors: BorrowckErrors<'tcx>,
}

Fields§

§infcx: &'cx InferCtxt<'tcx>§param_env: ParamEnv<'tcx>§body: &'cx Body<'tcx>§move_data: &'cx MoveData<'tcx>§location_table: &'cx LocationTable

Map from MIR Location to LocationIndex; created when MIR borrowck begins.

§movable_generator: bool§locals_are_invalidated_at_exit: bool

This keeps track of whether local variables are free-ed when the function exits even without a StorageDead, which appears to be the case for constants.

I’m not sure this is the right approach - @eddyb could you try and figure this out?

§access_place_error_reported: FxHashSet<(Place<'tcx>, Span)>

This field keeps track of when borrow errors are reported in the access_place function so that there is no duplicate reporting. This field cannot also be used for the conflicting borrow errors that is handled by the reservation_error_reported field as the inclusion of the Span type (while required to mute some errors) stops the muting of the reservation errors.

§reservation_error_reported: FxHashSet<Place<'tcx>>

This field keeps track of when borrow conflict errors are reported for reservations, so that we don’t report seemingly duplicate errors for corresponding activations.

§fn_self_span_reported: FxHashSet<Span>

This fields keeps track of the Spans that we have used to report extra information for FnSelfUse, to avoid unnecessarily verbose errors.

§uninitialized_error_reported: FxHashSet<PlaceRef<'tcx>>

This field keeps track of errors reported in the checking of uninitialized variables, so that we don’t report seemingly duplicate errors.

§used_mut: FxHashSet<Local>

This field keeps track of all the local variables that are declared mut and are mutated. Used for the warning issued by an unused mutable local variable.

§used_mut_upvars: SmallVec<[Field; 8]>

If the function we’re checking is a closure, then we’ll need to report back the list of mutable upvars that have been used. This field keeps track of them.

§regioncx: Rc<RegionInferenceContext<'tcx>>

Region inference context. This contains the results from region inference and lets us e.g. find out which CFG points are contained in each borrow region.

§borrow_set: Rc<BorrowSet<'tcx>>

The set of borrows extracted from the MIR

§dominators: Dominators<BasicBlock>

Dominators for MIR

§upvars: Vec<Upvar<'tcx>>

Information about upvars not necessarily preserved in types or MIR

§local_names: IndexVec<Local, Option<Symbol>>

Names of local (user) variables (extracted from var_debug_info).

§region_names: RefCell<FxHashMap<RegionVid, RegionName>>

Record the region names generated for each region in the given MIR def so that we can reuse them later in help/error messages.

§next_region_name: RefCell<usize>

The counter for generating new region names.

§polonius_output: Option<Rc<PoloniusOutput>>

Results of Polonius analysis.

§errors: BorrowckErrors<'tcx>

Implementations§

Signal an error due to an attempt to move out of the interior of an array or slice. is_index is None when error origin didn’t capture whether there was an indexing operation or not.

Generate a synthetic region named 'N, where N is the next value of the counter. Then, increment the counter.

This is not idempotent. Call give_region_a_name when possible.

Maps from an internal MIR region vid to something that we can report to the user. In some cases, the region vids will map directly to lifetimes that the user has a name for (e.g., 'static). But frequently they will not, in which case we have to find some way to identify the lifetime to the user. To that end, this function takes a “diagnostic” so that it can create auxiliary notes as needed.

The names are memoized, so this is both cheap to recompute and idempotent.

Example (function arguments):

Suppose we are trying to give a name to the lifetime of the reference x:

fn foo(x: &u32) { .. }

This function would create a label like this:

 | fn foo(x: &u32) { .. }
          ------- fully elaborated type of `x` is `&'1 u32`

and then return the name '1 for us to use.

Checks for the case where fr maps to something that the user has a name for. In that case, we’ll be able to map fr to a Region<'tcx>, and that region will be one of named variants.

Finds an argument that contains fr and label it with a fully elaborated type, returning something like '1. Result looks like:

 | fn foo(x: &u32) { .. }
          ------- fully elaborated type of `x` is `&'1 u32`

Attempts to highlight the specific part of a type in an argument that has no type annotation. For example, we might produce an annotation like this:

 |     foo(|a, b| b)
 |          -  -
 |          |  |
 |          |  has type `&'1 u32`
 |          has type `&'2 u32`

Attempts to highlight the specific part of a type annotation that contains the anonymous reference we want to give a name to. For example, we might produce an annotation like this:

 | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item = &T>> {
 |                - let's call the lifetime of this reference `'1`

the way this works is that we match up ty, which is a Ty<'tcx> (the internal form of the type) with hir_ty, a hir::Ty (the syntax of the type annotation). We are descending through the types stepwise, looking in to find the region needle_fr in the internal type. Once we find that, we can use the span of the hir::Ty to add the highlight.

This is a somewhat imperfect process, so along the way we also keep track of the closest type we’ve found. If we fail to find the exact & or '_ to highlight, then we may fall back to highlighting that closest type instead.

We’ve found an enum/struct/union type with the substitutions substs and – in the HIR – a path type with the final segment last_segment. Try to find a '_ to highlight in the generic args (or, if not, to produce new zipped pairs of types+hir to search through).

We’ve found an enum/struct/union type with the substitutions substs and – in the HIR – a path with the generic arguments args. If needle_fr appears in the args, return the hir::Lifetime that corresponds to it. If not, push onto search_stack the types+hir to search through.

Finds a closure upvar that contains fr and label it with a fully elaborated type, returning something like '1. Result looks like:

 | let x = Some(&22);
       - fully elaborated type of `x` is `Option<&'1 u32>`

Checks for arguments appearing in the (closure) return type. It must be a closure since, in a free fn, such an argument would have to either also appear in an argument (if using elision) or be early bound (named, not in argument).

From the hir::Ty of an async function’s lowered return type, retrieve the hir::Ty representing the type the user originally wrote.

e.g. given the function:

async fn foo() -> i32 { 2 }

this function, given the lowered return type of foo, an OpaqueDef that implements Future<Output=i32>, returns the i32.

Returns the description of the root place for a conflicting borrow and the full descriptions of the places that caused the conflict.

In the simplest case, where there are no unions involved, if a mutable borrow of x is attempted while a shared borrow is live, then this function will return:

("x", "", "")

In the simple union case, if a mutable borrow of a union field x.z is attempted while a shared borrow of another field x.y, then this function will return:

("x", "x.z", "x.y")

In the more complex union case, where the union is a field of a struct, then if a mutable borrow of a union field in a struct x.u.z is attempted while a shared borrow of another field x.u.y, then this function will return:

("x.u", "x.u.z", "x.u.y")

This is used when creating error messages like below:

cannot borrow `a.u` (via `a.u.z.c`) as immutable because it is also borrowed as
mutable (via `a.u.s.b`) [E0502]

Reports StorageDeadOrDrop of place conflicts with borrow.

This means that some data referenced by borrow needs to live past the point where the StorageDeadOrDrop of place occurs. This is usually interpreted as meaning that place has too short a lifetime. (But sometimes it is more useful to report it as a more direct conflict between the execution of a Drop::drop with an aliasing borrow.)

Reports an illegal reassignment; for example, an assignment to (part of) a non-mut local that occurs potentially after that local has already been initialized. place is the path being assigned; err_place is a place providing a reason why place is not mutable (e.g., the non-mut local x in an assignment to x.f).

Describe the reason for the fake borrow that was assigned to place.

Annotate argument and return type of function and closure with (synthesized) lifetime for borrow of local value that does not live long enough.

Annotate the first argument and return type of a function signature if they are references.

Returns structured explanation for why the borrow contains the point from location. This is key for the “3-point errors” described in the NLL RFC.

Parameters
  • borrow: the borrow in question
  • location: where the borrow occurs
  • kind_place: if Some, this describes the statement that triggered the error.
    • first half is the kind of write, if any, being performed
    • second half is the place being accessed

Determine how the borrow was later used. First span returned points to the location of the conflicting use Second span if Some is returned in the case of closures and points to the use of the path

Checks if a borrowed value was captured by a trait object. We do this by looking forward in the MIR from the reserve location and checking if we see an unsized cast to a trait object on our data.

User cannot make signature of a trait mutable without changing the trait. So we find if this error belongs to a trait and if so we move suggestion to the trait or disable it if it is out of scope of this crate

Targeted error when encountering an FnMut closure where an Fn closure was expected.

Converts a region inference variable into a ty::Region that we can use for error reporting. If r is universally bound, then we use the name that we have on record for it. If r is existentially bound, then we check its inferred value and try to find a good name from that. Returns None if we can’t find one (e.g., this is just some random part of the CFG).

Returns the RegionVid corresponding to the region returned by to_error_region.

Returns true if a closure is inferred to be an FnMut closure.

Produces nice borrowck error diagnostics for all the errors collected in nll_errors.

Report an error because the universal region fr was required to outlive outlived_fr but it is not known to do so. For example:

fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }

Here we would be invoked with fr = 'a and outlived_fr = ’b`.

Report a specialized error when FnMut closures return a reference to a captured variable. This function expects fr to be local and outlived_fr to not be local.

error: captured variable cannot escape `FnMut` closure body
  --> $DIR/issue-53040.rs:15:8
   |
LL |     || &mut v;
   |     -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
   |     |
   |     inferred to be a `FnMut` closure
   |
   = note: `FnMut` closures only have access to their captured variables while they are
           executing...
   = note: ...therefore, returned references to captured variables will escape the closure

Reports an error specifically for when data is escaping a closure.

error: borrowed data escapes outside of function
  --> $DIR/lifetime-bound-will-change-warning.rs:44:5
   |
LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
   |              - `x` is a reference that is only valid in the function body
LL |     // but ref_obj will not, so warn.
LL |     ref_obj(x)
   |     ^^^^^^^^^^ `x` escapes the function body here

Reports a region inference error for the general case with named/synthesized lifetimes to explain what is happening.

error: unsatisfied lifetime constraints
  --> $DIR/regions-creating-enums3.rs:17:5
   |
LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
   |                -- -- lifetime `'b` defined here
   |                |
   |                lifetime `'a` defined here
LL |     ast::add(x, y)
   |     ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
   |                    is returning data with lifetime `'b`

Adds a suggestion to errors where an impl Trait is returned.

help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
      a constraint
   |
LL |     fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Adds a suggestion when a closure is invoked twice with a moved variable or when a closure is moved after being invoked.

note: closure cannot be invoked more than once because it moves the variable `dict` out of
      its environment
  --> $DIR/issue-42065.rs:16:29
   |
LL |         for (key, value) in dict {
   |                             ^^^^

End-user visible description of place if one can be found. If the place is a temporary for instance, "value" will be returned.

End-user visible description of place if one can be found. If the place is a temporary for instance, None will be returned.

End-user visible description of place if one can be found. If the place is a temporary for instance, None will be returned. IncludingDowncast parameter makes the function return None if ProjectionElem is Downcast and IncludingDowncast is true

Appends end-user visible description of the local place to buf. If local doesn’t have a name, or its name was generated by the compiler, then Err is returned

End-user visible description of the fieldnth field of base

End-user visible description of the field_indexnth field of ty

Add a note that a type does not implement Copy

Return the name of the provided Ty (that must be a reference) with a synthesized lifetime name where required.

Returns the name of the provided Ty (that must be a reference)’s region with a synthesized lifetime name where required.

Finds the spans associated to a move or copy of move_place at location.

Finds the span of arguments of a closure (within maybe_closure_span) and its usage of the local assigned at location. This is done by searching in statements succeeding location and originating from maybe_closure_span.

Finds the spans of a captured place within a closure or generator. The first span is the location of the use resulting in the capture kind of the capture The second span is the location the use resulting in the captured path of the capture

Helper to retrieve span(s) of given borrow from the current MIR representation

Returns an iterator over the prefixes of place (inclusive) from longest to smallest, potentially terminating the iteration early based on kind.

Walks the MIR adding to the set of used_mut locals that will be ignored for the purposes of the unused_mut lint.

temporary_used_locals should contain locals that were found to be temporary, mutable and used from borrow checking. This function looks for assignments into these locals from user-declared locals and adds those user-defined locals to the used_mut set. This can occur due to a rare case involving upvars in closures.

never_initialized_mut_locals should contain the set of user-declared mutable locals (not arguments) that have not already been marked as being used. This function then looks for assignments from statements or the terminator into the locals from this set and removes them from the set. This leaves only those locals that have not been assigned to - this set is used as a proxy for locals that were not initialized due to unreachable code. These locals are then considered “used” to silence the lint for them. See #55344 for context.

Checks an access to the given place to see if it is allowed. Examines the set of borrows that are in scope, as well as which paths have been initialized, to ensure that (a) the place is initialized and (b) it is not borrowed in some way that would prevent this access.

Returns true if an error is reported.

Checks whether a borrow of this place is invalidated when the function exits

Reports an error if this is a borrow of local data. This is called for all Yield expressions on movable generators

Subslices correspond to multiple move paths, so we iterate through the elements of the base array. For each element we check

  • Does this element overlap with our slice.
  • Is any part of it uninitialized.

Currently MoveData does not store entries for all places in the input MIR. For example it will currently filter out places that are Copy; thus we do not track places of shared reference type. This routine will walk up a place along its prefixes, searching for a foundational place that is tracked in the MoveData.

An Err result includes a tag indicated why the search failed. Currently this can only occur if the place is built off of a static variable, as we do not track those in the MoveData.

Checks the permissions for the given place and read or write kind

Returns true if an error is reported.

Adds the place into the used mutable variables set

Whether this value can be written or borrowed mutably. Returns the root place if the place passed in is a projection.

If place is a field projection, and the field is being projected from a closure type, then returns the index of the field being projected. Note that this closure will always be self in the current MIR, because that is the only time we directly access the fields of a closure type.

Trait Implementations§

Called with the before_statement_effect of the given statement applied to state but not its statement_effect. Read more
Called with the before_terminator_effect of the given terminator applied to state but not its terminator_effect. Read more
Called with both the before_terminator_effect and the terminator_effect of the given terminator applied to state. Read more
Called with both the before_statement_effect and the statement_effect of the given statement applied to state. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.

Size: 488 bytes